Switched to fiber's JSON encoder

This commit is contained in:
Shiny Nematoda
2023-04-02 16:53:29 +00:00
parent c52153c3c3
commit 01be66bd7f
9 changed files with 55 additions and 160 deletions
+8 -30
View File
@@ -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
}
+4 -16
View File
@@ -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
}
+4 -16
View File
@@ -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
}
+4 -18
View File
@@ -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
}
+4 -16
View File
@@ -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
}
+4 -16
View File
@@ -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
}
+10 -16
View File
@@ -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
}