- Proper structure for code
- Support for musicCarouselShelfRenderer in genres
This commit is contained in:
Shiny Nematoda
2023-03-16 17:26:12 +00:00
parent aad917393d
commit b208d87309
20 changed files with 1124 additions and 994 deletions
+59
View File
@@ -0,0 +1,59 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func ErrMsg(err error) string {
return fmt.Sprintf("{\"error\":true,\"message\":\"%s\"}", err)
}
func Fetch(path string, data []byte) (string, int, error) {
url := "https://music.youtube.com/youtubei/v1/" + path + "?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30&prettyPrint=false"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return "", 500, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Origin", "https://music.youtube.com")
req.Header.Set("x-origin", "https://music.youtube.com")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", 500, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", 500, err
}
return string(body), resp.StatusCode, nil
}
func FetchBrowse(browse BrowseData) (string, int) {
data, err := json.Marshal(browse)
if err != nil {
return ErrMsg(err), 500
}
raw, status, err := Fetch("browse", data)
if err != nil {
return ErrMsg(err), 500
}
return raw, status
}
+100
View File
@@ -0,0 +1,100 @@
package utils
import "strings"
type Client struct {
Name string `json:"clientName,omitempty"`
Version string `json:"clientVersion,omitempty"`
Hl string `json:"hl",omitempty`
}
type Context struct {
Client Client `json:"client",omitempty`
}
type PageType struct {
PageType string `json:"pageType,omitempty"`
}
type BrowseMusicConfig struct {
MusicConfig PageType `json:"browseEndpointContextMusicConfig,omitempty"`
}
type Form struct {
Values []string `json:"selectedValues"`
}
type WatchMusicConfig struct {
Panel bool `json:"hasPersistentPlaylistPanel,omitempty"`
Type string `json:"musicVideoType,omitempty"`
}
type BrowseData struct {
Context Context `json:"context,omitempty"`
MusicConfig BrowseMusicConfig `json:"browseEndpointContextMusicConfig,omitempty"`
BrowseId string `json:"browseId,omitempty"`
Params string `json:"params,omitempty"`
Form Form `json:"formData,omitempty"`
}
type NextData struct {
Id string `json:"videoId,omitempty"`
PlaylistId string `json:"playlistId",omitempty`
Context Context `json:"context,omitempty"`
Audio bool `json:"isAudioOnly,omitempty"`
Tuner string `json:"tunerSettingValue,omitempty"`
Panel bool `json:"enablePersistentPlaylistPanel,omitempty"`
MusicConfig WatchMusicConfig `json:"watchEndpointMusicConfig,omitempty"`
}
var BaseContext Context = Context{
Client: Client{
Name: "WEB_REMIX",
Version: "1.20220926.01.00",
Hl: "en-US",
},
}
func TypeBrowse(t, id, params, form string) BrowseData {
if t != "" {
return BrowseData{
Context: BaseContext,
MusicConfig: BrowseMusicConfig{
MusicConfig: PageType{
PageType: "MUSIC_PAGE_TYPE_" + strings.ToUpper(t),
},
},
BrowseId: id,
}
} else if form != "" {
return BrowseData{
Context: BaseContext,
BrowseId: id,
Params: params,
Form: Form{
Values: []string{form},
},
}
} else {
return BrowseData{
Context: BaseContext,
BrowseId: id,
Params: params,
}
}
}
func TypeNext(id, pid string) NextData {
return NextData{
Id: id,
PlaylistId: pid,
Context: BaseContext,
Panel: true,
Audio: true,
Tuner: "AUTOMIX_SETTING_NORMAL",
MusicConfig: WatchMusicConfig{
Panel: true,
Type: "MUSIC_VIDEO_TYPE_ATV",
},
}
}