diff --git a/README.md b/README.md index 6ffbf6c..08774e1 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ https://codeberg.org/Hyperpipe/Hyperpipe - `:id` -> `MPLY*` ### `GET` `/channel/:id` -s + - `:id` -> `UC*` ### `GET` `/next/channel/:id/:params?ct=${click}&v=${visit}` diff --git a/lib/artist.go b/lib/artist.go index 758e6ea..3986817 100644 --- a/lib/artist.go +++ b/lib/artist.go @@ -1,8 +1,6 @@ package lib import ( - "encoding/json" - "codeberg.org/Hyperpipe/hyperpipe-backend/utils" "github.com/tidwall/gjson" ) @@ -51,7 +49,7 @@ func parseMoreButton(raw, v gjson.Result) MoreItem { } } -func parseArtist(raw string) (string, error) { +func parseArtist(raw string) Artist { j := gjson.Parse(raw) @@ -74,7 +72,7 @@ func parseArtist(raw string) (string, error) { "#(musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.text == Fans might also like).musicCarouselShelfRenderer", ) - val := Artist{ + return Artist{ Title: RunsText(h.Get("title")), Description: RunsText(h.Get("description")), SubscriberCount: RunsText( @@ -100,20 +98,13 @@ func parseArtist(raw string) (string, error) { Singles: parseMoreButton(m, j.Get("responseContext.visitorData")), }, } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func parseArtistNext(raw string) (string, error) { +func parseArtistNext(raw string) ArtistNext { j := gjson.Parse(raw) - val := ArtistNext{ + return ArtistNext{ Title: RunsText(j.Get("header.musicHeaderRenderer.title")), Items: TwoRowItemRenderer( j.Get( @@ -122,39 +113,26 @@ func parseArtistNext(raw string) (string, error) { true, ), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetArtist(id string) (string, int) { +func GetArtist(id string) (Artist, int) { context := utils.TypeBrowsePage(id, "artist") raw, status := utils.FetchBrowse(context) - res, err := parseArtist(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseArtist(raw) return res, status } -func GetArtistNext(id, params, ct, v string) (string, int) { +func GetArtistNext(id, params, ct, v string) (ArtistNext, int) { context := utils.TypeBrowse(id, params, []string{ct, v}) raw, status := utils.FetchBrowse(context) - res, err := parseArtistNext(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseArtistNext(raw) return res, status } diff --git a/lib/charts.go b/lib/charts.go index 9c16701..1f4545b 100644 --- a/lib/charts.go +++ b/lib/charts.go @@ -1,8 +1,6 @@ package lib import ( - "encoding/json" - "codeberg.org/Hyperpipe/hyperpipe-backend/utils" "github.com/tidwall/gjson" ) @@ -18,7 +16,7 @@ type Charts struct { Trending []Item `json:"trending"` } -func parseCharts(raw string) (string, error) { +func parseCharts(raw string) Charts { j := gjson.Parse(raw) @@ -43,7 +41,7 @@ func parseCharts(raw string) (string, error) { ) ref := j.Get("frameworkUpdates.entityBatchUpdate.mutations") - val := Charts{ + return Charts{ Options: Options{ Default: RunsText(o.Get("title")), All: MultiSelectMenuItemRenderer(opts, ref), @@ -51,25 +49,15 @@ func parseCharts(raw string) (string, error) { Artists: ResponsiveListItemRendererCH(a), Trending: ResponsiveListItemRenderer(t), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetCharts(params, code string) (string, int) { +func GetCharts(params, code string) (Charts, int) { context := utils.TypeBrowseForm("FEmusic_charts", params, code) raw, status := utils.FetchBrowse(context) - res, err := parseCharts(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseCharts(raw) return res, status } diff --git a/lib/explore.go b/lib/explore.go index 9b2a16c..4e1fe46 100644 --- a/lib/explore.go +++ b/lib/explore.go @@ -1,8 +1,6 @@ package lib import ( - "encoding/json" - "codeberg.org/Hyperpipe/hyperpipe-backend/utils" "github.com/tidwall/gjson" ) @@ -14,7 +12,7 @@ type Explore struct { Trending []Item `json:"trending"` } -func parseExplore(raw string) (string, error) { +func parseExplore(raw string) Explore { j := gjson.Parse(raw) @@ -33,7 +31,7 @@ func parseExplore(raw string) (string, error) { "#(gridRenderer).gridRenderer.items.#.musicNavigationButtonRenderer", ).Get("#(buttonText.runs.0.text == Charts)") - val := Explore{ + return Explore{ TrendingId: t.Get( "header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.navigationEndpoint.browseEndpoint.browseId", ).String(), @@ -41,25 +39,15 @@ func parseExplore(raw string) (string, error) { Albums: TwoRowItemRenderer(a.Get("contents"), true), Trending: ResponsiveListItemRenderer(t.Get("contents")), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetExplore() (string, int) { +func GetExplore() (Explore, int) { context := utils.TypeBrowse("FEmusic_explore", "", []string{}) raw, status := utils.FetchBrowse(context) - res, err := parseExplore(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseExplore(raw) return res, status } diff --git a/lib/genre.go b/lib/genre.go index e973f8f..5b6f930 100644 --- a/lib/genre.go +++ b/lib/genre.go @@ -1,8 +1,6 @@ package lib import ( - "encoding/json" - "codeberg.org/Hyperpipe/hyperpipe-backend/utils" "github.com/tidwall/gjson" ) @@ -15,7 +13,7 @@ type Genre struct { Shelf map[string]interface{} `json:"shelf"` } -func parseGenre(raw string) (string, error) { +func parseGenre(raw string) Genre { j := gjson.Parse(raw) c := j.Get( @@ -24,13 +22,11 @@ func parseGenre(raw string) (string, error) { g := c.Get("#.gridRenderer") - var val Genre - s := g.Get("#(header.gridHeaderRenderer.title.runs.0.text == Spotlight)") f := g.Get("#(header.gridHeaderRenderer.title.runs.0.text == Featured playlists)") cp := g.Get("#(header.gridHeaderRenderer.title.runs.0.text == Community playlists)") - val = Genre{ + return Genre{ Title: RunsText(j.Get("header.musicHeaderRenderer.title")), Spotlight: TwoRowItemRenderer(s.Get("items"), false), Featured: TwoRowItemRenderer(f.Get("items"), false), @@ -39,25 +35,15 @@ func parseGenre(raw string) (string, error) { c.Get("#.musicCarouselShelfRenderer"), ), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetGenre(param string) (string, int) { +func GetGenre(param string) (Genre, int) { context := utils.TypeBrowse("FEmusic_moods_and_genres_category", param, []string{}) raw, status := utils.FetchBrowse(context) - res, err := parseGenre(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseGenre(raw) return res, status } diff --git a/lib/genres.go b/lib/genres.go index d919ef0..94771f0 100644 --- a/lib/genres.go +++ b/lib/genres.go @@ -1,8 +1,6 @@ package lib import ( - "encoding/json" - "codeberg.org/Hyperpipe/hyperpipe-backend/utils" "github.com/tidwall/gjson" ) @@ -12,7 +10,7 @@ type Genres struct { Genres []Item `json:"genres"` } -func parseGenres(raw string) (string, error) { +func parseGenres(raw string) Genres { j := gjson.Parse(raw) @@ -25,29 +23,19 @@ func parseGenres(raw string) (string, error) { m := c.Get("#(header.gridHeaderRenderer.title.runs.0.text == Moods & moments)") g := c.Get("#(header.gridHeaderRenderer.title.runs.0.text == Genres)") - val := Genres{ + return Genres{ Moods: NavigationButton(m.Get("items")), Genres: NavigationButton(g.Get("items")), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetGenres() (string, int) { +func GetGenres() (Genres, int) { context := utils.TypeBrowse("FEmusic_moods_and_genres", "", []string{}) raw, status := utils.FetchBrowse(context) - res, err := parseGenres(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseGenres(raw) return res, status } diff --git a/lib/lyrics.go b/lib/lyrics.go index 6090811..e71d0db 100644 --- a/lib/lyrics.go +++ b/lib/lyrics.go @@ -1,8 +1,6 @@ package lib import ( - "encoding/json" - "codeberg.org/Hyperpipe/hyperpipe-backend/utils" "github.com/tidwall/gjson" ) @@ -12,7 +10,7 @@ type Lyrics struct { Source string `json:"source"` } -func parseLyrics(raw string) (string, error) { +func parseLyrics(raw string) Lyrics { j := gjson.Parse(raw) @@ -21,29 +19,19 @@ func parseLyrics(raw string) (string, error) { l := d.Get("description") s := d.Get("footer") - val := Lyrics{ + return Lyrics{ Text: RunsText(l), Source: RunsText(s), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetLyrics(id string) (string, int) { +func GetLyrics(id string) (Lyrics, int) { context := utils.TypeBrowsePage(id, "lyrics") raw, status := utils.FetchBrowse(context) - res, err := parseLyrics(raw) - if err != nil { - return utils.ErrMsg(err), 500 - } + res := parseLyrics(raw) return res, status } diff --git a/lib/next.go b/lib/next.go index 4281c63..93b440a 100644 --- a/lib/next.go +++ b/lib/next.go @@ -14,6 +14,7 @@ type MediaSession struct { } type Next struct { + Err error `json:"error"` LyricsId string `json:"lyricsId"` Songs []Item `json:"songs"` MediaSession MediaSession `json:"mediaSession"` @@ -52,7 +53,7 @@ func parseNextSongs(n gjson.Result) []Item { return r } -func parseNext(raw string) (string, error) { +func parseNext(raw string) Next { j := gjson.Parse(raw) @@ -71,7 +72,7 @@ func parseNext(raw string) (string, error) { "#(tabRenderer.title == Lyrics).tabRenderer.endpoint.browseEndpoint.browseId", ) - val := Next{ + return Next{ LyricsId: l.String(), MediaSession: MediaSession{ Album: RunsText(m.Get("album")), @@ -79,25 +80,18 @@ func parseNext(raw string) (string, error) { }, Songs: parseNextSongs(n.Get("contents")), } - - res, err := json.Marshal(val) - if err != nil { - return "", err - } - - return string(res), nil } -func GetNext(id string) (string, int) { +func GetNext(id string) (Next, int) { pldata, err := json.Marshal(utils.TypeNext(id, "")) if err != nil { - return utils.ErrMsg(err), 500 + return Next{Err: err}, 500 } plraw, plstatus, err := utils.Fetch("next", pldata) if err != nil || plstatus > 399 { - return utils.ErrMsg(err), plstatus + return Next{Err: err}, plstatus } pl := gjson.Parse(plraw).Get( @@ -110,15 +104,15 @@ func GetNext(id string) (string, int) { data, err := json.Marshal(utils.TypeNext(id, pl)) if err != nil { - return utils.ErrMsg(err), 500 + return Next{Err: err}, 500 } raw, status, err := utils.Fetch("next", data) - - res, err := parseNext(raw) if err != nil { - return utils.ErrMsg(err), 500 + return Next{Err: err}, 500 } + res := parseNext(raw) + return res, status } diff --git a/main.go b/main.go index e2b7bf8..bbcaa07 100644 --- a/main.go +++ b/main.go @@ -28,74 +28,60 @@ func HandleHealth(c *fiber.Ctx) error { func HandleExplore(c *fiber.Ctx) error { res, status := lib.GetExplore() - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleNext(c *fiber.Ctx) error { res, status := lib.GetNext(c.Params("id")) - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleGenres(c *fiber.Ctx) error { res, status := lib.GetGenres() - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleGenre(c *fiber.Ctx) error { res, status := lib.GetGenre(c.Params("id")) - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleCharts(c *fiber.Ctx) error { res, status := lib.GetCharts(c.Query("params"), c.Query("code")) - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleLyrics(c *fiber.Ctx) error { res, status := lib.GetLyrics(c.Params("id")) - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleArtist(c *fiber.Ctx) error { res, status := lib.GetArtist(c.Params("id")) - return c.Status(status).SendString(res) + return c.Status(status).JSON(res) } func HandleArtistNext(c *fiber.Ctx) error { + if c.Params("id") == "" || c.Params("params") == "" || + c.Query("ct") == "" || c.Query("v") == "" { + return c.Status(400).JSON(fiber.Map{ + "error": "invalid params", + }) + } + + res, status := lib.GetArtistNext(c.Params("id"), c.Params("params"), c.Query("ct"), c.Query("v")) - return c.Status(status).SendString(res) -} - -func HandleBrowse(c *fiber.Ctx) error { - - // Will be removed, Do not use - - id := c.Params("id") - - if len(id) < 4 { - return c.Status(500).SendString("{\"error\": \"browse id is too short\"}") - } - - switch { - case id[:2] == "UC": - res, status := lib.GetArtist(id) - return c.Status(status).SendString(res) - case id[:4] == "MPLY": - res, status := lib.GetLyrics(id) - return c.Status(status).SendString(res) - default: - return c.Status(500).SendString("{\"error\": \"URL not supported\"}") - } + return c.Status(status).JSON(res) } func main() { @@ -116,7 +102,6 @@ func main() { app.Get("/charts", HandleCharts) app.Get("/next/:id", HandleNext) app.Get("/lyrics/:id", HandleLyrics) - app.Get("/browse/:id", HandleBrowse) app.Get("/channel/:id", HandleArtist) app.Get("/next/channel/:id/:params", HandleArtistNext)