From 367b7452687a288d7cbb9b0dc877055efacaf891 Mon Sep 17 00:00:00 2001 From: dragongoose Date: Mon, 3 Jul 2023 19:09:39 -0400 Subject: [PATCH] Complete translation support --- extractor/twitch/twitchExtractor.go | 21 +++++++-------------- main.go | 6 ++++++ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/extractor/twitch/twitchExtractor.go b/extractor/twitch/twitchExtractor.go index f58d2b0..cb36ff5 100644 --- a/extractor/twitch/twitchExtractor.go +++ b/extractor/twitch/twitchExtractor.go @@ -15,32 +15,25 @@ import ( 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 getResponse(url string) ([]byte, error) { - resp, err := http.Get(url) - if err != nil { - return []byte{}, err - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return []byte{}, err - } - - return body, nil -} - func parseResponse(payload []TwitchPayload) (response *[]structs.TwitchApiResponse, body []byte, err error) { json_data, err := json.Marshal(payload) if err != nil { diff --git a/main.go b/main.go index 1950d0d..2da5c1f 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "log" "os" "safetwitch-backend/extractor/chat" + "safetwitch-backend/extractor/twitch" "safetwitch-backend/routes" "strings" @@ -40,6 +41,10 @@ func CORS() gin.HandlerFunc { } } +func setLang(c *gin.Context) { + twitch.SetLanguage(c.GetHeader("Accept-Language")) +} + func main() { log.Println("Starting Safetwitch...") // check for env @@ -54,6 +59,7 @@ func main() { go chat.BeginTwitchChatConnection() + router.Use(setLang) router.Use(ErrorHandler) routes.SetRoutes(router) router.NoRoute(notFoundHandler)