/discover - featured playlists, some more fixes

This commit is contained in:
Laptop
2024-11-23 00:22:04 +02:00
parent 0e8244b3cc
commit ea3426edd9
7 changed files with 103 additions and 27 deletions

View File

@@ -137,7 +137,7 @@ footer {
margin-top: 5rem;
gap: 1rem;
display: grid;
grid-template: auto / auto auto;
grid-template: auto / auto auto auto;
justify-content: center;
}

View File

@@ -25,6 +25,7 @@
</form>
<footer>
<a class="btn" href="/_/featured">Featured Tracks</a>
<a class="btn" href="/discover">Discover Playlists</a>
<a class="btn" href="/_/preferences">Preferences</a>
<a class="btn" href="https://github.com/maid-zone/soundcloak">Source code</a>

59
lib/sc/featured.go Normal file
View File

@@ -0,0 +1,59 @@
package sc
import "github.com/maid-zone/soundcloak/lib/cfg"
// Functions/structions related to featured/suggested content
type Selection struct {
Title string `json:"title"`
Kind string `json:"kind"` // should always be "selection"!
Items Paginated[*Playlist] `json:"items"` // ?? why
}
func GetFeaturedTracks(prefs cfg.Preferences, args string) (*Paginated[*Track], error) {
cid, err := GetClientID()
if err != nil {
return nil, err
}
p := Paginated[*Track]{Next: "https://" + api + "/featured_tracks/top/all-music" + args + "&client_id=" + cid}
// DO NOT UNFOLD
// dangerous
// seems to go in an infinite loop
err = p.Proceed(false)
if err != nil {
return nil, err
}
for _, t := range p.Collection {
t.Fix(prefs, false)
}
return &p, nil
}
func GetSelections(prefs cfg.Preferences) (*Paginated[*Selection], error) {
cid, err := GetClientID()
if err != nil {
return nil, err
}
// There is no pagination
p := Paginated[*Selection]{Next: "https://" + api + "/mixed-selections?limit=20&client_id=" + cid}
err = p.Proceed(false)
if err != nil {
return nil, err
}
for _, t := range p.Collection {
t.Fix(prefs)
}
return &p, nil
}
func (s *Selection) Fix(prefs cfg.Preferences) {
for _, p := range s.Items.Collection {
p.Fix(prefs, false)
}
}

View File

@@ -397,25 +397,3 @@ func GetTrackByID(prefs cfg.Preferences, id string) (Track, error) {
return t, nil
}
func GetFeaturedTracks(prefs cfg.Preferences, args string) (*Paginated[*Track], error) {
cid, err := GetClientID()
if err != nil {
return nil, err
}
p := Paginated[*Track]{Next: "https://" + api + "/featured_tracks/top/all-music" + args + "&client_id=" + cid}
// DO NOT UNFOLD
// dangerous
// seems to go in an infinite loop
err = p.Proceed(false)
if err != nil {
return nil, err
}
for _, t := range p.Collection {
t.Fix(prefs, false)
}
return &p, nil
}

16
main.go
View File

@@ -176,6 +176,22 @@ func main() {
return templates.Base("Featured Tracks", templates.FeaturedTracks(tracks), nil).Render(context.Background(), c)
})
app.Get("/discover", func(c *fiber.Ctx) error {
prefs, err := preferences.Get(c)
if err != nil {
return err
}
selections, err := sc.GetSelections(prefs) // There is no pagination
if err != nil {
log.Printf("error getting selections: %s\n", err)
return err
}
c.Set("Content-Type", "text/html")
return templates.Base("Discover", templates.Discover(selections), nil).Render(context.Background(), c)
})
if cfg.ProxyImages {
proxyimages.Load(app)
}

View File

@@ -1,16 +1,14 @@
package templates
// I made a separate templates file for this, since I'm planning on adding another "featured" feature later (soundcloud.com/discover)
import (
"github.com/maid-zone/soundcloak/lib/sc"
"net/url"
"strings"
"strconv"
)
templ FeaturedTracks(p *sc.Paginated[*sc.Track]) {
<h1>Featured Tracks</h1>
<br/>
if len(p.Collection) == 0 {
<p>no more tracks</p>
} else {
@@ -22,3 +20,27 @@ templ FeaturedTracks(p *sc.Paginated[*sc.Track]) {
}
}
}
templ Discover(p *sc.Paginated[*sc.Selection]) {
<h1>Discover Playlists</h1> // also tracks apparently? haven't seen any
<span>Got { strconv.FormatInt(int64(len(p.Collection)), 10) } playlists</span>
if len(p.Collection) != 0 {
for _, selection := range p.Collection {
// skip so we don't just include the title and zero playlists with it
if len(selection.Items.Collection) == 0 {
{{ continue }} // raw go code my beloved
}
<h2>{selection.Title}</h2>
for _, pl := range selection.Items.Collection {
// We don't need the username
@PlaylistItem(pl, false)
}
}
// I don't think they have pagination for this endpoint, just leaving this here, in case they actually do
// if p.Next != "" {
// <a class="btn" href={ templ.URL("/discover?pagination=" + url.QueryEscape(strings.Split(p.Next, "/discover")[1])) } rel="noreferrer">more playlists</a>
// }
}
}

View File

@@ -38,7 +38,7 @@ templ Playlist(p sc.Playlist) {
}
<h1>{ p.Title }</h1>
@UserItem(&p.Author)
<div class="btns">
<div style="display: flex;">
<a class="btn" href={ templ.URL("https://soundcloud.com/" + p.Author.Permalink + "/sets/" + p.Permalink) }>view on soundcloud</a>
</div>
<br/>