better playlist page

This commit is contained in:
Laptop
2024-08-26 12:15:05 +03:00
parent 68fcec278b
commit da7661b4c5
3 changed files with 113 additions and 16 deletions

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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) {
<meta name="og:site_name" content={p.Author.Username + " ~ soundcloak"}>
@@ -10,5 +14,30 @@ templ PlaylistEmbed(p sc.Playlist) {
}
templ Playlist(p sc.Playlist) {
if p.Artwork != "" {
<img src={ p.Artwork }/>
}
<h1>{p.Title}</h1>
<p style="white-space: pre-wrap">{p.Description}</p>
<h1>{strconv.FormatInt(int64(len(p.Tracks)), 10)} tracks</h1>
<div>
for _, track := range p.Tracks {
if track.Title != "" {
<h2><a href={templ.URL("/" + track.Author.Permalink + "/" + track.Permalink)}>{track.Title}</a></h2>
}
}
</div>
<div>
if p.TagList != "" {
<h2>Tags: { strings.Join(sc.TagListParser(p.TagList), ",") }</h2>
}
<h2>{ strconv.FormatInt(p.Likes, 10) } likes</h2>
<br/>
<h2>Created: { p.CreatedAt }</h2>
<h2>Last modified: { p.LastModified }</h2>
</div>
}