From 231150c9f51c2d1d0ba6570be49e12e775ba7a96 Mon Sep 17 00:00:00 2001 From: xiaoxuan6 <1527736751@qq.com> Date: Wed, 28 Aug 2024 21:00:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deeplx.go | 87 ++++++++++++++++++++++++++++---------------------- deeplx_test.go | 4 +-- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/deeplx.go b/deeplx.go index b66d6a5..f2ffad5 100644 --- a/deeplx.go +++ b/deeplx.go @@ -3,7 +3,6 @@ package deeplx import ( "bufio" "encoding/json" - "errors" "github.com/abadojack/whatlanggo" "github.com/avast/retry-go" "github.com/tidwall/gjson" @@ -11,27 +10,33 @@ import ( "math/rand" "net/http" "strings" + "time" ) var urls = []string{"https://deeplx.mingming.dev/translate", "https://deeplx.niubipro.com/translate"} -func Translate(text, sourceLang, targetLang string) (string, error) { - if len(text) == 0 { - return "", errors.New("No Translate Text Found") - } +type Request struct { + Text string `json:"text"` + SourceLang string `json:"source_lang"` + TargetLang string `json:"target_lang"` +} - if len(sourceLang) == 0 { - lang := whatlanggo.DetectLang(text) - deepLLang := strings.ToUpper(lang.Iso6391()) - sourceLang = deepLLang - } +type Response struct { + Code int64 `json:"code"` + Data string `json:"data"` + Msg string `json:"msg"` +} - if len(targetLang) == 0 { - targetLang = "EN" +func fetchUri() string { + client := &http.Client{ + Timeout: 3 * time.Second, } - resp, err := http.Get("https://github-mirror.us.kg/https://github.com/ycvk/deeplx-local/blob/windows/url.txt") - defer resp.Body.Close() + resp, err := client.Get("https://github-mirror.us.kg/https://github.com/ycvk/deeplx-local/blob/windows/url.txt") + defer func() { + _ = resp.Body.Close() + }() + if err == nil { r := bufio.NewReader(resp.Body) for { @@ -45,40 +50,38 @@ func Translate(text, sourceLang, targetLang string) (string, error) { } randomIndex := rand.Intn(len(urls)) - uri := urls[randomIndex] - - response, err := post(uri, RequestParams{ - Text: text, - SourceLang: sourceLang, - TargetLang: targetLang, - }) + return urls[randomIndex] +} - if err != nil { - return "", err +func Translate(text, sourceLang, targetLang string) Response { + if len(text) == 0 { + return Response{ + Code: 500, + Msg: "No Translate Text Found", + } } - if gjson.Get(string(response), "code").Int() != 200 { - return "", errors.New(gjson.Get(string(response), "message").String()) + if len(sourceLang) == 0 { + lang := whatlanggo.DetectLang(text) + deepLLang := strings.ToUpper(lang.Iso6391()) + sourceLang = deepLLang } - return gjson.Get(string(response), "data").String(), nil -} - -type RequestParams struct { - Text string `json:"text"` - SourceLang string `json:"source_lang"` - TargetLang string `json:"target_lang"` -} - -func post(url string, request RequestParams) ([]byte, error) { + if len(targetLang) == 0 { + targetLang = "EN" + } + request := &Request{ + Text: text, + SourceLang: sourceLang, + TargetLang: targetLang, + } jsonBody, _ := json.Marshal(request) - params := strings.NewReader(string(jsonBody)) var body []byte - err := retry.Do( + _ = retry.Do( func() error { - response, err := http.Post(url, "application/json", params) + response, err := http.Post(fetchUri(), "application/json", strings.NewReader(string(jsonBody))) if err == nil { defer func() { @@ -86,6 +89,8 @@ func post(url string, request RequestParams) ([]byte, error) { }() body, err = io.ReadAll(response.Body) + } else { + body = []byte(`{"code":500, "message": ` + err.Error() + `}`) } return err @@ -94,5 +99,9 @@ func post(url string, request RequestParams) ([]byte, error) { retry.LastErrorOnly(true), ) - return body, err + return Response{ + Code: gjson.Get(string(body), "code").Int(), + Data: gjson.Get(string(body), "data").String(), + Msg: gjson.Get(string(body), "message").String(), + } } diff --git a/deeplx_test.go b/deeplx_test.go index d5fd2be..5f2a86d 100644 --- a/deeplx_test.go +++ b/deeplx_test.go @@ -6,7 +6,7 @@ import ( ) func TestTranslate(t *testing.T) { - response, err := Translate("Hello", "EN", "ZH") - assert.Nil(t, err) + response := Translate("Hello", "EN", "ZH") + assert.Equal(t, int64(200), response.Code) t.Log(response) }