feat: support tag_handling

This commit is contained in:
Vincent Yang 2024-09-15 02:04:53 -04:00
parent 0d73552773
commit e9b5dedc43
No known key found for this signature in database
GPG Key ID: 55F1635E821BF0E8
3 changed files with 43 additions and 13 deletions

27
main.go
View File

@ -2,7 +2,7 @@
* @Author: Vincent Yang
* @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-06-18 02:41:55
* @LastEditTime: 2024-09-15 02:00:23
* @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
@ -106,10 +106,20 @@ func main() {
sourceLang := req.SourceLang
targetLang := req.TargetLang
translateText := req.TransText
tagHandling := req.TagHandling
authKey := cfg.AuthKey
proxyURL := cfg.Proxy
result, err := translateByDeepLX(sourceLang, targetLang, translateText, authKey, proxyURL)
if tagHandling != "" && tagHandling != "html" && tagHandling != "xml" {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid tag_handling value. Allowed values are 'html' and 'xml'.",
})
return
}
result, err := translateByDeepLX(sourceLang, targetLang, translateText, tagHandling, authKey, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}
@ -141,10 +151,19 @@ func main() {
sourceLang := req.SourceLang
targetLang := req.TargetLang
translateText := req.TransText
tagHandling := req.TagHandling
proxyURL := cfg.Proxy
dlSession := cfg.DlSession
if tagHandling != "" && tagHandling != "html" && tagHandling != "xml" {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid tag_handling value. Allowed values are 'html' and 'xml'.",
})
return
}
cookie := c.GetHeader("Cookie")
if cookie != "" {
dlSession = strings.Replace(cookie, "dl_session=", "", -1)
@ -164,7 +183,7 @@ func main() {
return
}
result, err := translateByDeepLXPro(sourceLang, targetLang, translateText, dlSession, proxyURL)
result, err := translateByDeepLXPro(sourceLang, targetLang, translateText, tagHandling, dlSession, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}
@ -225,7 +244,7 @@ func main() {
targetLang = jsonData.TargetLang
}
result, err := translateByDeepLX("", targetLang, translateText, authKey, proxyURL)
result, err := translateByDeepLX("", targetLang, translateText, "", authKey, proxyURL)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}

View File

@ -111,7 +111,7 @@ func translateByOfficialAPI(text string, sourceLang string, targetLang string, a
return sb.String(), nil
}
func translateByDeepLX(sourceLang string, targetLang string, translateText string, authKey string, proxyURL string) (DeepLXTranslationResult, error) {
func translateByDeepLX(sourceLang string, targetLang string, translateText string, tagHandling string, authKey string, proxyURL string) (DeepLXTranslationResult, error) {
id := getRandomNumber()
if sourceLang == "" {
lang := whatlanggo.DetectLang(translateText)
@ -142,6 +142,10 @@ func translateByDeepLX(sourceLang string, targetLang string, translateText strin
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)
@ -288,7 +292,7 @@ func translateByDeepLX(sourceLang string, targetLang string, translateText strin
}, nil
}
func translateByDeepLXPro(sourceLang string, targetLang string, translateText string, dlSession string, proxyURL string) (DeepLXTranslationResult, error) {
func translateByDeepLXPro(sourceLang string, targetLang string, translateText string, tagHandling string, dlSession string, proxyURL string) (DeepLXTranslationResult, error) {
id := getRandomNumber()
if sourceLang == "" {
lang := whatlanggo.DetectLang(translateText)
@ -319,6 +323,10 @@ func translateByDeepLXPro(sourceLang string, targetLang string, translateText st
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)

View File

@ -2,7 +2,7 @@
* @Author: Vincent Yang
* @Date: 2024-03-20 15:43:57
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-06-18 02:38:48
* @LastEditTime: 2024-09-15 01:54:05
* @FilePath: /DeepLX/types.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
@ -37,6 +37,7 @@ type Params struct {
Lang Lang `json:"lang"`
Timestamp int64 `json:"timestamp"`
CommonJobParams CommonJobParams `json:"commonJobParams"`
TagHandling string `json:"tag_handling"`
}
type Text struct {
@ -52,15 +53,17 @@ type PostData struct {
}
type PayloadFree struct {
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
TagHandling string `json:"tag_handling"`
}
type PayloadAPI struct {
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"`
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"`
TagHandling string `json:"tag_handling"`
}
type Translation struct {