From 3b26dd2f07f96b146bbf0561be58713db303a351 Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Sat, 1 Oct 2022 10:34:03 +0000 Subject: [PATCH] Added Support for Charts --- builder.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch.go | 30 +++++++++++++++++++------ main.go | 9 ++++++++ parser.go | 38 +++++++++++++++++++++++++++++-- structs.go | 16 ++++++++++++-- types.go | 4 ++-- 6 files changed, 149 insertions(+), 13 deletions(-) diff --git a/builder.go b/builder.go index 8812c4e..0c22404 100644 --- a/builder.go +++ b/builder.go @@ -135,6 +135,42 @@ func ResponsiveListItemRenderer(s gjson.Result) []Item { return r } +func ResponsiveListItemRendererCH(s gjson.Result) []Item { + + r := []Item{} + + wg := sync.WaitGroup{} + + s.ForEach( + func(_, v gjson.Result) bool { + + wg.Add(1) + + go func() { + defer wg.Done() + + j := v.Get("musicResponsiveListItemRenderer") + flex := j.Get("flexColumns.#.musicResponsiveListItemFlexColumnRenderer" + + ".text.runs.0.text") + + r = append(r, Item{ + Id: j.Get("navigationEndpoint.browseEndpoint.browseId").String(), + Title: flex.Get("0").String() + " • " + flex.Get("1").String(), + Thumbnails: GetThumbnails(j.Get("thumbnail.musicThumbnailRenderer" + + ".thumbnail.thumbnails")), + }) + + }() + + wg.Wait() + + return true + }, + ) + + return r +} + func NavigationButton(s gjson.Result) []Item { r := []Item{} @@ -168,3 +204,32 @@ func NavigationButton(s gjson.Result) []Item { return r } + +func MultiSelectMenuItemRenderer(j gjson.Result) []Item { + + r := []Item{} + + wg := sync.WaitGroup{} + + j.ForEach( + func(_, v gjson.Result) bool { + + wg.Add(1) + + go func() { + defer wg.Done() + + r = append(r, Item{ + Id: v.Get("selectedCommand.commandExecutorCommand.commands.#(musicBrowseFormBinderCommand).musicBrowseFormBinderCommand.browseEndpoint.params").String(), + Title: RunsText(v.Get("title")), + }) + }() + + wg.Wait() + + return true + }, + ) + + return r +} diff --git a/fetch.go b/fetch.go index fe1c1dc..1633ebe 100644 --- a/fetch.go +++ b/fetch.go @@ -44,7 +44,7 @@ func Fetch(path string, data []byte) (string, int, error) { return string(body), resp.StatusCode, nil } -func FetchBrowse(id string, browse BrowseData) (string, int) { +func FetchBrowse(browse BrowseData) (string, int) { data, err := json.Marshal(browse) if err != nil { @@ -66,7 +66,7 @@ func FetchExplore() (string, int) { context := GetTypeBrowse("", id, "") - raw, status := FetchBrowse(id, context) + raw, status := FetchBrowse(context) res, err := ParseExplore(raw) if err != nil { @@ -82,7 +82,7 @@ func FetchGenres() (string, int) { context := GetTypeBrowse("", id, "") - raw, status := FetchBrowse(id, context) + raw, status := FetchBrowse(context) res, err := ParseGenres(raw) if err != nil { @@ -98,7 +98,7 @@ func FetchGenre(param string) (string, int) { context := GetTypeBrowse("", id, param) - raw, status := FetchBrowse(id, context) + raw, status := FetchBrowse(context) res, err := ParseGenre(raw) if err != nil { @@ -108,11 +108,27 @@ func FetchGenre(param string) (string, int) { return res, status } +func FetchCharts(params string) (string, int) { + + id := "FEmusic_charts" + + context := GetTypeBrowse("", id, params) + + raw, status := FetchBrowse(context) + + res, err := ParseCharts(raw) + if err != nil { + return ErrorMessage(err), 500 + } + + return res, status +} + func FetchArtist(id string) (string, int) { context := GetTypeBrowse("artist", id, "") - raw, status := FetchBrowse(id, context) + raw, status := FetchBrowse(context) res, err := ParseArtist(raw) if err != nil { @@ -126,7 +142,7 @@ func FetchLyrics(id string) (string, int) { context := GetTypeBrowse("lyrics", id, "") - raw, status := FetchBrowse(id, context) + raw, status := FetchBrowse(context) res, err := ParseLyrics(raw) if err != nil { @@ -140,7 +156,7 @@ func FetchAlbum(id string) (string, int) { context := GetTypeBrowse("album", id, "") - raw, status := FetchBrowse(id, context) + raw, status := FetchBrowse(context) url := gjson.Parse(raw).Get("microformat.microformatDataRenderer.urlCanonical").String() diff --git a/main.go b/main.go index 50e50da..8ee5249 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,14 @@ func HandleGenre(c *fiber.Ctx) error { return c.Status(status).SendString(res) } +func HandleCharts(c *fiber.Ctx) error { + defer calc()() + + res, status := FetchCharts(c.Params("id")) + + return c.Status(status).SendString(res) +} + func HandleBrowse(c *fiber.Ctx) error { defer calc()() @@ -98,6 +106,7 @@ func main() { app.Get("/explore", HandleExplore) app.Get("/genres", HandleGenres) app.Get("/genres/:id", HandleGenre) + app.Get("/charts/:id", HandleCharts) app.Get("/next/:id", HandleNext) app.Get("/browse/:id", HandleBrowse) app.Get("/channel/:id", HandleArtist) diff --git a/parser.go b/parser.go index 5f10fbe..5c8f231 100644 --- a/parser.go +++ b/parser.go @@ -42,17 +42,24 @@ func ParseExplore(raw string) (string, error) { j := gjson.Parse(raw) - c := j.Get("contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content" + - ".sectionListRenderer.contents.#.musicCarouselShelfRenderer") + m := j.Get("contents.singleColumnBrowseResultsRenderer.tabs.0" + + ".tabRenderer.content.sectionListRenderer.contents") + + c := m.Get("#.musicCarouselShelfRenderer") a := c.Get("#(header.musicCarouselShelfBasicHeaderRenderer" + ".title.runs.0.text == New albums & singles)") t := c.Get("#(header.musicCarouselShelfBasicHeaderRenderer" + ".title.runs.0.text == Trending)") + charts := m.Get("#(gridRenderer).gridRenderer.items.#" + + ".musicNavigationButtonRenderer").Get("#(buttonText" + + ".runs.0.text == Charts)") + val := Explore{ TrendingId: t.Get("header.musicCarouselShelfBasicHeaderRenderer" + ".title.runs.0.navigationEndpoint.browseEndpoint.browseId").String(), + ChartsId: charts.Get("clickCommand.browseEndpoint.params").String(), Albums: TwoRowItemRenderer("album", a.Get("contents")), Trending: ResponsiveListItemRenderer(t.Get("contents")), } @@ -114,6 +121,33 @@ func ParseGenre(raw string) (string, error) { return string(res), nil } +func ParseCharts(raw string) (string, error) { + + j := gjson.Parse(raw) + + c := j.Get("contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents") + + o := c.Get("0.musicShelfRenderer.subheaders.0.musicSideAlignedItemRenderer.startItems.0.musicSortFilterButtonRenderer") + a := c.Get("#(musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.text == Top artists).musicCarouselShelfRenderer.contents") + t := c.Get("#(musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.text == Trending).musicCarouselShelfRenderer.contents") + + val := Charts{ + Options: Options{ + Default: RunsText(o.Get("title")), + All: MultiSelectMenuItemRenderer(o.Get("menu.musicMultiSelectMenuRenderer.options.#.musicMultiSelectMenuItemRenderer")), + }, + Artists: ResponsiveListItemRendererCH(a), + Trending: ResponsiveListItemRenderer(t), + } + + res, err := json.Marshal(val) + if err != nil { + return "", err + } + + return string(res), nil +} + func ParseArtist(raw string) (string, error) { j := gjson.Parse(raw) diff --git a/structs.go b/structs.go index f6660c7..99d7232 100644 --- a/structs.go +++ b/structs.go @@ -1,6 +1,6 @@ package main -/* Parsers */ +// Parsers type Thumbnail struct { Height int64 `json:"height"` Width int64 `json:"width"` @@ -24,6 +24,7 @@ type Items struct { type Explore struct { TrendingId string `json:"trendingId"` + ChartsId string `json:"chartsId"` Albums []Item `json:"albums_and_singles"` Trending []Item `json:"trending"` } @@ -40,6 +41,17 @@ type Genre struct { Community []Item `json:"community"` } +type Options struct { + Default string `json:"default"` + All []Item `json:"all"` +} + +type Charts struct { + Options Options `json:"options"` + Artists []Item `json:"artists"` + Trending []Item `json:"trending"` +} + type Artist struct { Title string `json:"title"` Description string `json:"description,omitempty"` @@ -66,7 +78,7 @@ type Lyrics struct { Source string `json:"source"` } -/* Structs and Types */ +// Structs and Types type Client struct { Name string `json:"clientName,omitempty"` Version string `json:"clientVersion,omitempty"` diff --git a/types.go b/types.go index cc57240..bda7d02 100644 --- a/types.go +++ b/types.go @@ -2,10 +2,10 @@ package main import "strings" -var BaseContext = Context{ +var BaseContext Context = Context{ Client: Client{ Name: "WEB_REMIX", - Version: "1.20220803.01.00", + Version: "1.20220926.01.00", Hl: "en-US", }, }