Skip to content

Commit

Permalink
feat: 添加更多翻译接口,接口请求时验证接口有效性
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuan6 committed Nov 25, 2024
1 parent 6eaa023 commit f1bddb3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 81 deletions.
97 changes: 16 additions & 81 deletions deeplx.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,29 @@
package deeplx

import (
"bufio"
"encoding/json"
"bytes"
"github.com/OwO-Network/DeepLX/translate"
"github.com/abadojack/whatlanggo"
"github.com/avast/retry-go"
"github.com/tidwall/gjson"
"io"
"math/rand"
"net/http"
"strings"
"sync"
"time"
)

var (
wg sync.WaitGroup

targetUrls = make([]string, 0)
urls = []string{"https://deeplx.mingming.dev/translate"}
)

type request struct {
Text string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
}

type Response struct {
Code int64 `json:"code"`
Data string `json:"data"`
Msg string `json:"msg"`
}

func fetchUri() string {
if len(targetUrls) < 1 {
client := &http.Client{
Timeout: 3 * time.Second,
}

resp, err := client.Get("https://github-mirror.us.kg/https://github.com/ycvk/deeplx-local/blob/windows/url.txt")
if err == nil && resp.StatusCode == 200 {
r := bufio.NewReader(resp.Body)
for {
line, _, errs := r.ReadLine()
if errs == io.EOF {
break
}

targetUrls = append(targetUrls, string(line))
}
}

var wg sync.WaitGroup
for _, url := range targetUrls {
wg.Add(1)
url := url

go func() {
defer wg.Done()
resp, err = client.Get(url)
if err != nil {
return
}
defer resp.Body.Close()

if resp.StatusCode == 200 {
urls = append(urls, url)
}
}()
}
wg.Wait()
}

urlsLen := len(urls)
randomIndex := rand.Intn(urlsLen)
if randomIndex >= urlsLen {
return urls[0]
} else {
return urls[randomIndex]
}
}

func Translate(text, sourceLang, targetLang string) *Response {
if len(text) == 0 {
return &Response{
Expand All @@ -98,26 +41,18 @@ func Translate(text, sourceLang, targetLang string) *Response {
targetLang = "EN"
}

req := &request{
Text: text,
SourceLang: sourceLang,
TargetLang: targetLang,
}
jsonBody, _ := json.Marshal(req)
requestParams := bytes.Buffer{}
requestParams.WriteString(`{"text":"` + text + `","source_lang":"` + sourceLang + `","target_lang":"` + targetLang + `"}`)

var body []byte
err := retry.Do(
func() error {
client := &http.Client{
Timeout: 3 * time.Second,
}
response, err := client.Post(fetchUri(), "application/json", strings.NewReader(string(jsonBody)))
response, err := client.Post(fetchUri(), "application/json", strings.NewReader(requestParams.String()))
defer func() {
_ = response.Body.Close()
}()

if err == nil {
defer func() {
_ = response.Body.Close()
}()

body, err = io.ReadAll(response.Body)
} else {
body = []byte(`{"code":500, "message": ` + err.Error() + `}`)
Expand All @@ -129,15 +64,15 @@ func Translate(text, sourceLang, targetLang string) *Response {
retry.LastErrorOnly(true),
)

if err == nil {
return &Response{
Code: gjson.Get(string(body), "code").Int(),
Data: gjson.Get(string(body), "data").String(),
Msg: gjson.Get(string(body), "message").String(),
}
if err != nil {
return TranslateByDeeplx(text, sourceLang, targetLang)
}

return TranslateByDeeplx(text, sourceLang, targetLang)
return &Response{
Code: gjson.Get(string(body), "code").Int(),
Data: gjson.Get(string(body), "data").String(),
Msg: gjson.Get(string(body), "message").String(),
}
}

func TranslateByDeeplx(text, sourceLang, targetLang string) *Response {
Expand Down
87 changes: 87 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package deeplx

import (
"bufio"
"fmt"
"io"
"math/rand"
"net/http"
"strings"
"sync"
"time"
)

func fetchUri() string {
if len(urls) < 1 {

var wgs sync.WaitGroup
wgs.Add(2)
for i, url := range []string{
"https://github-mirror.us.kg/https://github.com/ycvk/deeplx-local/blob/windows/url.txt",
"https://github-mirror.us.kg/https://github.com/xiaozhou26/serch_deeplx/blob/main/success.txt",
} {
go fetchUrls(&wgs, i, url)
}
wgs.Wait()

for _, url := range targetUrls {
wg.Add(1)
go checkUrlVerify(url, &wg)
}
wg.Wait()
}

randomIndex := randomNum()
if randomIndex <= len(urls) {
return urls[randomIndex]
}

return urls[0]
}

var client = &http.Client{
Timeout: 3 * time.Second,
}

func fetchUrls(wg *sync.WaitGroup, k int, url string) {
defer wg.Done()

resp, err := client.Get(url)
if err != nil {
return
}
defer resp.Body.Close()

r := bufio.NewReader(resp.Body)
for {
line, _, errs := r.ReadLine()
if errs == io.EOF {
break
}

newUrl := string(line)
if k == 1 {
newUrl = fmt.Sprintf("%s/translate", newUrl)
}
targetUrls = append(targetUrls, newUrl)
}
}

func checkUrlVerify(url string, wg *sync.WaitGroup) {
defer wg.Done()

resp, err := client.Get(strings.ReplaceAll(url, "/translate", ""))
if err != nil {
return
}
defer resp.Body.Close()

if resp.StatusCode == 200 {
urls = append(urls, url)
}
}

func randomNum() int {
urlsLen := len(urls)
return rand.Intn(urlsLen)
}

0 comments on commit f1bddb3

Please sign in to comment.