0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 13:13:00 -05:00
safetwitch-backend/extractor/twitchExtractor.go

211 lines
5.3 KiB
Go
Raw Normal View History

2023-05-20 15:30:12 -05:00
package extractor
import (
"bytes"
"encoding/json"
"errors"
2023-05-20 15:30:12 -05:00
"io"
"net/http"
"safetwitch-backend/extractor/structs"
"github.com/tidwall/gjson"
2023-05-20 15:30:12 -05:00
)
const twitchUrl = "https://gql.twitch.tv/gql"
2023-05-26 09:20:03 -05:00
// useful funcs
2023-05-20 15:30:12 -05:00
func call(url, method string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return req.Response, err
2023-05-20 15:30:12 -05:00
}
req.Header.Add("Client-Id", "kimne78kx3ncx6brgo4mv6wki5h1ko")
resp, err := http.DefaultClient.Do(req)
return resp, err
}
2023-05-26 09:17:55 -05:00
func parseResponse(payload []TwitchPayload) (response *[]structs.TwitchApiResponse, body []byte, err error) {
json_data, err := json.Marshal(payload)
if err != nil {
return nil, nil, nil
}
resp, err := call(twitchUrl, "POST", bytes.NewBuffer(json_data))
if err != nil {
return nil, nil, nil
}
defer resp.Body.Close()
rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, nil
}
var twitchResponse []structs.TwitchApiResponse
err = json.Unmarshal(rawBody, &twitchResponse)
if err != nil {
return nil, nil, nil
}
return &twitchResponse, rawBody, nil
}
2023-05-20 15:30:12 -05:00
type TwitchPayload = map[string]interface{}
func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
2023-05-20 15:30:12 -05:00
payload := []TwitchPayload{
{
"operationName": "ChannelRoot_AboutPanel",
"variables": map[string]interface{}{
"channelLogin": streamerName,
"skipSchedule": false,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "6089531acef6c09ece01b440c41978f4c8dc60cb4fa0124c9a9d3f896709b6c6",
},
},
},
{
"operationName": "StreamMetadata",
"variables": map[string]interface{}{
"channelLogin": streamerName,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "a647c2a13599e5991e175155f798ca7f1ecddde73f7f341f39009c14dbf59962",
},
},
},
{
"operationName": "StreamTagsTrackingChannel",
"variables": map[string]interface{}{
"channel": streamerName,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "6aa3851aaaf88c320d514eb173563d430b28ed70fdaaf7eeef6ed4b812f48608",
},
},
},
{
"operationName": "VideoPreviewOverlay",
"variables": map[string]interface{}{
"login": streamerName,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "9515480dee68a77e667cb19de634739d33f243572b007e98e67184b1a5d8369f",
},
},
},
{
"operationName": "UseViewCount",
"variables": map[string]interface{}{
"channelLogin": streamerName,
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "00b11c9c428f79ae228f30080a06ffd8226a1f068d6f52fbc057cbde66e994c2",
},
},
},
}
2023-05-26 09:17:55 -05:00
_, body, err := parseResponse(payload)
2023-05-20 15:30:12 -05:00
if err != nil {
return structs.Streamer{}, err
2023-05-20 15:30:12 -05:00
}
// begin parsing response
streamerFound := gjson.Get(string(body), "0.data.user")
if streamerFound.String() == "" {
return structs.Streamer{}, errors.New("streamer not found")
}
streamerData := gjson.Get(string(body), "0.data")
parsedSocials, err := ParseSocials(streamerData.String())
if err != nil {
return structs.Streamer{}, err
}
parsedStream, err := ParseStream(string(body))
var isLive bool
if err != nil {
if err.Error() == "streamer is not live" {
parsedStream = nil
} else {
return structs.Streamer{}, err
}
}
if parsedStream != nil {
isLive = true
} else {
isLive = false
}
parsedStreamer := structs.Streamer{
Username: streamerData.Get("user.displayName").String(),
About: streamerData.Get("user.description").String(),
Pfp: ProxyUrl(streamerData.Get("user.profileImageURL").String()),
Followers: int(streamerData.Get("user.followers.totalCount").Int()),
Socials: parsedSocials,
IsLive: isLive,
IsPartner: streamerData.Get("user.isPartner").Bool(),
ColorHex: streamerData.Get("user.primaryColorHex").String(),
Id: streamerData.Get("user.id").String(),
Stream: parsedStream,
}
return parsedStreamer, nil
2023-05-20 15:30:12 -05:00
}
2023-05-26 09:17:55 -05:00
func GetDirectory(limit int, cursor string) ([]structs.Category, 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{},
},
},
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "1d1914ca3cbfaa607ecd5595b2e305e96acf987c8f25328f7713b25f604c4668",
},
},
},
}
_, body, err := parseResponse(payload)
if err != nil {
return []structs.Category{}, err
}
var parsedCategoryArray []structs.Category
categoryArray := gjson.Get(string(body), "0.data.directoriesWithTags.edges")
for _, categoryRes := range categoryArray.Array() {
parsedCategory, err := ParseCategory(categoryRes)
if err != nil {
return []structs.Category{}, nil
}
parsedCategoryArray = append(parsedCategoryArray, parsedCategory)
}
return parsedCategoryArray, nil
}