moved translation to utils.go

This commit is contained in:
2022-02-12 01:51:42 +01:00
parent 5d299dfa17
commit 8fe8ec5233
2 changed files with 43 additions and 34 deletions

31
main.go
View File

@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/gob" "encoding/gob"
"encoding/json"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"flag" "flag"
@@ -28,10 +27,8 @@ import (
"go.uber.org/ratelimit" "go.uber.org/ratelimit"
"golang.org/x/text/language" "golang.org/x/text/language"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io"
"net" "net"
"net/http" "net/http"
"net/url"
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
@@ -704,39 +701,25 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
if translate { if translate {
lang, _ := tag[0].Base() lang, _ := tag[0].Base()
v := url.Values{} translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(),
v.Set("auth_key", conf.Csgowtfd.DeepL.APIKey) conf.Csgowtfd.DeepL.BaseURL, conf.Csgowtfd.DeepL.APIKey)
v.Set("text", stat.Message)
v.Set("target_lang", lang.String())
dlResp, err := http.PostForm("https://"+conf.Csgowtfd.DeepL.BaseURL+"/v2/translate", v)
if err != nil || dlResp.StatusCode != http.StatusOK {
log.Warningf("[GMC] Unable to translate with DeepL: %v (HTTP %d)", err, dlResp.StatusCode)
goto sendNormalResp
} else {
respBytes, err := io.ReadAll(dlResp.Body)
if err != nil { if err != nil {
log.Warningf("[GMC] Unable to translate with DeepL: %v (HTTP %d)", err, dlResp.StatusCode) log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
goto sendNormalResp
}
dlRespJSON := new(utils.DeepLResponse)
err = json.Unmarshal(respBytes, &dlRespJSON)
if err != nil {
log.Warningf("[GMC] Unable to unmarshal JSON from DeepL: %v", err)
goto sendNormalResp goto sendNormalResp
} }
if strings.ToLower(dlRespJSON.Translations[0].DetectedSourceLanguage) == lang.String() { if srcLang == lang.String() {
goto sendNormalResp goto sendNormalResp
} }
resp[steamid] = append(resp[steamid], &utils.ChatResponse{ resp[steamid] = append(resp[steamid], &utils.ChatResponse{
Message: dlRespJSON.Translations[0].Text, Message: translated,
AllChat: stat.AllChat, AllChat: stat.AllChat,
Tick: stat.Tick, Tick: stat.Tick,
TranslatedFrom: strings.ToLower(dlRespJSON.Translations[0].DetectedSourceLanguage), TranslatedFrom: srcLang,
TranslatedTo: lang.String(), TranslatedTo: lang.String(),
}) })
}
continue continue
} }
sendNormalResp: sendNormalResp:

View File

@@ -19,6 +19,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"path" "path"
"regexp" "regexp"
"sort" "sort"
@@ -686,6 +687,31 @@ func PlayerFromSteamID64(db *ent.Client, steamID uint64, apiKey string, rl ratel
} }
} }
func TranslateWithDeepL(text string, language string, baseURL string, apiKey string) (translated string, detectedLanguage string, err error) {
v := url.Values{}
v.Set("auth_key", apiKey)
v.Set("text", text)
v.Set("target_lang", language)
dlResp, err := http.PostForm("https://"+baseURL+"/v2/translate", v)
if err != nil {
return "", "", fmt.Errorf("deepl response: %w", err)
} else if dlResp.StatusCode != http.StatusOK {
return "", "", fmt.Errorf("deepl response %d", dlResp.StatusCode)
} else {
respBytes, err := io.ReadAll(dlResp.Body)
if err != nil {
return "", "", fmt.Errorf("error reading deepl response: %w", err)
}
dlRespJSON := new(DeepLResponse)
err = json.Unmarshal(respBytes, &dlRespJSON)
if err != nil {
return "", "", fmt.Errorf("error decoding json from deepl: %w", err)
}
return dlRespJSON.Translations[0].Text, strings.ToLower(dlRespJSON.Translations[0].DetectedSourceLanguage), nil
}
}
func PlayerFromSteam(players []*ent.Player, db *ent.Client, apiKey string, rl ratelimit.Limiter) ([]*ent.Player, error) { func PlayerFromSteam(players []*ent.Player, db *ent.Client, apiKey string, rl ratelimit.Limiter) ([]*ent.Player, error) {
var idsToUpdate []uint64 var idsToUpdate []uint64