Skip to content

Commit

Permalink
refactor: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuan6 committed Aug 28, 2024
1 parent bccab25 commit 231150c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
87 changes: 48 additions & 39 deletions deeplx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,40 @@ package deeplx
import (
"bufio"
"encoding/json"
"errors"
"github.com/abadojack/whatlanggo"
"github.com/avast/retry-go"
"github.com/tidwall/gjson"
"io"
"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 {
Expand All @@ -45,47 +50,47 @@ 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() {
_ = response.Body.Close()
}()

body, err = io.ReadAll(response.Body)
} else {
body = []byte(`{"code":500, "message": ` + err.Error() + `}`)
}

return err
Expand All @@ -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(),
}
}
4 changes: 2 additions & 2 deletions deeplx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 231150c

Please sign in to comment.