mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2025-01-08 13:50:05 -05:00
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package twitch
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
func GetStream(streamerName string) (string, error) {
|
|
// STAGE 1
|
|
// Get playback token from twitch
|
|
// Same request browser makes
|
|
payload1 := []TwitchPayload{
|
|
{
|
|
"extensions": map[string]interface{}{
|
|
"persistedQuery": map[string]interface{}{
|
|
"version": 1,
|
|
"sha256Hash": "0828119ded1c13477966434e15800ff57ddacf13ba1911c129dc2200705b0712",
|
|
},
|
|
},
|
|
"operationName": "PlaybackAccessToken",
|
|
"variables": map[string]interface{}{
|
|
"isLive": true,
|
|
"login": streamerName,
|
|
"isVod": false,
|
|
"vodID": "",
|
|
"playerType": "site",
|
|
},
|
|
},
|
|
}
|
|
|
|
_, body, err := parseResponse(payload1)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// These will be needed to get playlist file from twitch
|
|
token := gjson.Get(string(body), "0.data.streamPlaybackAccessToken.value").String()
|
|
signature := gjson.Get(string(body), "0.data.streamPlaybackAccessToken.signature").String()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
playlistUrl := "https://usher.ttvnw.net/api/channel/hls/" + strings.ToLower(streamerName) + ".m3u8"
|
|
params := "?sig=" + signature + "&token=" + token
|
|
|
|
req, err := http.NewRequest("GET", playlistUrl+params, nil)
|
|
req.Header.Add("Client-Id", "ue6666qo983tsx6so1t0vnawi233wa")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
playlistFile, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// holy zooks, what the scallop??? we got the playlist, houston!!!
|
|
// time to proxy all the urls!!!
|
|
proxiedPlaylist := ProxyPlaylistFile(string(playlistFile), false)
|
|
return proxiedPlaylist, nil
|
|
|
|
}
|
|
|
|
func GetSubPlaylist(rawurl string) (string, error) {
|
|
req, err := http.NewRequest("GET", rawurl, nil)
|
|
|
|
req.Header.Add("Client-Id", "ue6666qo983tsx6so1t0vnawi233wa")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
playlistFile, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
proxiedPlaylist := ProxyPlaylistFile(string(playlistFile), true)
|
|
return proxiedPlaylist, nil
|
|
}
|