diff --git a/fetch.go b/fetch.go index bd4e758..b055671 100644 --- a/fetch.go +++ b/fetch.go @@ -10,7 +10,7 @@ import ( func Fetch(path string, data []byte) (string, int, error) { - url := "https://music.youtube.com/youtubei/v1/" + path + "?key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30&prettyPrint=false" + url := "https://music.youtube.com/youtubei/v1/" + path + "?key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30" req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { @@ -41,9 +41,9 @@ func Fetch(path string, data []byte) (string, int, error) { return string(body), resp.StatusCode, nil } -func FetchArtist(id string) (string, int) { +func FetchBrowse(id string, browse BrowseData) (string, int) { - data, err := json.Marshal(GetTypeBrowse("artist", id)) + data, err := json.Marshal(browse) if err != nil { return ErrorMessage(err), 500 } @@ -51,10 +51,18 @@ func FetchArtist(id string) (string, int) { raw, status, err := Fetch("browse", data) if err != nil { return ErrorMessage(err), 500 - } else if status > 300 { - return raw, status } + return raw, status + +} + +func FetchArtist(id string) (string, int) { + + browse := GetTypeBrowse("artist", id) + + raw, status := FetchBrowse(id, browse) + res, err := ParseArtist(raw) if err != nil { return ErrorMessage(err), 500 @@ -63,6 +71,20 @@ func FetchArtist(id string) (string, int) { return res, status } +func FetchLyrics(id string) (string, int) { + + browse := GetTypeBrowse("lyrics", id) + + raw, status := FetchBrowse(id, browse) + + res, err := ParseLyrics(raw) + if err != nil { + return ErrorMessage(err), 500 + } + + return res, status +} + func FetchNext(id string) (string, int) { data, err := json.Marshal(GetTypeNext(id)) @@ -73,19 +95,12 @@ func FetchNext(id string) (string, int) { raw, status, err := Fetch("next", data) if err != nil { return ErrorMessage(err), 500 - } else if status > 300 { - return raw, status } res, err := ParseNext(raw) + if err != nil { + return ErrorMessage(err), 500 + } return res, status -} - -/*func main() { - - res, status := FetchNext("KWLGyeg74es") - - log.Println(status) - log.Println(res) -}*/ +} \ No newline at end of file diff --git a/main.go b/main.go index 5d1de8d..90eaa4f 100644 --- a/main.go +++ b/main.go @@ -29,20 +29,25 @@ func HandleNext(c *fiber.Ctx) error { return c.Status(status).SendString(res) } + func HandleBrowse(c *fiber.Ctx) error { defer calc()() id := c.Params("id") - switch id[:2] { - case "UC": + switch { + case id[:2] == "UC": res, status := FetchArtist(id) return c.Status(status).SendString(res) + case id[:4] == "MPLY": + res, status := FetchLyrics(id) + return c.Status(status).SendString(res) default: return c.SendString("{error: \"Invalid URL\"}") } } + func HandleArtist(c *fiber.Ctx) error { defer calc()() res, status := FetchArtist(c.Params("id")) diff --git a/parser.go b/parser.go index c2ea764..07de80d 100644 --- a/parser.go +++ b/parser.go @@ -201,17 +201,43 @@ func ParseArtist(raw string) (string, error) { return string(res), nil } +func ParseLyrics(raw string) (string, error) { + + j := gjson.Parse(raw) + + d := j.Get("contents.sectionListRenderer.contents.0.musicDescriptionShelfRenderer") + + l := d.Get("description") + s := d.Get("footer") + + val := Lyrics{ + Text: RunsText(l), + Source: RunsText(s), + } + + res, err := json.Marshal(val) + if err != nil { + return "", err + } + + return string(res), nil +} + func ParseNext(raw string) (string, error) { j := gjson.Parse(raw) + c := j.Get("contents.singleColumnMusicWatchNextResultsRenderer.tabbedRenderer.watchNextTabbedResultsRenderer.tabs") m := j.Get("playerOverlays.playerOverlayRenderer.browserMediaSession.browserMediaSessionRenderer") - n := j.Get("contents.singleColumnMusicWatchNextResultsRenderer.tabbedRenderer.watchNextTabbedResultsRenderer.tabs.#(tabRenderer.title == Up next).tabRenderer.content.musicQueueRenderer.content.playlistPanelRenderer") + n := c.Get("#(tabRenderer.title == Up next).tabRenderer.content.musicQueueRenderer.content.playlistPanelRenderer") + l := c.Get("#(tabRenderer.title == Lyrics).tabRenderer.endpoint.browseEndpoint.browseId") val := Next{ + LyricsId: l.String(), MediaSession: MediaSession{ Album: RunsText(m.Get("album")), Thumbnails: GetThumbnails(m.Get("thumbnailDetails.thumbnails")), + }, Songs: GetNextSongs(n.Get("contents")), } diff --git a/structs.go b/structs.go index 635c9aa..81be638 100644 --- a/structs.go +++ b/structs.go @@ -37,10 +37,16 @@ type MediaSession struct { } type Next struct { + LyricsId string `json:"lyricsId"` Songs []Item `json:"songs"` MediaSession MediaSession `json:"mediaSession"` } +type Lyrics struct { + Text string `json:"text"` + Source string `json:"source"` +} + /* Structs and Types */ type Client struct { Name string `json:"clientName"`