mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-21 20:53:00 -05:00
Consistency
This commit is contained in:
parent
6fd2993be0
commit
91dda1ceb5
1 changed files with 14 additions and 51 deletions
|
@ -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,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)
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
bodiesMutex.Lock()
|
||||
bodies = append(bodies, string(body))
|
||||
bodiesMutex.Unlock()
|
||||
}(payload)
|
||||
}
|
||||
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"))
|
||||
|
||||
|
@ -242,8 +207,6 @@ func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
|
|||
liveStreamers = append(liveStreamers, streamers[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return liveStreamers, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue