some fixes && implement stations

This commit is contained in:
Laptop
2024-12-28 09:06:53 +02:00
parent 752ce59c12
commit 316953930b
9 changed files with 74 additions and 26 deletions

View File

@@ -29,9 +29,14 @@ func (pr *ProxyReader) Read(p []byte) (int, error) {
} }
func (pr *ProxyReader) Close() error { func (pr *ProxyReader) Close() error {
defer fasthttp.ReleaseResponse(pr.Resp) pr.Resp.CloseBodyStream()
defer prpool.Put(pr) fasthttp.ReleaseResponse(pr.Resp)
return pr.Resp.CloseBodyStream()
pr.Reader = nil
pr.Resp = nil
prpool.Put(pr)
return nil
} }
func Log(what ...any) { func Log(what ...any) {

View File

@@ -11,6 +11,7 @@ import (
) )
var httpc *fasthttp.HostClient var httpc *fasthttp.HostClient
var al_httpc *fasthttp.HostClient
func Load(r fiber.Router) { func Load(r fiber.Router) {
httpc = &fasthttp.HostClient{ httpc = &fasthttp.HostClient{
@@ -22,6 +23,15 @@ func Load(r fiber.Router) {
StreamResponseBody: true, StreamResponseBody: true,
} }
al_httpc = &fasthttp.HostClient{
Addr: "al.sndcdn.com:443",
IsTLS: true,
DialDualStack: true,
Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial,
MaxIdleConnDuration: 1<<63 - 1,
StreamResponseBody: true,
}
r.Get("/_/proxy/images", func(c *fiber.Ctx) error { r.Get("/_/proxy/images", func(c *fiber.Ctx) error {
url := c.Query("url") url := c.Query("url")
if url == "" { if url == "" {
@@ -40,7 +50,13 @@ func Load(r fiber.Router) {
return fiber.ErrBadRequest return fiber.ErrBadRequest
} }
parsed.SetHost(cfg.ImageCDN) var cl *fasthttp.HostClient
if parsed.Host()[0] == 'i' {
parsed.SetHost(cfg.ImageCDN)
cl = httpc
} else if string(parsed.Host()[:2]) == "al" {
cl = al_httpc
}
req := fasthttp.AcquireRequest() req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req) defer fasthttp.ReleaseRequest(req)
@@ -52,7 +68,7 @@ func Load(r fiber.Router) {
resp := fasthttp.AcquireResponse() resp := fasthttp.AcquireResponse()
//defer fasthttp.ReleaseResponse(resp) moved to proxyreader!!! //defer fasthttp.ReleaseResponse(resp) moved to proxyreader!!!
err = sc.DoWithRetry(httpc, req, resp) err = sc.DoWithRetry(cl, req, resp)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -19,7 +19,7 @@ type Playlist struct {
Artwork string `json:"artwork_url"` Artwork string `json:"artwork_url"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
Description string `json:"description"` Description string `json:"description"`
Kind string `json:"kind"` // should always be "playlist"! Kind string `json:"kind"` // should always be "playlist"! or "system-playlist"
LastModified string `json:"last_modified"` LastModified string `json:"last_modified"`
Likes int64 `json:"likes_count"` Likes int64 `json:"likes_count"`
Permalink string `json:"permalink"` Permalink string `json:"permalink"`
@@ -57,7 +57,7 @@ func GetPlaylist(cid string, permalink string) (Playlist, error) {
return p, err return p, err
} }
if p.Kind != "playlist" { if p.Kind != "playlist" && p.Kind != "system-playlist" {
return p, ErrKindNotCorrect return p, ErrKindNotCorrect
} }
@@ -217,5 +217,17 @@ func (p *Playlist) GetMissingTracks(cid string) error {
} }
func (p Playlist) Href() string { func (p Playlist) Href() string {
if p.Kind == "system-playlist" {
return "/discover/sets/" + p.Permalink
}
return "/" + p.Author.Permalink + "/sets/" + p.Permalink return "/" + p.Author.Permalink + "/sets/" + p.Permalink
} }
func (p Playlist) TracksCount() int64 {
if p.TrackCount != 0 {
return p.TrackCount
}
return int64(len(p.Tracks))
}

View File

@@ -44,6 +44,7 @@ type Track struct {
Authorization string `json:"track_authorization"` Authorization string `json:"track_authorization"`
Author User `json:"user"` Author User `json:"user"`
Policy TrackPolicy `json:"policy"` Policy TrackPolicy `json:"policy"`
Station string `json:"station_permalink"`
} }
type TrackPolicy string type TrackPolicy string

View File

@@ -35,6 +35,7 @@ type User struct {
ID json.Number `json:"id"` ID json.Number `json:"id"`
Username string `json:"username"` Username string `json:"username"`
Verified bool `json:"verified"` Verified bool `json:"verified"`
Station string `json:"station_permalink"`
WebProfiles []Link WebProfiles []Link
} }

View File

@@ -32,7 +32,7 @@ templ PlaylistItem(playlist *sc.Playlist, showUsername bool) {
if showUsername { if showUsername {
<span>{ playlist.Author.Username }</span> <span>{ playlist.Author.Username }</span>
} }
<p>{ strconv.FormatInt(playlist.TrackCount, 10) } tracks</p> <p>{ strconv.FormatInt(playlist.TracksCount(), 10) } tracks</p>
</div> </div>
</a> </a>
} }
@@ -48,7 +48,7 @@ templ Playlist(prefs cfg.Preferences, p sc.Playlist) {
</div> </div>
<br/> <br/>
@Description(prefs, p.Description, nil) @Description(prefs, p.Description, nil)
<p>{ strconv.FormatInt(p.TrackCount, 10) } tracks</p> <p>{ strconv.FormatInt(p.TracksCount(), 10) } tracks</p>
<br/> <br/>
<br/> <br/>
<div> <div>
@@ -69,8 +69,12 @@ templ Playlist(prefs cfg.Preferences, p sc.Playlist) {
} }
<p>{ strconv.FormatInt(p.Likes, 10) } likes</p> <p>{ strconv.FormatInt(p.Likes, 10) } likes</p>
<br/> <br/>
<p>Created: { p.CreatedAt }</p> if p.CreatedAt != "" {
<p>Last modified: { p.LastModified }</p> <p>Created: { p.CreatedAt }</p>
}
if p.LastModified != "" {
<p>Last modified: { p.LastModified }</p>
}
</div> </div>
} }

View File

@@ -9,7 +9,7 @@ import (
templ TagsButtons(current string, tag string) { templ TagsButtons(current string, tag string) {
<div class="btns"> <div class="btns">
for _, b := range [...]btn{{"recent tracks", "", false},{"popular tracks", "/popular-tracks", false},{"playlists", "/playlists",false}} { for _, b := range [...]btn{{"recent tracks", "", false, false},{"popular tracks", "/popular-tracks", false, false},{"playlists", "/playlists", false, false}} {
if b.text == current { if b.text == current {
<a class="btn active">{ b.text }</a> <a class="btn active">{ b.text }</a>
} else { } else {

View File

@@ -23,14 +23,18 @@ func toExt(audio string) string {
templ TrackButtons(current string, track sc.Track) { templ TrackButtons(current string, track sc.Track) {
<div class="btns"> <div class="btns">
for _, b := range [...]btn{{"related tracks", "/recommended", false},{"in albums", "/albums", false},{"in playlists", "/sets", false},{"view on soundcloud", "https://soundcloud.com"+track.Href(), true}} { for _, b := range [...]btn{{"related tracks", "/recommended", false, false},{"in albums", "/albums", false, false},{"in playlists", "/sets", false, false},{"track station", "/discover/sets/"+track.Station, true, false},{"view on soundcloud", "https://soundcloud.com"+track.Href(), true, true}} {
if b.text == current { if b.text == current {
<a class="btn active">{ b.text }</a> <a class="btn active">{ b.text }</a>
} else { } else {
if b.external { if b.external {
<a class="btn" href={ templ.URL(b.href) } referrerpolicy="no-referrer" rel="external nofollow noopener noreferrer" target="_blank">{ b.text }</a> <a class="btn" href={ templ.URL(b.href) } referrerpolicy="no-referrer" rel="external nofollow noopener noreferrer" target="_blank">{ b.text }</a>
} else { } else {
<a class="btn" href={ templ.URL(track.Href() + b.href) }>{ b.text }</a> if b.override {
<a class="btn" href={ templ.URL(b.href) }>{ b.text }</a>
} else {
<a class="btn" href={ templ.URL(track.Href() + b.href) }>{ b.text }</a>
}
} }
} }
} }

View File

@@ -77,20 +77,25 @@ templ UserBase(prefs cfg.Preferences, u sc.User) {
type btn struct { type btn struct {
text string text string
href string href string
override bool
external bool external bool
} }
templ UserButtons(current string, user string) { templ UserButtons(current string, user sc.User) {
<div class="btns"> <div class="btns">
// this part is the tedious one now, because formatting breaks if i space the list out with newlines // this part is the tedious one now, because formatting breaks if i space the list out with newlines
for _, b := range [...]btn{{"tracks", "", false},{"popular tracks", "/popular-tracks", false},{"playlists", "/sets",false},{"albums", "/albums", false},{"reposts","/reposts", false},{"related", "/_/related", false},{"view on soundcloud", "https://soundcloud.com/"+user, true}} { for _, b := range [...]btn{{"tracks", "", false, false},{"popular tracks", "/popular-tracks", false, false},{"playlists", "/sets", false, false},{"albums", "/albums", false, false},{"reposts","/reposts", false, false},{"related", "/_/related", false, false},{"user station", "/discover/sets/"+user.Station, true, false},{"view on soundcloud", "https://soundcloud.com/"+user.Permalink, true, true}} {
if b.text == current { if b.text == current {
<a class="btn active">{ b.text }</a> <a class="btn active">{ b.text }</a>
} else { } else {
if b.external { if b.external {
<a class="btn" href={ templ.URL(b.href) } referrerpolicy="no-referrer" rel="external nofollow noopener noreferrer" target="_blank">{ b.text }</a> <a class="btn" href={ templ.URL(b.href) } referrerpolicy="no-referrer" rel="external nofollow noopener noreferrer" target="_blank">{ b.text }</a>
} else { } else {
<a class="btn" href={ templ.URL("/" + user + b.href) }>{ b.text }</a> if b.override {
<a class="btn" href={ templ.URL(b.href) }>{ b.text }</a>
} else {
<a class="btn" href={ templ.URL("/" + user.Permalink + b.href) }>{ b.text }</a>
}
} }
} }
} }
@@ -101,7 +106,7 @@ templ User(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Track]) {
@UserBase(prefs, u) @UserBase(prefs, u)
// kinda tedious but whatever, might make it more flexible in the future // kinda tedious but whatever, might make it more flexible in the future
// ^ outdated, no longer tedious but whatever // ^ outdated, no longer tedious but whatever
@UserButtons("tracks", u.Permalink) @UserButtons("tracks", u)
<br/> <br/>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>
@@ -119,7 +124,7 @@ templ User(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Track]) {
templ UserPlaylists(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]) { templ UserPlaylists(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("playlists", u.Permalink) @UserButtons("playlists", u)
<br/> <br/>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>
@@ -137,7 +142,7 @@ templ UserPlaylists(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playli
templ UserAlbums(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]) { templ UserAlbums(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("albums", u.Permalink) @UserButtons("albums", u)
<br/> <br/>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>
@@ -155,7 +160,7 @@ templ UserAlbums(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]
templ UserReposts(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Repost]) { templ UserReposts(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Repost]) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("reposts", u.Permalink) @UserButtons("reposts", u)
<br/> <br/>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>
@@ -177,7 +182,7 @@ templ UserReposts(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Repost])
templ UserLikes(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Like]) { templ UserLikes(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Like]) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("", u.Permalink) @UserButtons("", u)
<h1>Likes</h1> <h1>Likes</h1>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>
@@ -199,7 +204,7 @@ templ UserLikes(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Like]) {
templ UserRelated(prefs cfg.Preferences, u sc.User, r []*sc.User) { templ UserRelated(prefs cfg.Preferences, u sc.User, r []*sc.User) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("related", u.Permalink) @UserButtons("related", u)
<br/> <br/>
if len(r) != 0 { if len(r) != 0 {
<div> <div>
@@ -214,7 +219,7 @@ templ UserRelated(prefs cfg.Preferences, u sc.User, r []*sc.User) {
templ UserTopTracks(prefs cfg.Preferences, u sc.User, t []*sc.Track) { templ UserTopTracks(prefs cfg.Preferences, u sc.User, t []*sc.Track) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("popular tracks", u.Permalink) @UserButtons("popular tracks", u)
<br/> <br/>
if len(t) != 0 { if len(t) != 0 {
<div> <div>
@@ -229,7 +234,7 @@ templ UserTopTracks(prefs cfg.Preferences, u sc.User, t []*sc.Track) {
templ UserFollowers(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.User]) { templ UserFollowers(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.User]) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("", u.Permalink) @UserButtons("", u)
<h1>Followers</h1> <h1>Followers</h1>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>
@@ -247,7 +252,7 @@ templ UserFollowers(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.User])
templ UserFollowing(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.User]) { templ UserFollowing(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.User]) {
@UserBase(prefs, u) @UserBase(prefs, u)
@UserButtons("", u.Permalink) @UserButtons("", u)
<h1>Following</h1> <h1>Following</h1>
if len(p.Collection) != 0 { if len(p.Collection) != 0 {
<div> <div>