mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 13:13:00 -05:00
253 lines
6.2 KiB
Go
253 lines
6.2 KiB
Go
package twitch
|
|
|
|
import (
|
|
"errors"
|
|
"safetwitch-backend/extractor"
|
|
"safetwitch-backend/extractor/structs"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
func GetStreamerInfo(streamerName string) (structs.Streamer, error) {
|
|
payload := []TwitchPayload{
|
|
// Streamer data
|
|
{
|
|
"operationName": "ChannelRoot_AboutPanel",
|
|
"variables": map[string]interface{}{
|
|
"channelLogin": streamerName,
|
|
"skipSchedule": false,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "6089531acef6c09ece01b440c41978f4c8dc60cb4fa0124c9a9d3f896709b6c6",
|
|
},
|
|
},
|
|
},
|
|
|
|
// Stream metadata
|
|
{
|
|
"operationName": "StreamMetadata",
|
|
"variables": map[string]interface{}{
|
|
"channelLogin": streamerName,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "a647c2a13599e5991e175155f798ca7f1ecddde73f7f341f39009c14dbf59962",
|
|
},
|
|
},
|
|
},
|
|
|
|
// Stream tags
|
|
{
|
|
"operationName": "StreamTagsTrackingChannel",
|
|
"variables": map[string]interface{}{
|
|
"channel": streamerName,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "6aa3851aaaf88c320d514eb173563d430b28ed70fdaaf7eeef6ed4b812f48608",
|
|
},
|
|
},
|
|
},
|
|
|
|
// Stream preview image
|
|
{
|
|
"operationName": "VideoPreviewOverlay",
|
|
"variables": map[string]interface{}{
|
|
"login": streamerName,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "9515480dee68a77e667cb19de634739d33f243572b007e98e67184b1a5d8369f",
|
|
},
|
|
},
|
|
},
|
|
|
|
// Current views
|
|
{
|
|
"operationName": "UseViewCount",
|
|
"variables": map[string]interface{}{
|
|
"channelLogin": streamerName,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "00b11c9c428f79ae228f30080a06ffd8226a1f068d6f52fbc057cbde66e994c2",
|
|
},
|
|
},
|
|
},
|
|
|
|
// Get offline banner image
|
|
{
|
|
"operationName": "ChannelShell",
|
|
"variables": map[string]interface{}{
|
|
"login": streamerName,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "580ab410bcd0c1ad194224957ae2241e5d252b2c5173d8e0cce9d32d5bb14efe",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
_, body, err := parseResponse(payload)
|
|
if err != nil {
|
|
return structs.Streamer{}, err
|
|
}
|
|
|
|
// 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), 0)
|
|
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
|
|
}
|
|
|
|
streamerBanner := gjson.Get(string(body), "5.data.userOrError.bannerImageURL").String()
|
|
|
|
parsedStreamer, err := ParseStreamer(streamerData, isLive, parsedSocials, parsedStream, streamerName, streamerBanner)
|
|
if err != nil {
|
|
return structs.Streamer{}, err
|
|
}
|
|
|
|
return parsedStreamer, nil
|
|
}
|
|
|
|
func GetStreamerId(channelName string) (string, error) {
|
|
payload := []TwitchPayload{
|
|
{
|
|
"operationName": "ChannelRoot_AboutPanel",
|
|
"variables": map[string]interface{}{
|
|
"channelLogin": channelName,
|
|
"skipSchedule": true,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "6089531acef6c09ece01b440c41978f4c8dc60cb4fa0124c9a9d3f896709b6c6",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
_, body, err := parseResponse(payload)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
id := gjson.Get(string(body), "0.data.user.id").String()
|
|
|
|
if id != "" {
|
|
return id, nil
|
|
} else {
|
|
return "", errors.New("could not find user")
|
|
}
|
|
}
|
|
|
|
func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
|
|
|
|
if len(streamers) > 35 {
|
|
return []string{}, errors.New("no more than 35 streamers can be fetched at once")
|
|
}
|
|
|
|
var payload []TwitchPayload
|
|
for _, streamer := range streamers {
|
|
tmp := TwitchPayload{
|
|
"operationName": "UseLive",
|
|
"variables": map[string]interface{}{
|
|
"channelLogin": streamer,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "639d5f11bfb8bf3053b424d9ef650d04c4ebb7d94711d644afb08fe9a0fad5d9",
|
|
},
|
|
},
|
|
}
|
|
|
|
payload = append(payload, tmp)
|
|
}
|
|
|
|
_, body, err := parseResponse(payload)
|
|
if err != nil {
|
|
return []string{}, err
|
|
}
|
|
|
|
var liveStreamers []string
|
|
|
|
for i := 0; i < len(streamers); i++ {
|
|
stream := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data.user.stream"))
|
|
|
|
if stream.IsObject() {
|
|
liveStreamers = append(liveStreamers, streamers[i])
|
|
}
|
|
}
|
|
return liveStreamers, nil
|
|
}
|
|
|
|
func BulkFollowingUser(streamers []string) ([]structs.FollowingStreamer, error) {
|
|
|
|
if len(streamers) > 35 {
|
|
return []structs.FollowingStreamer{}, errors.New("no more than 35 streamers can be fetched at once")
|
|
}
|
|
|
|
var payload []TwitchPayload
|
|
for _, streamer := range streamers {
|
|
tmp := TwitchPayload{
|
|
"operationName": "ChannelRoot_AboutPanel",
|
|
"variables": map[string]interface{}{
|
|
"channelLogin": streamer,
|
|
"skipSchedule": false,
|
|
},
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "6089531acef6c09ece01b440c41978f4c8dc60cb4fa0124c9a9d3f896709b6c6",
|
|
},
|
|
},
|
|
}
|
|
|
|
payload = append(payload, tmp)
|
|
}
|
|
|
|
_, body, err := parseResponse(payload)
|
|
if err != nil {
|
|
return []structs.FollowingStreamer{}, err
|
|
}
|
|
|
|
var parsedFollowingStreamers []structs.FollowingStreamer
|
|
|
|
// parse the data from all of the segments before
|
|
for i := 0; i < len(streamers); i++ {
|
|
data := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data"))
|
|
parsed := ParseFollowingStreamer(data, streamers[i])
|
|
parsedFollowingStreamers = append(parsedFollowingStreamers, parsed)
|
|
}
|
|
|
|
return parsedFollowingStreamers, nil
|
|
}
|