0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 05:02:58 -05:00

Consistency

This commit is contained in:
dragongoose 2024-02-06 12:02:50 -05:00
parent 6fd2993be0
commit 91dda1ceb5
No known key found for this signature in database
GPG key ID: 01397EEC371CDAA5

View file

@ -2,10 +2,8 @@ package twitch
import (
"errors"
"fmt"
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/structs"
"sync"
"github.com/tidwall/gjson"
)
@ -172,20 +170,13 @@ func GetStreamerId(channelName string) (string, error) {
}
func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
// We can only make 35 different queries in the same request
// or else it throws 400 error :( So here, payload will be
// an array of an array, which we will make requests in
// 35 segments
var payloads [][]TwitchPayload
var tmparr []TwitchPayload
for _, streamer := range streamers {
if len(tmparr) > 34 {
// add full tmparr to payloads
payloads = append(payloads, tmparr)
// empty tmp arr
tmparr = []TwitchPayload{}
}
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{}{
@ -199,51 +190,23 @@ func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
},
}
tmparr = append(tmparr, tmp)
}
if len(tmparr) > 0 {
payloads = append(payloads, tmparr)
payload = append(payload, tmp)
}
bodies := []string{}
bodiesMutex := sync.Mutex{}
wg := sync.WaitGroup{}
ch := make(chan error, len(payloads))
for _, payload := range payloads {
wg.Add(1)
go func(payload []TwitchPayload) {
defer wg.Done()
_, body, err := parseResponse(payload)
if err != nil {
ch <- err
return
}
bodiesMutex.Lock()
bodies = append(bodies, string(body))
bodiesMutex.Unlock()
}(payload)
_, body, err := parseResponse(payload)
if err != nil {
return []string{}, err
}
wg.Wait()
fmt.Println(bodies)
var liveStreamers []string
// parse the data from all of the segments before
for _, body := range bodies {
fmt.Println(body)
for i := 0; i < len(streamers); i++ {
stream := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data.user.stream"))
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])
}
if stream.IsObject() {
liveStreamers = append(liveStreamers, streamers[i])
}
}
return liveStreamers, nil
}