mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 05:02:58 -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 (
|
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
|
|
||||||
// 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{
|
tmp := TwitchPayload{
|
||||||
"operationName": "UseLive",
|
"operationName": "UseLive",
|
||||||
"variables": map[string]interface{}{
|
"variables": map[string]interface{}{
|
||||||
|
@ -199,51 +190,23 @@ func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
tmparr = append(tmparr, tmp)
|
payload = append(payload, tmp)
|
||||||
}
|
|
||||||
if len(tmparr) > 0 {
|
|
||||||
payloads = append(payloads, tmparr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bodies := []string{}
|
_, body, err := parseResponse(payload)
|
||||||
bodiesMutex := sync.Mutex{}
|
if err != nil {
|
||||||
wg := sync.WaitGroup{}
|
return []string{}, err
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
wg.Wait()
|
|
||||||
fmt.Println(bodies)
|
|
||||||
|
|
||||||
var liveStreamers []string
|
var liveStreamers []string
|
||||||
|
|
||||||
// parse the data from all of the segments before
|
for i := 0; i < len(streamers); i++ {
|
||||||
for _, body := range bodies {
|
stream := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data.user.stream"))
|
||||||
fmt.Println(body)
|
|
||||||
for i := 0; i < len(streamers); i++ {
|
|
||||||
stream := gjson.Get(string(body), extractor.GenGjsonQuery(i, ".data.user.stream"))
|
|
||||||
|
|
||||||
if stream.IsObject() {
|
if stream.IsObject() {
|
||||||
liveStreamers = append(liveStreamers, streamers[i])
|
liveStreamers = append(liveStreamers, streamers[i])
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return liveStreamers, nil
|
return liveStreamers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue