From 5bc48c5ad71327013b649af571e7e24a4983b5ae Mon Sep 17 00:00:00 2001 From: eeeXun Date: Sat, 10 Aug 2024 22:31:54 +0800 Subject: [PATCH] refactor: move DeepLX to DeepL --- README.md | 2 +- internal/translate/deepl/translator.go | 59 +++++++++++++++++- internal/translate/deeplx/language.go | 67 --------------------- internal/translate/deeplx/translator.go | 80 ------------------------- internal/translate/translator.go | 7 +-- 5 files changed, 59 insertions(+), 156 deletions(-) delete mode 100644 internal/translate/deeplx/language.go delete mode 100644 internal/translate/deeplx/translator.go diff --git a/README.md b/README.md index af99590..341bfbd 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ See the example in [server.yaml](example/server.yaml) file. ```yaml api_key: deeplx: - value: DEEPLX_API_KEY # <- Replace with your API Key + value: DEEPLX_API_KEY # <- Replace with your TOKEN # file: $HOME/secrets/deeplx.txt # <- You can also specify the file where to read API Key host: deeplx: 127.0.0.1:1188 # <- Replace with your server IP address and port diff --git a/internal/translate/deepl/translator.go b/internal/translate/deepl/translator.go index 6df2de9..5f55b8e 100644 --- a/internal/translate/deepl/translator.go +++ b/internal/translate/deepl/translator.go @@ -1,6 +1,7 @@ package deepl import ( + "bytes" "encoding/json" "errors" "io" @@ -22,12 +23,12 @@ type Translator struct { core.EngineName } -func NewTranslator() *Translator { +func NewTranslator(name string) *Translator { return &Translator{ Server: new(core.Server), Language: new(core.Language), TTS: core.NewTTS(), - EngineName: core.NewEngineName("DeepL"), + EngineName: core.NewEngineName(name), } } @@ -35,7 +36,7 @@ func (t *Translator) GetAllLang() []string { return lang } -func (t *Translator) Translate(message string) (translation *core.Translation, err error) { +func (t *Translator) deeplTranslate(message string) (translation *core.Translation, err error) { translation = new(core.Translation) var data map[string]interface{} @@ -75,6 +76,58 @@ func (t *Translator) Translate(message string) (translation *core.Translation, e return translation, nil } +func (t *Translator) deeplxTranslate(message string) (translation *core.Translation, err error) { + translation = new(core.Translation) + var data map[string]interface{} + + if len(t.GetHost()) <= 0 { + return nil, errors.New("Please write your host in config file for " + t.GetEngineName()) + } + + userData, _ := json.Marshal(map[string]interface{}{ + "text": message, + "source_lang": langCode[t.GetSrcLang()], + "target_lang": langCode[t.GetDstLang()], + }) + req, _ := http.NewRequest(http.MethodPost, + "http://"+t.GetHost()+"/translate", + bytes.NewBuffer(userData), + ) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+t.GetAPIKey()) + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + body, err := io.ReadAll(res.Body) + if err != nil { + return nil, err + } + if err = json.Unmarshal(body, &data); err != nil { + return nil, err + } + + if len(data) <= 0 { + return nil, errors.New("Translation not found") + } + if res.StatusCode != 200 { + return nil, errors.New(data["message"].(string)) + } + + translation.TEXT = data["data"].(string) + + return translation, nil +} + +func (t *Translator) Translate(message string) (translation *core.Translation, err error) { + switch t.GetEngineName() { + case "DeepLX": + return t.deeplxTranslate(message) + default: + return t.deeplTranslate(message) + } +} + func (t *Translator) PlayTTS(lang, message string) error { defer t.ReleaseLock() diff --git a/internal/translate/deeplx/language.go b/internal/translate/deeplx/language.go deleted file mode 100644 index 02480c4..0000000 --- a/internal/translate/deeplx/language.go +++ /dev/null @@ -1,67 +0,0 @@ -package deeplx - -// Generated from DeepL -var ( - lang = []string{ - "Bulgarian", - "Chinese", - "Czech", - "Danish", - "Dutch", - "English", - "Estonian", - "Finnish", - "French", - "German", - "Greek", - "Hungarian", - "Indonesian", - "Italian", - "Japanese", - "Korean", - "Latvian", - "Lithuanian", - "Norwegian", - "Polish", - "Portuguese", - "Romanian", - "Russian", - "Slovak", - "Slovenian", - "Spanish", - "Swedish", - "Turkish", - "Ukrainian", - } - langCode = map[string]string{ - "Bulgarian": "BG", - "Chinese": "ZH", - "Czech": "CS", - "Danish": "DA", - "Dutch": "NL", - "English": "EN", - "Estonian": "ET", - "Finnish": "FI", - "French": "FR", - "German": "DE", - "Greek": "EL", - "Hungarian": "HU", - "Indonesian": "ID", - "Italian": "IT", - "Japanese": "JA", - "Korean": "KO", - "Latvian": "LV", - "Lithuanian": "LT", - "Norwegian": "NB", - "Polish": "PL", - "Portuguese": "PT", - "Romanian": "RO", - "Russian": "RU", - "Slovak": "SK", - "Slovenian": "SL", - "Spanish": "ES", - "Swedish": "SV", - "Turkish": "TR", - "Ukrainian": "UK", - } -) diff --git a/internal/translate/deeplx/translator.go b/internal/translate/deeplx/translator.go deleted file mode 100644 index 307bb4c..0000000 --- a/internal/translate/deeplx/translator.go +++ /dev/null @@ -1,80 +0,0 @@ -package deeplx - -import ( - "bytes" - "encoding/json" - "errors" - "io" - "net/http" - - "github.com/eeeXun/gtt/internal/translate/core" -) - -type Translator struct { - *core.Server - *core.Language - *core.TTS - core.EngineName -} - -func NewTranslator() *Translator { - return &Translator{ - Server: new(core.Server), - Language: new(core.Language), - TTS: core.NewTTS(), - EngineName: core.NewEngineName("DeepLX"), - } -} - -func (t *Translator) GetAllLang() []string { - return lang -} - -func (t *Translator) Translate(message string) (translation *core.Translation, err error) { - translation = new(core.Translation) - var data map[string]interface{} - - if len(t.GetHost()) <= 0 { - return nil, errors.New("Please write your host in config file for " + t.GetEngineName()) - } - - userData, _ := json.Marshal(map[string]interface{}{ - "text": message, - "source_lang": langCode[t.GetSrcLang()], - "target_lang": langCode[t.GetDstLang()], - }) - req, _ := http.NewRequest(http.MethodPost, - "http://"+t.GetHost()+"/translate", - bytes.NewBuffer(userData), - ) - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", "Bearer "+t.GetAPIKey()) - res, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - body, err := io.ReadAll(res.Body) - if err != nil { - return nil, err - } - if err = json.Unmarshal(body, &data); err != nil { - return nil, err - } - - if len(data) <= 0 { - return nil, errors.New("Translation not found") - } - if res.StatusCode != 200 { - return nil, errors.New(data["message"].(string)) - } - - translation.TEXT = data["data"].(string) - - return translation, nil -} - -func (t *Translator) PlayTTS(lang, message string) error { - defer t.ReleaseLock() - - return errors.New(t.GetEngineName() + " does not support text to speech") -} diff --git a/internal/translate/translator.go b/internal/translate/translator.go index a86fa44..1c99406 100644 --- a/internal/translate/translator.go +++ b/internal/translate/translator.go @@ -6,7 +6,6 @@ import ( "github.com/eeeXun/gtt/internal/translate/chatgpt" "github.com/eeeXun/gtt/internal/translate/core" "github.com/eeeXun/gtt/internal/translate/deepl" - "github.com/eeeXun/gtt/internal/translate/deeplx" "github.com/eeeXun/gtt/internal/translate/google" "github.com/eeeXun/gtt/internal/translate/libre" "github.com/eeeXun/gtt/internal/translate/reverso" @@ -79,10 +78,8 @@ func NewTranslator(name string) Translator { translator = bing.NewTranslator() case "ChatGPT": translator = chatgpt.NewTranslator() - case "DeepL": - translator = deepl.NewTranslator() - case "DeepLX": - translator = deeplx.NewTranslator() + case "DeepL", "DeepLX": + translator = deepl.NewTranslator(name) case "Google": translator = google.NewTranslator() case "Libre":