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

127 lines
3.1 KiB
Go

package extractor
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"safetwitch-backend/extractor/structs"
"github.com/tidwall/gjson"
)
const twitchUrl = "https://gql.twitch.tv/gql"
func call(url, method string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return req.Response, fmt.Errorf("Got error %s", err.Error())
}
req.Header.Add("Client-Id", "kimne78kx3ncx6brgo4mv6wki5h1ko")
resp, err := http.DefaultClient.Do(req)
return resp, err
}
type TwitchPayload = map[string]interface{}
func GetStreamerInfo(streamerName string) (structs.TwitchApiResponse, error) {
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",
},
},
},
}
json_data, err := json.Marshal(payload)
if err != nil {
fmt.Errorf("err")
}
resp, err := call(twitchUrl, "POST", bytes.NewBuffer(json_data))
if err != nil {
fmt.Errorf("y")
}
body, err := io.ReadAll(resp.Body)
var result []structs.TwitchApiResponse
err = json.Unmarshal(body, &result)
if err != nil {
log.Fatalln(err)
}
// begin parsing response
streamerFound := gjson.Get(string(body), "0.data.user")
if streamerFound.String() == "" {
return result[0], errors.New("streamer not found")
}
streamerData := gjson.Get(string(body), "0.data")
parsedSocials, err := ParseSocials(streamerData.String())
if err != nil {
return result[0], nil
}
log.Println(parsedSocials)
return result[0], nil
}