0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 13:13:00 -05:00
safetwitch-backend/extractor/twitch/PlaybackAccessToken.go
2023-07-21 10:28:39 -04:00

71 lines
1.6 KiB
Go

package twitch
import (
"github.com/tidwall/gjson"
)
type PlaybackAccessToken struct {
Signature string
Token string
}
// Assumes streamer is live
// If no vodID give ""
func getPlaybackAccessToken(streamerName string, vodID string) (PlaybackAccessToken, error) {
var isVod bool
var isLive bool
if vodID != "" {
isVod = true
isLive = false
} else {
isVod = false
isLive = true
}
payload1 := []TwitchPayload{
{
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "0828119ded1c13477966434e15800ff57ddacf13ba1911c129dc2200705b0712",
},
},
"operationName": "PlaybackAccessToken",
"variables": map[string]interface{}{
"isLive": isLive,
"login": streamerName,
"isVod": isVod,
"vodID": vodID,
"playerType": "site",
},
},
}
_, body, err := parseResponse(payload1)
if err != nil {
return PlaybackAccessToken{}, err
}
var token string
var signature string
if isLive {
token = gjson.Get(string(body), "0.data.streamPlaybackAccessToken.value").String()
signature = gjson.Get(string(body), "0.data.streamPlaybackAccessToken.signature").String()
if err != nil {
return PlaybackAccessToken{}, err
}
} else {
token = gjson.Get(string(body), "0.data.videoPlaybackAccessToken.value").String()
signature = gjson.Get(string(body), "0.data.videoPlaybackAccessToken.signature").String()
if err != nil {
return PlaybackAccessToken{}, err
}
}
return PlaybackAccessToken{
Signature: signature,
Token: token,
}, nil
}