mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 21:23:01 -05:00
130 lines
3.5 KiB
Go
130 lines
3.5 KiB
Go
package twitch
|
|
|
|
import (
|
|
"safetwitch-backend/extractor"
|
|
"safetwitch-backend/extractor/structs"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
func GetDiscoveryPage(limit int, cursor string) ([]structs.CategoryPreview, error) {
|
|
payload := []TwitchPayload{
|
|
{
|
|
"operationName": "BrowsePage_AllDirectories",
|
|
"variables": map[string]interface{}{
|
|
"limit": limit,
|
|
"options": map[string]interface{}{
|
|
"recommendationsContext": map[string]interface{}{
|
|
"platform": "web",
|
|
},
|
|
"requestID": "JIRA-VXP-2397",
|
|
"sort": "RELEVANCE",
|
|
"tags": []string{},
|
|
},
|
|
"cursor": cursor,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "1d1914ca3cbfaa607ecd5595b2e305e96acf987c8f25328f7713b25f604c4668",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
_, body, err := parseResponse(payload)
|
|
if err != nil {
|
|
return []structs.CategoryPreview{}, err
|
|
}
|
|
|
|
var parsedCategoryArray []structs.CategoryPreview
|
|
categoryArray := gjson.Get(string(body), "0.data.directoriesWithTags.edges")
|
|
for _, categoryRes := range categoryArray.Array() {
|
|
parsedCategory, err := ParseCategory(categoryRes)
|
|
if err != nil {
|
|
return []structs.CategoryPreview{}, nil
|
|
}
|
|
|
|
parsedCategoryArray = append(parsedCategoryArray, parsedCategory)
|
|
}
|
|
|
|
return parsedCategoryArray, nil
|
|
}
|
|
|
|
func GetDiscoveryItem(name string, streamLimit int, cursor string) (structs.CategoryData, error) {
|
|
payload := []TwitchPayload{
|
|
{
|
|
"operationName": "DirectoryPage_Game",
|
|
"variables": map[string]interface{}{
|
|
"cursor": cursor,
|
|
"imageWidth": 50,
|
|
"name": name,
|
|
"options": map[string]interface{}{
|
|
"sort": "RELEVANCE",
|
|
"recommendationsContext": map[string]interface{}{
|
|
"platform": "web",
|
|
},
|
|
"requestID": "JIRA-VXP-2397",
|
|
"freeformTags": nil,
|
|
"tags": []string{},
|
|
},
|
|
"sortTypeIsRecency": false,
|
|
"limit": streamLimit,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "df4bb6cc45055237bfaf3ead608bbafb79815c7100b6ee126719fac3762ddf8b",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"operationName": "Directory_DirectoryBanner",
|
|
"variables": map[string]interface{}{
|
|
"name": name,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "2670fbecd8fbea0211c56528d6eff5752ef9d6c73cd5238d395784b46335ded4",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
_, body, err := parseResponse(payload)
|
|
if err != nil {
|
|
return structs.CategoryData{}, err
|
|
}
|
|
|
|
categoryStreams := gjson.Get(string(body), "0.data.game.streams.edges")
|
|
|
|
var parsedStreams []structs.CategoryMinifiedStream
|
|
for _, stream := range categoryStreams.Array() {
|
|
parsed, err := ParseMinifiedStream(stream)
|
|
if err != nil {
|
|
return structs.CategoryData{}, err
|
|
}
|
|
parsedStreams = append(parsedStreams, parsed)
|
|
}
|
|
|
|
categoryData := gjson.Get(string(body), "1.data.game")
|
|
|
|
var tags []string
|
|
for _, tag := range categoryData.Get("tags").Array() {
|
|
tags = append(tags, tag.Get("localizedName").String())
|
|
}
|
|
|
|
parsedCategory := structs.CategoryData{
|
|
Name: categoryData.Get("name").String(),
|
|
DisplayName: categoryData.Get("displayName").String(),
|
|
Description: categoryData.Get("description").String(),
|
|
Viewers: int(categoryData.Get("viewersCount").Int()),
|
|
Followers: int(categoryData.Get("followersCount").Int()),
|
|
Tags: tags,
|
|
Cover: extractor.ProxyUrl(categoryData.Get("avatarURL").String()),
|
|
Streams: parsedStreams,
|
|
}
|
|
|
|
return parsedCategory, err
|
|
}
|