vanity-tester-backend-go/cmd/vanity-tester-backend/gql.go
2025-01-08 20:16:19 -03:00

57 lines
1.6 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type Gql struct{}
func (s *Gql) getUserID(username string) (string, error) {
headers := map[string]string{
"Content-Type": "application/json",
"Client-Id": config.Gql.ClientID,
// The headers from bellow are not really necessary, but since it's GQL,
// it's better to fake the headers to make it more like a real browser
// and not just an scrapper.
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Access-Control-Request-Method": "POST",
"Access-Control-Request-Headers": "authorization,client-id,client-session-id,client-version,x-device-id",
"Referer": "https://www.twitch.tv/",
"Origin": "https://www.twitch.tv",
"Connection": "keep-alive",
}
query := map[string]string{"query": fmt.Sprintf(`query { user(login: "%s") { id } }`, username)}
data, err := json.Marshal(query)
if err != nil {
return "", logger.Error().GetCtx().Err()
}
logger.Trace().Msg(string(data))
request, err := http.NewRequest("POST", "https://gql.twitch.tv/gql", bytes.NewBuffer(data))
if err != nil {
return "", err
}
for k, v := range headers {
request.Header.Add(k, v)
}
res, err := client.Do(request)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
return string(body), nil
}