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 ( import (
"errors" "errors"
"fmt"
"safetwitch-backend/extractor" "safetwitch-backend/extractor"
"safetwitch-backend/extractor/structs" "safetwitch-backend/extractor/structs"
"sync"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -172,20 +170,13 @@ func GetStreamerId(channelName string) (string, error) {
} }
func BulkCheckIfStreamerIsLive(streamers []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 if len(streamers) > 35 {
// an array of an array, which we will make requests in return []string{}, errors.New("no more than 35 streamers can be fetched at once")
// 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{}
} }
var payload []TwitchPayload
for _, streamer := range streamers {
tmp := TwitchPayload{ tmp := TwitchPayload{
"operationName": "UseLive", "operationName": "UseLive",
"variables": map[string]interface{}{ "variables": map[string]interface{}{
@ -199,42 +190,16 @@ func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
}, },
} }
tmparr = append(tmparr, tmp) payload = append(payload, tmp)
} }
if len(tmparr) > 0 {
payloads = append(payloads, tmparr)
}
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) _, body, err := parseResponse(payload)
if err != nil { if err != nil {
ch <- err return []string{}, err
return
} }
bodiesMutex.Lock()
bodies = append(bodies, string(body))
bodiesMutex.Unlock()
}(payload)
}
wg.Wait()
fmt.Println(bodies)
var liveStreamers []string 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++ { for i := 0; i < len(streamers); i++ {
stream := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data.user.stream")) stream := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data.user.stream"))
@ -242,8 +207,6 @@ func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
liveStreamers = append(liveStreamers, streamers[i]) liveStreamers = append(liveStreamers, streamers[i])
} }
} }
}
return liveStreamers, nil return liveStreamers, nil
} }