moved translation to utils.go
This commit is contained in:
51
main.go
51
main.go
@@ -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)
|
if err != nil {
|
||||||
v.Set("target_lang", lang.String())
|
log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
|
||||||
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
|
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
|
continue
|
||||||
}
|
}
|
||||||
sendNormalResp:
|
sendNormalResp:
|
||||||
|
@@ -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
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user