From d2c8e0e7a825e2214ab965a5b02c2408d6b973f3 Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Sun, 6 Aug 2023 10:51:29 +0000 Subject: [PATCH] re-add album canonical url --- go.mod | 2 +- go.sum | 4 ++-- lib/album.go | 30 ++++++++++++++++++++++++++++++ main.go | 6 ++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 lib/album.go diff --git a/go.mod b/go.mod index 7048fc9..69a88f0 100644 --- a/go.mod +++ b/go.mod @@ -20,5 +20,5 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.48.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect - golang.org/x/sys v0.10.0 // indirect + golang.org/x/sys v0.11.0 // indirect ) diff --git a/go.sum b/go.sum index f3fa7a0..4392220 100644 --- a/go.sum +++ b/go.sum @@ -31,5 +31,5 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/lib/album.go b/lib/album.go new file mode 100644 index 0000000..49c4935 --- /dev/null +++ b/lib/album.go @@ -0,0 +1,30 @@ +package lib + +import ( + "net/url" + + "codeberg.org/Hyperpipe/hyperpipe-backend/utils" + "github.com/tidwall/gjson" +) + +func GetAlbumUrl(id string) (map[string]string, int) { + + ctx := utils.TypeBrowsePage(id, "album") + raw, status := utils.FetchBrowse(ctx) + + uri := gjson.Parse(raw).Get( + "microformat.microformatDataRenderer.urlCanonical", + ).String() + + u, err := url.Parse(uri) + if err != nil { + return map[string]string{ + "error": "500", + "message": "failed to parse url", + }, 500 + } + + return map[string]string{ + "id": u.Query().Get("list"), + }, status +} diff --git a/main.go b/main.go index 6ffe83b..b6193ee 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,11 @@ func HandleLyrics(c *fiber.Ctx) error { return c.Status(status).JSON(res) } +func HandleAlbum(c *fiber.Ctx) error { + res, status := lib.GetAlbumUrl(c.Params("id")) + return c.Status(status).JSON(res) +} + func HandleArtist(c *fiber.Ctx) error { res, status := lib.GetArtist(c.Params("id")) @@ -112,6 +117,7 @@ func main() { app.Get("/charts", HandleCharts) app.Get("/next/:id", HandleNext) app.Get("/lyrics/:id", HandleLyrics) + app.Get("/album/:id", HandleAlbum) app.Get("/channel/:id", HandleArtist) app.Get("/next/channel/:id/:params", HandleArtistNext)