From da7661b4c5285af82997dcf05ff0e3c1bdbf9b5e Mon Sep 17 00:00:00 2001 From: Laptop Date: Mon, 26 Aug 2024 12:15:05 +0300 Subject: [PATCH] better playlist page --- lib/sc/init.go | 70 +++++++++++++++++++++++++++++++++++++++- lib/sc/structs.go | 28 ++++++++-------- templates/playlist.templ | 31 +++++++++++++++++- 3 files changed, 113 insertions(+), 16 deletions(-) diff --git a/lib/sc/init.go b/lib/sc/init.go index 5fb5475..82570f9 100644 --- a/lib/sc/init.go +++ b/lib/sc/init.go @@ -369,10 +369,15 @@ func GetPlaylist(permalink string) (Playlist, error) { } if u.Kind != "playlist" { - fmt.Println(u.Kind) return u, ErrKindNotCorrect } + err = u.GetMissingTracks() + if err != nil { + return u, err + } + + u.Artwork = strings.Replace(u.Artwork, "-large.", "-t200x200.", 1) playlistsCache[permalink] = cached[Playlist]{Value: u, Expires: time.Now().Add(cfg.PlaylistTTL)} return u, nil @@ -460,3 +465,66 @@ func (p Playlist) FormatDescription() string { return desc } + +type missingtrack struct { + ID int64 + Index int +} + +func (p *Playlist) GetMissingTracks() error { + cid, err := GetClientID() + if err != nil { + return err + } + + missing := []missingtrack{} + for i, track := range p.Tracks { + if track.Title == "" { + missing = append(missing, missingtrack{ID: track.ID, Index: i}) + } + } + + var st string + for i, track := range missing { + st += strconv.FormatInt(track.ID, 10) + if i != len(missing)-1 { + st += "," + } + } + + req := fasthttp.AcquireRequest() + defer fasthttp.ReleaseRequest(req) + + req.SetRequestURI("https://api-v2.soundcloud.com/tracks?ids=" + st + "&client_id=" + cid) + req.Header.Set("User-Agent", cfg.UserAgent) + req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd") + + resp := fasthttp.AcquireResponse() + defer fasthttp.ReleaseResponse(resp) + + err = DoWithRetry(req, resp) + if err != nil { + return err + } + + data, err := resp.BodyUncompressed() + if err != nil { + data = resp.Body() + } + + var res []Track + err = cfg.JSON.Unmarshal(data, &res) + if err != nil { + return err + } + + for _, oldTrack := range missing { + for _, newTrack := range res { + if newTrack.ID == oldTrack.ID { + p.Tracks[oldTrack.Index] = newTrack + } + } + } + + return nil +} diff --git a/lib/sc/structs.go b/lib/sc/structs.go index 5c20663..d457c96 100644 --- a/lib/sc/structs.go +++ b/lib/sc/structs.go @@ -60,7 +60,7 @@ type Track struct { Description string `json:"description"` //Duration int `json:"duration"` // there are duration and full_duration fields wtf does that mean Genre string `json:"genre"` - ID int `json:"id"` + ID int64 `json:"id"` Kind string `json:"kind"` // should always be "track"! LastModified string `json:"last_modified"` Likes int64 `json:"likes_count"` @@ -84,17 +84,17 @@ type Stream struct { } type Playlist struct { - Artwork string `json:"artwork_url"` - CreatedAt string `json:"created_at"` - Description string `json:"description"` - Kind string `json:"kind"` // should always be "playlist"! - LastModified string `json:"last_modified"` - Likes int64 `json:"likes_count"` - ReleaseDate string `json:"release_date"` - TagList string `json:"tag_list"` - Title string `json:"title"` - Type string `json:"set_type"` - Album bool `json:"is_album"` - Author User `json:"user"` - Tracks []Track `json:"tracks"` + Artwork string `json:"artwork_url"` + CreatedAt string `json:"created_at"` + Description string `json:"description"` + Kind string `json:"kind"` // should always be "playlist"! + LastModified string `json:"last_modified"` + Likes int64 `json:"likes_count"` + //ReleaseDate string `json:"release_date"` + TagList string `json:"tag_list"` + Title string `json:"title"` + Type string `json:"set_type"` + Album bool `json:"is_album"` + Author User `json:"user"` + Tracks []Track `json:"tracks"` } diff --git a/templates/playlist.templ b/templates/playlist.templ index c47f5ec..66abff7 100644 --- a/templates/playlist.templ +++ b/templates/playlist.templ @@ -1,6 +1,10 @@ package templates -import "github.com/maid-zone/soundcloak/lib/sc" +import ( + "github.com/maid-zone/soundcloak/lib/sc" + "strings" + "strconv" +) templ PlaylistEmbed(p sc.Playlist) { @@ -10,5 +14,30 @@ templ PlaylistEmbed(p sc.Playlist) { } templ Playlist(p sc.Playlist) { + if p.Artwork != "" { + + }

{p.Title}

+

{p.Description}

+ + +

{strconv.FormatInt(int64(len(p.Tracks)), 10)} tracks

+
+ for _, track := range p.Tracks { + if track.Title != "" { +

{track.Title}

+ } + } +
+ +
+ if p.TagList != "" { +

Tags: { strings.Join(sc.TagListParser(p.TagList), ",") }

+ } + +

{ strconv.FormatInt(p.Likes, 10) } likes

+
+

Created: { p.CreatedAt }

+

Last modified: { p.LastModified }

+
} \ No newline at end of file