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

51
main.go
View File

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

View File

@@ -19,6 +19,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"regexp"
"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) {
var idsToUpdate []uint64