fixed module path; added translate endpoint to chat API

This commit is contained in:
2022-02-12 01:18:29 +01:00
parent 440aa41396
commit f2a859d084
56 changed files with 256 additions and 184 deletions

71
main.go
View File

@@ -3,19 +3,20 @@ package main
import (
"bytes"
"context"
"csgowtfd/csgo"
"csgowtfd/ent"
"csgowtfd/ent/match"
"csgowtfd/ent/matchplayer"
"csgowtfd/ent/messages"
"csgowtfd/ent/migrate"
"csgowtfd/ent/player"
"csgowtfd/utils"
"encoding/gob"
"encoding/json"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"flag"
"fmt"
"git.harting.dev/csgowtf/csgowtfd/csgo"
"git.harting.dev/csgowtf/csgowtfd/ent"
"git.harting.dev/csgowtf/csgowtfd/ent/match"
"git.harting.dev/csgowtf/csgowtfd/ent/matchplayer"
"git.harting.dev/csgowtf/csgowtfd/ent/messages"
"git.harting.dev/csgowtf/csgowtfd/ent/migrate"
"git.harting.dev/csgowtf/csgowtfd/ent/player"
"git.harting.dev/csgowtf/csgowtfd/utils"
"github.com/go-redis/cache/v8"
"github.com/go-redis/redis/v8"
"github.com/gorilla/handlers"
@@ -25,9 +26,12 @@ import (
log "github.com/sirupsen/logrus"
"github.com/wercker/journalhook"
"go.uber.org/ratelimit"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
"io"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
@@ -653,12 +657,29 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
id := mux.Vars(r)["id"]
trans := r.FormValue("translate")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
tag, _, err := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
if err != nil {
log.Warningf("[GMC] Unable to parse Accept-Language %s: %v", r.Header.Get("Accept-Language"), err)
w.WriteHeader(http.StatusBadRequest)
return
}
var translate bool
if trans != "" {
translate, err = strconv.ParseBool(trans)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
matchId, err := strconv.ParseUint(id, 10, 64)
if err != nil {
log.Infof("[GMC] Error parsing matchID %s: %v", id, err)
@@ -681,6 +702,39 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
resp[steamid] = make([]*utils.ChatResponse, 0)
}
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)
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
}
resp[steamid] = append(resp[steamid], &utils.ChatResponse{
Message: dlRespJSON.Translations[0].Text,
AllChat: stat.AllChat,
Tick: stat.Tick,
TranslatedFrom: dlRespJSON.Translations[0].DetectedSourceLanguage,
TranslatedTo: lang.String(),
})
}
continue
}
sendNormalResp:
resp[steamid] = append(resp[steamid], &utils.ChatResponse{
Message: stat.Message,
AllChat: stat.AllChat,
@@ -1126,6 +1180,7 @@ func main() {
router.HandleFunc(`/match/{id:\d{19}}/chat`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/match/{id:\d{19}}/chat`, getMatchChat).Methods(http.MethodGet)
router.HandleFunc(`/match/{id:\d{19}}/chat`, getMatchChat).Queries("translate", "{translate}").Methods(http.MethodGet)
router.HandleFunc(`/matches`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/matches`, getMatches).Methods(http.MethodGet)