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 {
defer fasthttp.ReleaseResponse(pr.Resp)
defer prpool.Put(pr)
return pr.Resp.CloseBodyStream()
pr.Resp.CloseBodyStream()
fasthttp.ReleaseResponse(pr.Resp)
pr.Reader = nil
pr.Resp = nil
prpool.Put(pr)
return nil
}
func Log(what ...any) {

View File

@@ -11,6 +11,7 @@ import (
)
var httpc *fasthttp.HostClient
var al_httpc *fasthttp.HostClient
func Load(r fiber.Router) {
httpc = &fasthttp.HostClient{
@@ -22,6 +23,15 @@ func Load(r fiber.Router) {
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 {
url := c.Query("url")
if url == "" {
@@ -40,7 +50,13 @@ func Load(r fiber.Router) {
return fiber.ErrBadRequest
}
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()
defer fasthttp.ReleaseRequest(req)
@@ -52,7 +68,7 @@ func Load(r fiber.Router) {
resp := fasthttp.AcquireResponse()
//defer fasthttp.ReleaseResponse(resp) moved to proxyreader!!!
err = sc.DoWithRetry(httpc, req, resp)
err = sc.DoWithRetry(cl, req, resp)
if err != nil {
return err
}

View File

@@ -19,7 +19,7 @@ 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"!
Kind string `json:"kind"` // should always be "playlist"! or "system-playlist"
LastModified string `json:"last_modified"`
Likes int64 `json:"likes_count"`
Permalink string `json:"permalink"`
@@ -57,7 +57,7 @@ func GetPlaylist(cid string, permalink string) (Playlist, error) {
return p, err
}
if p.Kind != "playlist" {
if p.Kind != "playlist" && p.Kind != "system-playlist" {
return p, ErrKindNotCorrect
}
@@ -217,5 +217,17 @@ func (p *Playlist) GetMissingTracks(cid string) error {
}
func (p Playlist) Href() string {
if p.Kind == "system-playlist" {
return "/discover/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"`
Author User `json:"user"`
Policy TrackPolicy `json:"policy"`
Station string `json:"station_permalink"`
}
type TrackPolicy string

View File

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

View File

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

View File

@@ -9,7 +9,7 @@ import (
templ TagsButtons(current string, tag string) {
<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 {
<a class="btn active">{ b.text }</a>
} else {

View File

@@ -23,17 +23,21 @@ func toExt(audio string) string {
templ TrackButtons(current string, track sc.Track) {
<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 {
<a class="btn active">{ b.text }</a>
} else {
if b.external {
<a class="btn" href={ templ.URL(b.href) } referrerpolicy="no-referrer" rel="external nofollow noopener noreferrer" target="_blank">{ b.text }</a>
} else {
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>
}
}
}
}
</div>
}

View File

@@ -77,20 +77,25 @@ templ UserBase(prefs cfg.Preferences, u sc.User) {
type btn struct {
text string
href string
override bool
external bool
}
templ UserButtons(current string, user string) {
templ UserButtons(current string, user sc.User) {
<div class="btns">
// 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 {
<a class="btn active">{ b.text }</a>
} else {
if b.external {
<a class="btn" href={ templ.URL(b.href) } referrerpolicy="no-referrer" rel="external nofollow noopener noreferrer" target="_blank">{ b.text }</a>
} 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)
// kinda tedious but whatever, might make it more flexible in the future
// ^ outdated, no longer tedious but whatever
@UserButtons("tracks", u.Permalink)
@UserButtons("tracks", u)
<br/>
if len(p.Collection) != 0 {
<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]) {
@UserBase(prefs, u)
@UserButtons("playlists", u.Permalink)
@UserButtons("playlists", u)
<br/>
if len(p.Collection) != 0 {
<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]) {
@UserBase(prefs, u)
@UserButtons("albums", u.Permalink)
@UserButtons("albums", u)
<br/>
if len(p.Collection) != 0 {
<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]) {
@UserBase(prefs, u)
@UserButtons("reposts", u.Permalink)
@UserButtons("reposts", u)
<br/>
if len(p.Collection) != 0 {
<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]) {
@UserBase(prefs, u)
@UserButtons("", u.Permalink)
@UserButtons("", u)
<h1>Likes</h1>
if len(p.Collection) != 0 {
<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) {
@UserBase(prefs, u)
@UserButtons("related", u.Permalink)
@UserButtons("related", u)
<br/>
if len(r) != 0 {
<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) {
@UserBase(prefs, u)
@UserButtons("popular tracks", u.Permalink)
@UserButtons("popular tracks", u)
<br/>
if len(t) != 0 {
<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]) {
@UserBase(prefs, u)
@UserButtons("", u.Permalink)
@UserButtons("", u)
<h1>Followers</h1>
if len(p.Collection) != 0 {
<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]) {
@UserBase(prefs, u)
@UserButtons("", u.Permalink)
@UserButtons("", u)
<h1>Following</h1>
if len(p.Collection) != 0 {
<div>