mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2025-04-19 06:03:25 +00:00
feat: result returns source and target lang. #64
This commit is contained in:
parent
eb6afa8106
commit
6d1a78933a
210
main.go
210
main.go
@ -2,7 +2,7 @@
|
|||||||
* @Author: Vincent Young
|
* @Author: Vincent Young
|
||||||
* @Date: 2023-07-01 21:45:34
|
* @Date: 2023-07-01 21:45:34
|
||||||
* @LastEditors: Vincent Young
|
* @LastEditors: Vincent Young
|
||||||
* @LastEditTime: 2023-07-03 00:50:36
|
* @LastEditTime: 2023-09-14 13:34:42
|
||||||
* @FilePath: /DeepLX/main.go
|
* @FilePath: /DeepLX/main.go
|
||||||
* @Telegram: https://t.me/missuo
|
* @Telegram: https://t.me/missuo
|
||||||
*
|
*
|
||||||
@ -122,152 +122,170 @@ type ResData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// parse flags
|
// Parsing the command-line flags
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// display information
|
// Displaying initialization information
|
||||||
fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", port)
|
fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", port)
|
||||||
fmt.Println("Made by sjlleo and missuo.")
|
fmt.Println("Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>.")
|
||||||
|
|
||||||
// create a random id
|
// Generating a random ID
|
||||||
id := getRandomNumber()
|
id := getRandomNumber()
|
||||||
|
|
||||||
// set release mode
|
// Setting the application to release mode
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.Use(cors.Default())
|
r.Use(cors.Default())
|
||||||
|
|
||||||
|
// Defining the root endpoint which returns the project details
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/", func(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"code": 200,
|
"code": 200,
|
||||||
"message": "DeepL Free API, Made by sjlleo and missuo. Go to /translate with POST. http://github.com/OwO-Network/DeepLX",
|
"message": "DeepL Free API, Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>. Go to /translate with POST. http://github.com/OwO-Network/DeepLX",
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Defining the translation endpoint which receives translation requests and returns translations
|
||||||
r.POST("/translate", func(c *gin.Context) {
|
r.POST("/translate", func(c *gin.Context) {
|
||||||
reqj := ResData{}
|
reqj := ResData{}
|
||||||
c.BindJSON(&reqj)
|
c.BindJSON(&reqj)
|
||||||
|
|
||||||
|
// Extracting details from the request JSON
|
||||||
sourceLang := reqj.SourceLang
|
sourceLang := reqj.SourceLang
|
||||||
targetLang := reqj.TargetLang
|
targetLang := reqj.TargetLang
|
||||||
translateText := reqj.TransText
|
translateText := reqj.TransText
|
||||||
|
|
||||||
|
// If source language is not specified, auto-detect it
|
||||||
if sourceLang == "" {
|
if sourceLang == "" {
|
||||||
lang := whatlanggo.DetectLang(translateText)
|
lang := whatlanggo.DetectLang(translateText)
|
||||||
deepLLang := strings.ToUpper(lang.Iso6391())
|
deepLLang := strings.ToUpper(lang.Iso6391())
|
||||||
sourceLang = deepLLang
|
sourceLang = deepLLang
|
||||||
}
|
}
|
||||||
|
// If target language is not specified, set it to English
|
||||||
if targetLang == "" {
|
if targetLang == "" {
|
||||||
targetLang = "EN"
|
targetLang = "EN"
|
||||||
}
|
}
|
||||||
|
// Handling empty translation text
|
||||||
if translateText == "" {
|
if translateText == "" {
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
"code": http.StatusNotFound,
|
"code": http.StatusNotFound,
|
||||||
"message": "No Translate Text Found",
|
"message": "No Translate Text Found",
|
||||||
})
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Preparing the request data for the DeepL API
|
||||||
|
url := "https://www2.deepl.com/jsonrpc"
|
||||||
|
id = id + 1
|
||||||
|
postData := initData(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))
|
||||||
|
|
||||||
|
// 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 {
|
} else {
|
||||||
url := "https://www2.deepl.com/jsonrpc"
|
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\": \"", -1)
|
||||||
id = id + 1
|
}
|
||||||
postData := initData(sourceLang, targetLang)
|
|
||||||
text := Text{
|
|
||||||
Text: translateText,
|
|
||||||
RequestAlternatives: 3,
|
|
||||||
}
|
|
||||||
// set id
|
|
||||||
postData.ID = id
|
|
||||||
// set text
|
|
||||||
postData.Params.Texts = append(postData.Params.Texts, text)
|
|
||||||
// set timestamp
|
|
||||||
postData.Params.Timestamp = getTimeStamp(getICount(translateText))
|
|
||||||
post_byte, _ := json.Marshal(postData)
|
|
||||||
postStr := string(post_byte)
|
|
||||||
|
|
||||||
// add space if necessary
|
// Creating a new HTTP POST request with the JSON data as the body
|
||||||
if (id+5)%29 == 0 || (id+3)%13 == 0 {
|
post_byte = []byte(postStr)
|
||||||
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\" : \"", -1)
|
reader := bytes.NewReader(post_byte)
|
||||||
} else {
|
request, err := http.NewRequest("POST", url, reader)
|
||||||
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\": \"", -1)
|
if err != nil {
|
||||||
}
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
post_byte = []byte(postStr)
|
// Setting HTTP headers to mimic a request from the DeepL iOS App
|
||||||
reader := bytes.NewReader(post_byte)
|
request.Header.Set("Content-Type", "application/json")
|
||||||
request, err := http.NewRequest("POST", url, reader)
|
request.Header.Set("Accept", "*/*")
|
||||||
if err != nil {
|
request.Header.Set("x-app-os-name", "iOS")
|
||||||
log.Println(err)
|
request.Header.Set("x-app-os-version", "16.3.0")
|
||||||
return
|
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")
|
||||||
|
|
||||||
// Set Headers
|
// Making the HTTP request to the DeepL API
|
||||||
request.Header.Set("Content-Type", "application/json")
|
client := &http.Client{}
|
||||||
request.Header.Set("Accept", "*/*")
|
resp, err := client.Do(request)
|
||||||
request.Header.Set("x-app-os-name", "iOS")
|
if err != nil {
|
||||||
request.Header.Set("x-app-os-version", "16.3.0")
|
log.Println(err)
|
||||||
request.Header.Set("Accept-Language", "en-US,en;q=0.9")
|
return
|
||||||
request.Header.Set("Accept-Encoding", "gzip, deflate, br")
|
}
|
||||||
request.Header.Set("x-app-device", "iPhone13,2")
|
defer resp.Body.Close()
|
||||||
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")
|
|
||||||
|
|
||||||
client := &http.Client{}
|
// Handling potential Brotli compressed response body
|
||||||
resp, err := client.Do(request)
|
var bodyReader io.Reader
|
||||||
if err != nil {
|
switch resp.Header.Get("Content-Encoding") {
|
||||||
log.Println(err)
|
case "br":
|
||||||
return
|
bodyReader = brotli.NewReader(resp.Body)
|
||||||
}
|
default:
|
||||||
defer resp.Body.Close()
|
bodyReader = resp.Body
|
||||||
|
}
|
||||||
|
|
||||||
var bodyReader io.Reader
|
// Reading the response body and parsing it with gjson
|
||||||
switch resp.Header.Get("Content-Encoding") {
|
body, err := io.ReadAll(bodyReader)
|
||||||
case "br":
|
// body, _ := io.ReadAll(resp.Body)
|
||||||
bodyReader = brotli.NewReader(resp.Body)
|
res := gjson.ParseBytes(body)
|
||||||
default:
|
|
||||||
bodyReader = resp.Body
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := io.ReadAll(bodyReader)
|
// Handling various response statuses and potential errors
|
||||||
|
if res.Get("error.code").String() == "-32600" {
|
||||||
|
log.Println(res.Get("error").String())
|
||||||
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
|
"code": http.StatusNotAcceptable,
|
||||||
|
"message": "Invalid targetLang",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// body, _ := io.ReadAll(resp.Body)
|
if resp.StatusCode == http.StatusTooManyRequests {
|
||||||
res := gjson.ParseBytes(body)
|
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||||
|
"code": http.StatusTooManyRequests,
|
||||||
// display response
|
"message": "Too Many Requests",
|
||||||
// fmt.Println(res)
|
})
|
||||||
if res.Get("error.code").String() == "-32600" {
|
} else {
|
||||||
log.Println(res.Get("error").String())
|
var alternatives []string
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
res.Get("result.texts.0.alternatives").ForEach(func(key, value gjson.Result) bool {
|
||||||
"code": http.StatusNotAcceptable,
|
alternatives = append(alternatives, value.Get("text").String())
|
||||||
"message": "Invalid targetLang",
|
return true
|
||||||
})
|
})
|
||||||
return
|
c.JSON(http.StatusOK, gin.H{
|
||||||
}
|
"code": http.StatusOK,
|
||||||
|
"id": id,
|
||||||
if resp.StatusCode == http.StatusTooManyRequests {
|
"data": res.Get("result.texts.0.text").String(),
|
||||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
"alternatives": alternatives,
|
||||||
"code": http.StatusTooManyRequests,
|
"source_lang": sourceLang,
|
||||||
"message": "Too Many Requests",
|
"target_lang": targetLang,
|
||||||
})
|
})
|
||||||
} 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
|
|
||||||
})
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"code": http.StatusOK,
|
|
||||||
"id": id,
|
|
||||||
"data": res.Get("result.texts.0.text").String(),
|
|
||||||
"alternatives": alternatives,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Catch-all route to handle undefined paths
|
||||||
|
r.NoRoute(func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"code": http.StatusNotFound,
|
||||||
|
"message": "Path not found",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Determining which port to run the server on, with a fallback to a default port
|
||||||
envPort, ok := os.LookupEnv("PORT")
|
envPort, ok := os.LookupEnv("PORT")
|
||||||
if ok {
|
if ok {
|
||||||
r.Run(":" + envPort)
|
r.Run(":" + envPort)
|
||||||
} else {
|
} else {
|
||||||
// by default, listen and serve on 0.0.0.0:1188
|
|
||||||
r.Run(fmt.Sprintf(":%v", port))
|
r.Run(fmt.Sprintf(":%v", port))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user