From 9fa85bb47d83e1c863f2fdf24332296ffe7f3f20 Mon Sep 17 00:00:00 2001 From: Laptop Date: Sun, 25 Aug 2024 23:57:47 +0300 Subject: [PATCH] start working on embeds; add prefork to cfg --- lib/cfg/init.go | 4 +++ lib/sc/init.go | 67 +++++++++++++++++++++++++++++++++++++++++++ lib/sc/structs.go | 6 ++-- main.go | 18 +++++++----- templates/base.templ | 6 +++- templates/track.templ | 10 +++++-- templates/user.templ | 7 +++++ 7 files changed, 106 insertions(+), 12 deletions(-) diff --git a/lib/cfg/init.go b/lib/cfg/init.go index 61b4297..f800143 100644 --- a/lib/cfg/init.go +++ b/lib/cfg/init.go @@ -28,4 +28,8 @@ const DNSCacheTTL = 10 * time.Minute // run soundcloak on this address (localhost:4664 by default) const Addr = ":4664" +// run multiple instances of soundcloud locally to be able to handle more requests +// each one will be a separate process, so they will have separate cache +const Prefork = false + var JSON = jsoniter.ConfigFastest diff --git a/lib/sc/init.go b/lib/sc/init.go index 039ba96..ac2c4f8 100644 --- a/lib/sc/init.go +++ b/lib/sc/init.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "regexp" + "strconv" "strings" "time" @@ -375,3 +376,69 @@ func GetPlaylist(permalink string) (Playlist, error) { return u, nil } + +func TagListParser(taglist string) (res []string) { + inString := false + cur := []rune{} + for i, c := range taglist { + if c == '"' { + if i == len(taglist)-1 { + res = append(res, string(cur)) + return + } + + inString = !inString + continue + } + + if !inString && c == ' ' { + res = append(res, string(cur)) + cur = []rune{} + continue + } + + cur = append(cur, c) + } + + return +} + +func (t Track) FormatDescription() string { + desc := t.Description + if t.Description != "" { + desc += "\n\n" + } + + desc += strconv.FormatInt(t.Likes, 10) + " ❤️ | " + strconv.FormatInt(t.Played, 10) + " ▶️" + desc += "\nGenre: " + t.Genre + desc += "\nCreated: " + t.CreatedAt + desc += "\nLast modified: " + t.LastModified + if len(t.TagList) != 0 { + desc += "\nTags: " + strings.Join(TagListParser(t.TagList), ", ") + } + + return desc +} + +func (u User) FormatDescription() string { + desc := u.Description + if u.Description != "" { + desc += "\n\n" + } + + desc += strconv.FormatInt(u.Followers, 10) + " followers | " + strconv.FormatInt(u.Following, 10) + " following" + desc += "\n" + strconv.FormatInt(u.Tracks, 10) + " tracks | " + strconv.FormatInt(u.Playlists, 10) + " playlists" + desc += "\nCreated: " + u.CreatedAt + desc += "\nLast modified: " + u.LastModified + + return desc +} + +func (u User) FormatUsername() string { + res := u.Username + if u.Verified { + res += " ☑️" + } + + return res +} diff --git a/lib/sc/structs.go b/lib/sc/structs.go index 32100e7..6660217 100644 --- a/lib/sc/structs.go +++ b/lib/sc/structs.go @@ -57,14 +57,16 @@ type Track struct { Artwork string `json:"artwork_url"` Comments int `json:"comment_count"` CreatedAt string `json:"created_at"` + 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"` Kind string `json:"kind"` // should always be "track"! LastModified string `json:"last_modified"` - Likes int `json:"likes_count"` + Likes int64 `json:"likes_count"` Permalink string `json:"permalink"` - Played int `json:"playback_count"` + Played int64 `json:"playback_count"` + TagList string `json:"tag_list"` Title string `json:"title"` Media Media `json:"media"` Authorization string `json:"track_authorization"` diff --git a/main.go b/main.go index db1ee28..4cd783e 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,11 @@ import ( ) func main() { - app := fiber.New() + app := fiber.New(fiber.Config{ + Prefork: cfg.Prefork, + JSONEncoder: cfg.JSON.Marshal, + JSONDecoder: cfg.JSON.Unmarshal, + }) app.Use(compress.New()) app.Use(recover.New()) app.Use(earlydata.New()) @@ -42,7 +46,7 @@ func main() { } c.Set("Content-Type", "text/html") - return templates.Base("tracks: "+q, templates.SearchTracks(p)).Render(context.Background(), c) + return templates.Base("tracks: "+q, templates.SearchTracks(p), nil).Render(context.Background(), c) case "users": p, err := sc.SearchUsers("?q=" + url.QueryEscape(q)) @@ -52,7 +56,7 @@ func main() { } c.Set("Content-Type", "text/html") - return templates.Base("users: "+q, templates.SearchUsers(p)).Render(context.Background(), c) + return templates.Base("users: "+q, templates.SearchUsers(p), nil).Render(context.Background(), c) case "playlists": p, err := sc.SearchPlaylists("?q=" + url.QueryEscape(q)) @@ -62,7 +66,7 @@ func main() { } c.Set("Content-Type", "text/html") - return templates.Base("playlists: "+q, templates.SearchPlaylists(p)).Render(context.Background(), c) + return templates.Base("playlists: "+q, templates.SearchPlaylists(p), nil).Render(context.Background(), c) } return c.SendStatus(404) @@ -81,7 +85,7 @@ func main() { } c.Set("Content-Type", "text/html") - return templates.Base(track.Title+" by "+track.Author.Username, templates.Track(track, stream)).Render(context.Background(), c) + return templates.Base(track.Title+" by "+track.Author.Username, templates.Track(track, stream), templates.TrackEmbed(track)).Render(context.Background(), c) }) app.Get("/:user", func(c *fiber.Ctx) error { @@ -102,7 +106,7 @@ func main() { //fmt.Println("gettracks", time.Since(h)) c.Set("Content-Type", "text/html") - return templates.Base(usr.Username, templates.User(usr, p)).Render(context.Background(), c) + return templates.Base(usr.Username, templates.User(usr, p), templates.UserEmbed(usr)).Render(context.Background(), c) }) app.Get("/:user/sets/:playlist", func(c *fiber.Ctx) error { @@ -113,7 +117,7 @@ func main() { } c.Set("Content-Type", "text/html") - return templates.Base(playlist.Title+" by "+playlist.Author.Username, templates.Playlist(playlist)).Render(context.Background(), c) + return templates.Base(playlist.Title+" by "+playlist.Author.Username, templates.Playlist(playlist), nil).Render(context.Background(), c) }) log.Fatal(app.Listen(cfg.Addr)) diff --git a/templates/base.templ b/templates/base.templ index 1f1792e..de83223 100644 --- a/templates/base.templ +++ b/templates/base.templ @@ -1,6 +1,6 @@ package templates -templ Base(title string, content templ.Component) { +templ Base(title string, content templ.Component, head templ.Component) { @@ -11,6 +11,10 @@ templ Base(title string, content templ.Component) { } else { soundcloak } + + if head != nil { + @head + } @content diff --git a/templates/track.templ b/templates/track.templ index 52cb624..9ee187c 100644 --- a/templates/track.templ +++ b/templates/track.templ @@ -2,9 +2,15 @@ package templates import "github.com/maid-zone/soundcloak/lib/sc" -templ Track(t sc.Track, stream string) { +templ TrackEmbed(t sc.Track) { + + + + - +} + +templ Track(t sc.Track, stream string) {

{t.Title}

diff --git a/templates/user.templ b/templates/user.templ index 62d4ed4..4ddebe8 100644 --- a/templates/user.templ +++ b/templates/user.templ @@ -7,6 +7,13 @@ import ( "net/url" ) +templ UserEmbed(u sc.User) { + + + + +} + templ User(u sc.User, p *sc.Paginated[sc.Track]) {
if u.Avatar != "" {