mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 05:02:58 -05:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package twitch
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"safetwitch-backend/extractor/structs"
|
|
)
|
|
|
|
const twitchUrl = "https://gql.twitch.tv/gql"
|
|
|
|
var Language string = "en"
|
|
|
|
// useful funcs
|
|
func SetLanguage(code string) {
|
|
Language = code
|
|
}
|
|
|
|
func call(url, method string, body io.Reader) (*http.Response, error) {
|
|
req, err := http.NewRequest(method, url, body)
|
|
if err != nil {
|
|
return req.Response, err
|
|
}
|
|
|
|
req.Header.Add("Accept-Language", Language)
|
|
req.Header.Add("Client-Id", "ue6666qo983tsx6so1t0vnawi233wa")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
return resp, err
|
|
}
|
|
|
|
func parseResponse(payload []TwitchPayload) (response *[]structs.TwitchApiResponse, body []byte, err error) {
|
|
json_data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
resp, err := call(twitchUrl, "POST", bytes.NewBuffer(json_data))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
rawBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var twitchResponse []structs.TwitchApiResponse
|
|
err = json.Unmarshal(rawBody, &twitchResponse)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return &twitchResponse, rawBody, nil
|
|
}
|
|
|
|
type TwitchPayload = map[string]interface{}
|