vanity-tester-backend-go/cmd/vanity-tester-backend/gql.go

58 lines
1.6 KiB
Go
Raw Normal View History

2025-01-07 23:42:16 -03:00
package main
import (
2025-01-08 20:16:19 -03:00
"bytes"
"encoding/json"
"fmt"
2025-01-07 23:42:16 -03:00
"io"
"net/http"
)
2025-01-08 20:16:19 -03:00
type Gql struct{}
2025-01-07 23:42:16 -03:00
2025-01-08 20:16:19 -03:00
func (s *Gql) getUserID(username string) (string, error) {
2025-01-07 23:42:16 -03:00
headers := map[string]string{
"Content-Type": "application/json",
"Client-Id": config.Gql.ClientID,
2025-01-08 20:16:19 -03:00
// 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",
2025-01-07 23:42:16 -03:00
}
2025-01-08 20:16:19 -03:00
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))
2025-01-07 23:42:16 -03:00
if err != nil {
2025-01-08 20:16:19 -03:00
return "", err
2025-01-07 23:42:16 -03:00
}
2025-01-08 20:16:19 -03:00
for k, v := range headers {
request.Header.Add(k, v)
2025-01-07 23:42:16 -03:00
}
2025-01-08 20:16:19 -03:00
res, err := client.Do(request)
2025-01-07 23:42:16 -03:00
if err != nil {
2025-01-08 20:16:19 -03:00
return "", err
2025-01-07 23:42:16 -03:00
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
2025-01-08 20:16:19 -03:00
return string(body), nil
2025-01-07 23:42:16 -03:00
}