Compare commits

...
8 Commits
Author SHA1 Message Date
Vincent Yang d98f71d1c5 fix: add pro support (may have no effect) 2024-11-01 13:13:28 -04:00
Vincent Yang 9edb997f06 fix: support CommonJobParams 2024-11-01 12:48:26 -04:00
Vincent Yang 62a993bb13 fix: unable to translate 2024-11-01 00:43:57 -04:00
Vincent Yang c678e87631 bump to Go 1.23.2 2024-10-07 19:25:36 -04:00
Vincent Young ac8ec23790 Update translate.go 2024-10-03 16:07:38 -04:00
Vincent Young 9e8c68fc94 Revert "Fix typo. (#143)" (#146)
This reverts commit 1bef182a15.
2024-10-03 16:05:53 -04:00
ryan961 1bef182a15 Fix typo. (#143) 2024-09-18 21:33:11 -04:00
Vincent Yang 74cf2b9845 fix: remove authkey 2024-09-17 19:34:32 -04:00
10 changed files with 339 additions and 368 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v4 uses: actions/setup-go@v4
with: with:
go-version: '1.22' go-version: '1.23.2'
- name: Install golint - name: Install golint
run: go install golang.org/x/lint/golint@latest run: go install golang.org/x/lint/golint@latest
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: "1.22" go-version: "1.23.2"
- run: bash .cross_compile.sh - run: bash .cross_compile.sh
+1 -1
View File
@@ -1,4 +1,4 @@
FROM golang:1.23.1 AS builder FROM golang:1.23.2 AS builder
WORKDIR /go/src/github.com/OwO-Network/DeepLX WORKDIR /go/src/github.com/OwO-Network/DeepLX
COPY . . COPY . .
RUN go get -d -v ./ RUN go get -d -v ./
-1
View File
@@ -8,5 +8,4 @@ services:
- "1188:1188" - "1188:1188"
# environment: # environment:
# - TOKEN=helloworld # - TOKEN=helloworld
# - AUTHKEY=xxxxxxx:fx
# - DL_SESSION=xxxxxx # - DL_SESSION=xxxxxx
+2 -3
View File
@@ -1,8 +1,8 @@
/* /*
* @Author: Vincent Yang * @Author: Vincent Yang
* @Date: 2024-04-23 00:39:03 * @Date: 2024-04-23 00:39:03
* @LastEditors: Vincent Young * @LastEditors: Vincent Yang
* @LastEditTime: 2024-09-16 12:02:15 * @LastEditTime: 2024-09-17 19:34:32
* @FilePath: /DeepLX/config.go * @FilePath: /DeepLX/config.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@@ -22,7 +22,6 @@ type Config struct {
IP string IP string
Port int Port int
Token string Token string
AuthKey string
DlSession string DlSession string
Proxy string Proxy string
} }
+1 -1
View File
@@ -1,6 +1,6 @@
module github.com/OwO-Network/DeepLX module github.com/OwO-Network/DeepLX
go 1.23.1 go 1.23.2
require ( require (
github.com/abadojack/whatlanggo v1.0.1 github.com/abadojack/whatlanggo v1.0.1
+5 -5
View File
@@ -1,8 +1,8 @@
/* /*
* @Author: Vincent Yang * @Author: Vincent Yang
* @Date: 2023-07-01 21:45:34 * @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Young * @LastEditors: Vincent Yang
* @LastEditTime: 2024-09-16 12:12:35 * @LastEditTime: 2024-11-01 13:04:50
* @FilePath: /DeepLX/main.go * @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@@ -131,7 +131,7 @@ func main() {
return return
} }
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL) result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, "")
if err != nil { if err != nil {
log.Fatalf("Translation failed: %s", err) log.Fatalf("Translation failed: %s", err)
} }
@@ -195,7 +195,7 @@ func main() {
return return
} }
result, err := translate.TranslateByDeepLXPro(sourceLang, targetLang, translateText, tagHandling, dlSession, proxyURL) result, err := translate.TranslateByDeepLX(sourceLang, targetLang, translateText, tagHandling, proxyURL, dlSession)
if err != nil { if err != nil {
log.Fatalf("Translation failed: %s", err) log.Fatalf("Translation failed: %s", err)
} }
@@ -247,7 +247,7 @@ func main() {
targetLang = jsonData.TargetLang targetLang = jsonData.TargetLang
} }
result, err := translate.TranslateByDeepLX("", targetLang, translateText, "", proxyURL) result, err := translate.TranslateByDeepLX("", targetLang, translateText, "", proxyURL, "")
if err != nil { if err != nil {
log.Fatalf("Translation failed: %s", err) log.Fatalf("Translation failed: %s", err)
} }
+229 -318
View File
@@ -1,8 +1,8 @@
/* /*
* @Author: Vincent Young * @Author: Vincent Young
* @Date: 2024-09-16 11:59:24 * @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Young * @LastEditors: Vincent Yang
* @LastEditTime: 2024-09-16 12:09:37 * @LastEditTime: 2024-11-01 13:12:25
* @FilePath: /DeepLX/translate/translate.go * @FilePath: /DeepLX/translate/translate.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@@ -14,354 +14,265 @@ package translate
import ( import (
"bytes" "bytes"
"encoding/json" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"github.com/abadojack/whatlanggo"
"github.com/andybalholm/brotli" "github.com/andybalholm/brotli"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
func initDeepLXData(sourceLang string, targetLang string) *PostData { const baseURL = "https://www2.deepl.com/jsonrpc"
// makeRequest makes an HTTP request to DeepL API
func makeRequest(postData *PostData, urlMethod string, proxyURL string, dlSession string) (gjson.Result, error) {
urlFull := fmt.Sprintf("%s?client=chrome-extension,1.28.0&method=%s", baseURL, urlMethod)
postStr := formatPostString(postData)
req, err := http.NewRequest("POST", urlFull, bytes.NewReader([]byte(postStr)))
if err != nil {
return gjson.Result{}, err
}
if dlSession != "" {
req.Header = http.Header{
"Accept": []string{"*/*"},
"Accept-Language": []string{"en-US,en;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7,zh-HK;q=0.6,zh;q=0.5"},
"Authorization": []string{"None"},
"Cache-Control": []string{"no-cache"},
"Content-Type": []string{"application/json"},
"DNT": []string{"1"},
"Origin": []string{"chrome-extension://cofdbpoegempjloogbagkncekinflcnj"},
"Pragma": []string{"no-cache"},
"Priority": []string{"u=1, i"},
"Referer": []string{"https://www.deepl.com/"},
"Sec-Fetch-Dest": []string{"empty"},
"Sec-Fetch-Mode": []string{"cors"},
"Sec-Fetch-Site": []string{"none"},
"Sec-GPC": []string{"1"},
"User-Agent": []string{"DeepLBrowserExtension/1.28.0 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"},
"Cookie": []string{"dl_session=" + dlSession},
}
} else {
req.Header = http.Header{
"Accept": []string{"*/*"},
"Accept-Language": []string{"en-US,en;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7,zh-HK;q=0.6,zh;q=0.5"},
"Authorization": []string{"None"},
"Cache-Control": []string{"no-cache"},
"Content-Type": []string{"application/json"},
"DNT": []string{"1"},
"Origin": []string{"chrome-extension://cofdbpoegempjloogbagkncekinflcnj"},
"Pragma": []string{"no-cache"},
"Priority": []string{"u=1, i"},
"Referer": []string{"https://www.deepl.com/"},
"Sec-Fetch-Dest": []string{"empty"},
"Sec-Fetch-Mode": []string{"cors"},
"Sec-Fetch-Site": []string{"none"},
"Sec-GPC": []string{"1"},
"User-Agent": []string{"DeepLBrowserExtension/1.28.0 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"},
}
}
// Setup client with proxy if provided
var client *http.Client
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
return gjson.Result{}, err
}
client = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
} else {
client = &http.Client{}
}
resp, err := client.Do(req)
if err != nil {
return gjson.Result{}, err
}
defer resp.Body.Close()
// Check status code before processing response
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusTooManyRequests:
return gjson.Result{}, fmt.Errorf("too many requests")
case http.StatusUnauthorized:
return gjson.Result{}, fmt.Errorf("unauthorized")
case http.StatusForbidden:
return gjson.Result{}, fmt.Errorf("forbidden")
default:
return gjson.Result{}, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}
var bodyReader io.Reader
if resp.Header.Get("Content-Encoding") == "br" {
bodyReader = brotli.NewReader(resp.Body)
} else {
bodyReader = resp.Body
}
body, err := io.ReadAll(bodyReader)
if err != nil {
return gjson.Result{}, err
}
return gjson.ParseBytes(body), nil
}
// splitText splits the input text for translation
func splitText(text string, tagHandling bool, proxyURL string, dlSession string) (gjson.Result, error) {
postData := &PostData{
Jsonrpc: "2.0",
Method: "LMT_split_text",
ID: getRandomNumber(),
Params: Params{
CommonJobParams: CommonJobParams{
Mode: "translate",
},
Lang: Lang{
LangUserSelected: "auto",
},
Texts: []string{text},
TextType: map[bool]string{true: "richtext", false: "plaintext"}[tagHandling || isRichText(text)],
},
}
return makeRequest(postData, "LMT_split_text", proxyURL, dlSession)
}
// TranslateByDeepLX performs translation using DeepL API
func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string, proxyURL string, dlSession string) (DeepLXTranslationResult, error) {
if text == "" {
return DeepLXTranslationResult{
Code: http.StatusNotFound,
Message: "No text to translate",
}, nil
}
// Split text first
splitResult, err := splitText(text, tagHandling == "html" || tagHandling == "xml", proxyURL, dlSession)
if err != nil {
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: err.Error(),
}, nil
}
// Get detected language if source language is auto
if sourceLang == "auto" || sourceLang == "" {
sourceLang = strings.ToLower(splitResult.Get("result.lang.detected").String())
}
// Prepare jobs from split result
var jobs []Job
chunks := splitResult.Get("result.texts.0.chunks").Array()
for idx, chunk := range chunks {
sentence := chunk.Get("sentences.0")
// Handle context
contextBefore := []string{}
contextAfter := []string{}
if idx > 0 {
contextBefore = []string{chunks[idx-1].Get("sentences.0.text").String()}
}
if idx < len(chunks)-1 {
contextAfter = []string{chunks[idx+1].Get("sentences.0.text").String()}
}
jobs = append(jobs, Job{
Kind: "default",
PreferredNumBeams: 4,
RawEnContextBefore: contextBefore,
RawEnContextAfter: contextAfter,
Sentences: []Sentence{{
Prefix: sentence.Get("prefix").String(),
Text: sentence.Get("text").String(),
ID: idx + 1,
}},
})
}
hasRegionalVariant := false hasRegionalVariant := false
targetLangParts := strings.Split(targetLang, "-") targetLangParts := strings.Split(targetLang, "-")
// targetLang can be "en", "pt", "pt-PT", "pt-BR"
// targetLangCode is the first part of the targetLang, e.g. "pt" in "pt-PT"
targetLangCode := targetLangParts[0] targetLangCode := targetLangParts[0]
if len(targetLangParts) > 1 { if len(targetLangParts) > 1 {
hasRegionalVariant = true hasRegionalVariant = true
} }
commonJobParams := CommonJobParams{ // Prepare translation request
WasSpoken: false, id := getRandomNumber()
TranscribeAS: "", postData := &PostData{
}
if hasRegionalVariant {
commonJobParams.RegionalVariant = targetLang
}
return &PostData{
Jsonrpc: "2.0", Jsonrpc: "2.0",
Method: "LMT_handle_texts", Method: "LMT_handle_jobs",
ID: id,
Params: Params{ Params: Params{
Splitting: "newlines", CommonJobParams: CommonJobParams{
Lang: Lang{ Mode: "translate",
SourceLangUserSelected: sourceLang, RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
TargetLang: targetLangCode,
}, },
CommonJobParams: commonJobParams, Lang: Lang{
SourceLangComputed: strings.ToUpper(sourceLang),
TargetLang: strings.ToUpper(targetLangCode),
},
Jobs: jobs,
Priority: 1,
Timestamp: getTimeStamp(getICount(text)),
}, },
} }
}
func TranslateByDeepLX(sourceLang string, targetLang string, translateText string, tagHandling string, proxyURL string) (DeepLXTranslationResult, error) {
id := getRandomNumber()
if sourceLang == "" {
lang := whatlanggo.DetectLang(translateText)
deepLLang := strings.ToUpper(lang.Iso6391())
sourceLang = deepLLang
}
// If target language is not specified, set it to English
if targetLang == "" {
targetLang = "EN"
}
// Handling empty translation text
if translateText == "" {
return DeepLXTranslationResult{
Code: http.StatusNotFound,
Message: "No text to translate",
}, nil
}
// Preparing the request data for the DeepL API
www2URL := "https://www2.deepl.com/jsonrpc"
id = id + 1
postData := initDeepLXData(sourceLang, targetLang)
text := Text{
Text: translateText,
RequestAlternatives: 3,
}
postData.ID = id
postData.Params.Texts = append(postData.Params.Texts, text)
postData.Params.Timestamp = getTimeStamp(getICount(translateText))
if tagHandling == "html" || tagHandling == "xml" {
postData.Params.TagHandling = tagHandling
}
// Marshalling the request data to JSON and making necessary string replacements
post_byte, _ := json.Marshal(postData)
postStr := string(post_byte)
// Adding spaces to the JSON string based on the ID to adhere to DeepL's request formatting rules
if (id+5)%29 == 0 || (id+3)%13 == 0 {
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\" : \"", -1)
} else {
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\": \"", -1)
}
// Creating a new HTTP POST request with the JSON data as the body
post_byte = []byte(postStr)
reader := bytes.NewReader(post_byte)
request, err := http.NewRequest("POST", www2URL, reader)
// Make translation request
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)
if err != nil { if err != nil {
log.Println(err)
return DeepLXTranslationResult{ return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable, Code: http.StatusServiceUnavailable,
Message: "Post request failed", Message: err.Error(),
}, nil
}
// Setting HTTP headers to mimic a request from the DeepL iOS App
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "*/*")
request.Header.Set("x-app-os-name", "iOS")
request.Header.Set("x-app-os-version", "16.3.0")
request.Header.Set("Accept-Language", "en-US,en;q=0.9")
request.Header.Set("Accept-Encoding", "gzip, deflate, br")
request.Header.Set("x-app-device", "iPhone13,2")
request.Header.Set("User-Agent", "DeepL-iOS/2.9.1 iOS 16.3.0 (iPhone13,2)")
request.Header.Set("x-app-build", "510265")
request.Header.Set("x-app-version", "2.9.1")
request.Header.Set("Connection", "keep-alive")
// Making the HTTP request to the DeepL API
var client *http.Client
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: "Uknown error",
}, nil
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxy),
}
client = &http.Client{Transport: transport}
} else {
client = &http.Client{}
}
resp, err := client.Do(request)
if err != nil {
log.Println(err)
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: "DeepL API request failed",
}, nil
}
defer resp.Body.Close()
// Handling potential Brotli compressed response body
var bodyReader io.Reader
switch resp.Header.Get("Content-Encoding") {
case "br":
bodyReader = brotli.NewReader(resp.Body)
default:
bodyReader = resp.Body
}
// Reading the response body and parsing it with gjson
body, _ := io.ReadAll(bodyReader)
// body, _ := io.ReadAll(resp.Body)
res := gjson.ParseBytes(body)
// Handling various response statuses and potential errors
if res.Get("error.code").String() == "-32600" {
log.Println(res.Get("error").String())
return DeepLXTranslationResult{
Code: http.StatusNotAcceptable,
Message: "Invalid target language",
}, nil
}
if resp.StatusCode == http.StatusTooManyRequests {
return DeepLXTranslationResult{
Code: http.StatusTooManyRequests,
Message: "Too Many Requests",
}, nil }, nil
} }
// Process translation results
var alternatives []string var alternatives []string
res.Get("result.texts.0.alternatives").ForEach(func(key, value gjson.Result) bool { var translatedText string
alternatives = append(alternatives, value.Get("text").String())
return true translations := result.Get("result.translations").Array()
}) if len(translations) > 0 {
if res.Get("result.texts.0.text").String() == "" { // Get alternatives
numBeams := len(translations[0].Get("beams").Array())
for i := 0; i < numBeams; i++ {
var altText string
for _, translation := range translations {
beams := translation.Get("beams").Array()
if i < len(beams) {
altText += beams[i].Get("sentences.0.text").String()
}
}
if altText != "" {
alternatives = append(alternatives, altText)
}
}
// Get main translation
for _, translation := range translations {
translatedText += translation.Get("beams.0.sentences.0.text").String() + " "
}
translatedText = strings.TrimSpace(translatedText)
}
if translatedText == "" {
return DeepLXTranslationResult{ return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable, Code: http.StatusServiceUnavailable,
Message: "Translation failed, API returns an empty result.", Message: "Translation failed",
}, nil
} else {
return DeepLXTranslationResult{
Code: http.StatusOK,
ID: id,
Message: "Success",
Data: res.Get("result.texts.0.text").String(),
Alternatives: alternatives,
SourceLang: sourceLang,
TargetLang: targetLang,
Method: "Free",
}, nil }, nil
} }
}
func TranslateByDeepLXPro(sourceLang string, targetLang string, translateText string, tagHandling string, dlSession string, proxyURL string) (DeepLXTranslationResult, error) { return DeepLXTranslationResult{
id := getRandomNumber() Code: http.StatusOK,
if sourceLang == "" { ID: id,
lang := whatlanggo.DetectLang(translateText) Data: translatedText,
deepLLang := strings.ToUpper(lang.Iso6391()) Alternatives: alternatives,
sourceLang = deepLLang SourceLang: sourceLang,
} TargetLang: targetLang,
// If target language is not specified, set it to English Method: map[bool]string{true: "Pro", false: "Free"}[dlSession != ""],
if targetLang == "" { }, nil
targetLang = "EN"
}
// Handling empty translation text
if translateText == "" {
return DeepLXTranslationResult{
Code: http.StatusNotFound,
Message: "No text to translate",
}, nil
}
// Preparing the request data for the DeepL API
proURL := "https://api.deepl.com/jsonrpc"
id = id + 1
postData := initDeepLXData(sourceLang, targetLang)
text := Text{
Text: translateText,
RequestAlternatives: 3,
}
postData.ID = id
postData.Params.Texts = append(postData.Params.Texts, text)
postData.Params.Timestamp = getTimeStamp(getICount(translateText))
if tagHandling == "html" || tagHandling == "xml" {
postData.Params.TagHandling = tagHandling
}
// Marshalling the request data to JSON and making necessary string replacements
post_byte, _ := json.Marshal(postData)
postStr := string(post_byte)
// Adding spaces to the JSON string based on the ID to adhere to DeepL's request formatting rules
if (id+5)%29 == 0 || (id+3)%13 == 0 {
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\" : \"", -1)
} else {
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\": \"", -1)
}
// Creating a new HTTP POST request with the JSON data as the body
post_byte = []byte(postStr)
reader := bytes.NewReader(post_byte)
request, err := http.NewRequest("POST", proURL, reader)
if err != nil {
log.Println(err)
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: "Post request failed",
}, nil
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "*/*")
request.Header.Set("Accept-Language", "en-US,en;q=0.9")
request.Header.Set("Accept-Encoding", "gzip, deflate, br")
request.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36")
request.Header.Set("Origin", "https://www.deepl.com")
request.Header.Set("Referer", "https://www.deepl.com")
request.Header.Set("Connection", "keep-alive")
request.Header.Set("Cookie", "dl_session="+dlSession)
// Making the HTTP request to the DeepL API
var client *http.Client
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: "DeepL API request failed",
}, nil
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxy),
}
client = &http.Client{Transport: transport}
} else {
client = &http.Client{}
}
resp, err := client.Do(request)
if err != nil {
log.Println(err)
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: "DeepL API request failed",
}, nil
}
defer resp.Body.Close()
// Handling potential Brotli compressed response body
var bodyReader io.Reader
switch resp.Header.Get("Content-Encoding") {
case "br":
bodyReader = brotli.NewReader(resp.Body)
default:
bodyReader = resp.Body
}
// Reading the response body and parsing it with gjson
body, _ := io.ReadAll(bodyReader)
// body, _ := io.ReadAll(resp.Body)
res := gjson.ParseBytes(body)
if res.Get("error.code").String() == "-32600" {
log.Println(res.Get("error").String())
return DeepLXTranslationResult{
Code: http.StatusNotAcceptable,
Message: "Invalid target language",
}, nil
}
if resp.StatusCode == http.StatusTooManyRequests {
return DeepLXTranslationResult{
Code: http.StatusTooManyRequests,
Message: "Too Many Requests",
}, nil
} else if resp.StatusCode == http.StatusUnauthorized {
return DeepLXTranslationResult{
Code: http.StatusUnauthorized,
Message: "dlsession is invalid",
}, nil
} else {
var alternatives []string
res.Get("result.texts.0.alternatives").ForEach(func(key, value gjson.Result) bool {
alternatives = append(alternatives, value.Get("text").String())
return true
})
if res.Get("result.texts.0.text").String() == "" {
return DeepLXTranslationResult{
Code: http.StatusServiceUnavailable,
Message: "Translation failed, API returns an empty result.",
}, nil
} else {
return DeepLXTranslationResult{
Code: http.StatusOK,
ID: id,
Message: "Success",
Data: res.Get("result.texts.0.text").String(),
Alternatives: alternatives,
SourceLang: sourceLang,
TargetLang: targetLang,
Method: "Pro",
}, nil
}
}
} }
+73 -33
View File
@@ -1,8 +1,8 @@
/* /*
* @Author: Vincent Young * @Author: Vincent Young
* @Date: 2024-09-16 11:59:24 * @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Young * @LastEditors: Vincent Yang
* @LastEditTime: 2024-09-16 12:06:36 * @LastEditTime: 2024-11-01 12:47:47
* @FilePath: /DeepLX/translate/types.go * @FilePath: /DeepLX/translate/types.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@@ -12,31 +12,47 @@
package translate package translate
// Lang represents the language settings for translation
type Lang struct { type Lang struct {
SourceLangUserSelected string `json:"source_lang_user_selected"` SourceLangComputed string `json:"source_lang_computed,omitempty"`
TargetLang string `json:"target_lang"` TargetLang string `json:"target_lang"`
LangUserSelected string `json:"lang_user_selected,omitempty"`
} }
// CommonJobParams represents common parameters for translation jobs
type CommonJobParams struct { type CommonJobParams struct {
WasSpoken bool `json:"wasSpoken"` Mode string `json:"mode"`
TranscribeAS string `json:"transcribe_as"` RegionalVariant string `json:"regionalVariant"`
RegionalVariant string `json:"regionalVariant,omitempty"`
} }
// Sentence represents a sentence in the translation request
type Sentence struct {
Prefix string `json:"prefix"`
Text string `json:"text"`
ID int `json:"id"`
}
// Job represents a translation job
type Job struct {
Kind string `json:"kind"`
PreferredNumBeams int `json:"preferred_num_beams"`
RawEnContextBefore []string `json:"raw_en_context_before"`
RawEnContextAfter []string `json:"raw_en_context_after"`
Sentences []Sentence `json:"sentences"`
}
// Params represents parameters for translation requests
type Params struct { type Params struct {
Texts []Text `json:"texts"`
Splitting string `json:"splitting"`
Lang Lang `json:"lang"`
Timestamp int64 `json:"timestamp"`
CommonJobParams CommonJobParams `json:"commonJobParams"` CommonJobParams CommonJobParams `json:"commonJobParams"`
TagHandling string `json:"tag_handling"` Lang Lang `json:"lang"`
} Texts []string `json:"texts,omitempty"`
TextType string `json:"textType,omitempty"`
type Text struct { Jobs []Job `json:"jobs,omitempty"`
Text string `json:"text"` Priority int `json:"priority,omitempty"`
RequestAlternatives int `json:"requestAlternatives"` Timestamp int64 `json:"timestamp"`
} }
// PostData represents the complete translation request
type PostData struct { type PostData struct {
Jsonrpc string `json:"jsonrpc"` Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"` Method string `json:"method"`
@@ -44,26 +60,50 @@ type PostData struct {
Params Params `json:"params"` Params Params `json:"params"`
} }
type Translation struct { // SplitTextResponse represents the response from text splitting
Text string `json:"text"` type SplitTextResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID int64 `json:"id"`
Result struct {
Lang struct {
Detected string `json:"detected"`
} `json:"lang"`
Texts []struct {
Chunks []struct {
Sentences []struct {
Prefix string `json:"prefix"`
Text string `json:"text"`
} `json:"sentences"`
} `json:"chunks"`
} `json:"texts"`
} `json:"result"`
} }
// TranslationResponse represents the response from translation
type TranslationResponse struct { type TranslationResponse struct {
Translations []Translation `json:"translations"` Jsonrpc string `json:"jsonrpc"`
} ID int64 `json:"id"`
Result struct {
type DeepLUsageResponse struct { Translations []struct {
CharacterCount int `json:"character_count"` Beams []struct {
CharacterLimit int `json:"character_limit"` Sentences []struct {
Text string `json:"text"`
} `json:"sentences"`
} `json:"beams"`
} `json:"translations"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
} `json:"result"`
} }
// DeepLXTranslationResult represents the final translation result
type DeepLXTranslationResult struct { type DeepLXTranslationResult struct {
Code int Code int `json:"code"`
ID int64 ID int64 `json:"id"`
Message string Message string `json:"message,omitempty"`
Data string Data string `json:"data"`
Alternatives []string Alternatives []string `json:"alternatives"`
SourceLang string SourceLang string `json:"source_lang"`
TargetLang string TargetLang string `json:"target_lang"`
Method string Method string `json:"method"`
} }
+26 -4
View File
@@ -1,8 +1,8 @@
/* /*
* @Author: Vincent Young * @Author: Vincent Young
* @Date: 2024-09-16 11:59:24 * @Date: 2024-09-16 11:59:24
* @LastEditors: Vincent Young * @LastEditors: Vincent Yang
* @LastEditTime: 2024-09-16 12:06:44 * @LastEditTime: 2024-11-01 00:39:32
* @FilePath: /DeepLX/translate/utils.go * @FilePath: /DeepLX/translate/utils.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo * @GitHub: https://github.com/missuo
@@ -13,15 +13,18 @@
package translate package translate
import ( import (
"encoding/json"
"math/rand" "math/rand"
"strings" "strings"
"time" "time"
) )
// getICount returns the number of 'i' characters in the text
func getICount(translateText string) int64 { func getICount(translateText string) int64 {
return int64(strings.Count(translateText, "i")) return int64(strings.Count(translateText, "i"))
} }
// getRandomNumber generates a random number for request ID
func getRandomNumber() int64 { func getRandomNumber() int64 {
src := rand.NewSource(time.Now().UnixNano()) src := rand.NewSource(time.Now().UnixNano())
rng := rand.New(src) rng := rand.New(src)
@@ -29,12 +32,31 @@ func getRandomNumber() int64 {
return num * 1000 return num * 1000
} }
// getTimeStamp generates timestamp for request based on i count
func getTimeStamp(iCount int64) int64 { func getTimeStamp(iCount int64) int64 {
ts := time.Now().UnixMilli() ts := time.Now().UnixMilli()
if iCount != 0 { if iCount != 0 {
iCount = iCount + 1 iCount = iCount + 1
return ts - ts%iCount + iCount return ts - ts%iCount + iCount
} else {
return ts
} }
return ts
}
// formatPostString formats the request JSON string with specific spacing rules
func formatPostString(postData *PostData) string {
postBytes, _ := json.Marshal(postData)
postStr := string(postBytes)
if (postData.ID+5)%29 == 0 || (postData.ID+3)%13 == 0 {
postStr = strings.Replace(postStr, `"method":"`, `"method" : "`, 1)
} else {
postStr = strings.Replace(postStr, `"method":"`, `"method": "`, 1)
}
return postStr
}
// isRichText checks if text contains HTML-like tags
func isRichText(text string) bool {
return strings.Contains(text, "<") && strings.Contains(text, ">")
} }