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

Improve following streamer data fetching

This commit is contained in:
dragongoose 2024-02-06 12:02:28 -05:00
parent f90cae54c3
commit 6fd2993be0
No known key found for this signature in database
GPG key ID: 01397EEC371CDAA5
4 changed files with 74 additions and 0 deletions

View file

@ -144,3 +144,10 @@ type StreamMessage struct {
Color string `json:"color"`
Badges []StreamMessageBadge `json:"badges"`
}
type FollowingStreamer struct {
Username string `json:"username"`
Login string `json:"login"`
Pfp string `json:"pfp"`
Followers int `json:"followers"`
}

View file

@ -246,3 +246,45 @@ func BulkCheckIfStreamerIsLive(streamers []string) ([]string, error) {
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
}

View file

@ -326,3 +326,12 @@ func ParseVODMessage(data gjson.Result) structs.VodComment {
}
}
func ParseFollowingStreamer(data gjson.Result, login string) structs.FollowingStreamer {
return structs.FollowingStreamer{
Username: data.Get("user.displayName").String(),
Login: login,
Pfp: extractor.ProxyUrl(data.Get("user.profileImageURL").String()),
Followers: int(data.Get("user.followers.totalCount").Int()),
}
}

View file

@ -47,4 +47,20 @@ func Routes(route *gin.Engine) {
}
context.JSON(200, extractor.FormatMessage(data, true))
})
auth.POST("/followingStreamer/bulk", func(context *gin.Context) {
var f postData
err := context.ShouldBindJSON(&f)
if err != nil {
context.Error(err)
return
}
data, err := twitch.BulkFollowingUser(f.Streamers)
if err != nil {
context.Error(err)
return
}
context.JSON(200, extractor.FormatMessage(data, true))
})
}