mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
181db37210 | ||
|
|
5c6b8802e4 | ||
|
|
fb0dac1889 | ||
|
|
781467bb69 | ||
|
|
03093a658a | ||
|
|
90fc4b96c6 | ||
|
|
f8b6821fb4 | ||
|
|
1da0f1363b | ||
|
|
6d1a78933a | ||
|
|
5f5c59ca48 | ||
|
|
eb6afa8106 | ||
|
|
a3a964ded1 | ||
|
|
fad9fffb6f | ||
|
|
76c929e934 | ||
|
|
ec37935f72 | ||
|
|
83cff7a7ba | ||
|
|
7fc2eb8798 | ||
|
|
a2959a01af | ||
|
|
7bf2bfca21 |
@@ -9,7 +9,7 @@ jobs:
|
|||||||
|
|
||||||
Build:
|
Build:
|
||||||
if: startsWith(github.ref, 'refs/tags/v')
|
if: startsWith(github.ref, 'refs/tags/v')
|
||||||
runs-on: ubuntu-latest
|
runs-on: self-hosted
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ env:
|
|||||||
GHCR_IMAGE_NAME: ${{ github.repository }}
|
GHCR_IMAGE_NAME: ${{ github.repository }}
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
GHCR_TOKEN: ${{ secrets.GT_Token }}
|
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
GHCR_USERNAME: ${{ github.repository_owner }}
|
GHCR_USERNAME: ${{ github.repository_owner }}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
deepl-api
|
DeepLX
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
* @Author: Vincent Young
|
* @Author: Vincent Young
|
||||||
* @Date: 2022-10-18 07:32:29
|
* @Date: 2022-10-18 07:32:29
|
||||||
* @LastEditors: Vincent Young
|
* @LastEditors: Vincent Young
|
||||||
* @LastEditTime: 2023-05-30 01:29:33
|
* @LastEditTime: 2023-10-28 22:45:58
|
||||||
* @FilePath: /DeepLX/README.md
|
* @FilePath: /DeepLX/README.md
|
||||||
* @Telegram: https://t.me/missuo
|
* @Telegram: https://t.me/missuo
|
||||||
*
|
*
|
||||||
@@ -13,9 +13,6 @@
|
|||||||
</h1>
|
</h1>
|
||||||
<h4 align="center">Permanently free DeepL API written in Golang.</h4>
|
<h4 align="center">Permanently free DeepL API written in Golang.</h4>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/OwO-Network/DeepLX/actions">
|
|
||||||
<img src="https://img.shields.io/github/actions/workflow/status/OwO-Network/DeepLX/build.yml?branch=main&style=flat-square" alt="Github Actions">
|
|
||||||
</a>
|
|
||||||
<a href="https://goreportcard.com/report/github.com/OwO-Network/DeepLX">
|
<a href="https://goreportcard.com/report/github.com/OwO-Network/DeepLX">
|
||||||
<img src="https://goreportcard.com/badge/github.com/OwO-Network/DeepLX?style=flat-square">
|
<img src="https://goreportcard.com/badge/github.com/OwO-Network/DeepLX?style=flat-square">
|
||||||
</a>
|
</a>
|
||||||
@@ -24,8 +21,13 @@
|
|||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
## **Python Package**
|
## **Related Projects**
|
||||||
[OwO-Network/PyDeepLX](https://github.com/OwO-Network/PyDeepLX)
|
[OwO-Network/PyDeepLX](https://github.com/OwO-Network/PyDeepLX): Python Package for DeepLX.
|
||||||
|
|
||||||
|
[OwO-Network/gdeeplx](https://github.com/OwO-Network/gdeeplx): Golang Package for DeepLX.
|
||||||
|
|
||||||
|
## Discussion Group
|
||||||
|
[Telegram Group](https://t.me/+8KDGHKJCxEVkNzll)
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
- `DeepLX` is listening to `0.0.0.0:1188` by default. You can modify the listening port by yourself.
|
- `DeepLX` is listening to `0.0.0.0:1188` by default. You can modify the listening port by yourself.
|
||||||
@@ -42,21 +44,40 @@
|
|||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"alternatives": [
|
"alternatives": [
|
||||||
"Undisputed",
|
"no one else",
|
||||||
"Unquestionable",
|
"there is no other person (idiom); there is no one else",
|
||||||
"Unquestionably"
|
"there is no other person"
|
||||||
],
|
],
|
||||||
"code": 200,
|
"code": 200,
|
||||||
"data": "Undoubtedly",
|
"data": "there is no one else",
|
||||||
"id": 8300079001
|
"id": 8352115005,
|
||||||
|
"source_lang": "ZH",
|
||||||
|
"target_lang": "EN"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### Specify the port
|
### Specify the port (Optional)
|
||||||
**Thanks to [cijiugechu](https://github.com/cijiugechu) for [his contribution](https://github.com/OwO-Network/DeepLX/commit/4a0920579ea868b0f05ccdff6bceae316bfd5dc8) to make this feature possible for this project!**
|
**Thanks to [cijiugechu](https://github.com/cijiugechu) for [his contribution](https://github.com/OwO-Network/DeepLX/commit/4a0920579ea868b0f05ccdff6bceae316bfd5dc8) to make this feature possible for this project!**
|
||||||
```bash
|
```bash
|
||||||
./deeplx -p 3333
|
./deeplx -p 3333
|
||||||
# or
|
# or
|
||||||
./deeplx -port 3333
|
./deeplx -port 3333
|
||||||
|
```
|
||||||
|
### Set access password (Optional)
|
||||||
|
**To prevent abuse of your public API, you can use a token to restrict translation requests.**
|
||||||
|
```bash
|
||||||
|
./deeplx -token hellodeeplx
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
curl -X POST http://localhost:1188/translate \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer your_access_token" \
|
||||||
|
-d '{
|
||||||
|
"text": "Hello, world!",
|
||||||
|
"source_lang": "EN",
|
||||||
|
"target_lang": "DE"
|
||||||
|
}'
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Run with Docker
|
### Run with Docker
|
||||||
@@ -83,12 +104,12 @@ docker compose up -d
|
|||||||
```bash
|
```bash
|
||||||
bash <(curl -Ls https://raw.githubusercontent.com/OwO-Network/DeepLX/main/install.sh)
|
bash <(curl -Ls https://raw.githubusercontent.com/OwO-Network/DeepLX/main/install.sh)
|
||||||
# or
|
# or
|
||||||
bash <(curl -Ls https://cpp.li/deeplx)
|
bash <(curl -Ls https://qwq.mx/deeplx)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Run on Mac
|
### Run on Mac
|
||||||
#### Homebrew (Recommended)
|
#### Homebrew (Recommended)
|
||||||
**For unknown reasons v0.7.8 does not work properly with Homebrew, so we are still using v0.7.7 for our version of Homebrew, it has no bugs and it is almost perfect.**
|
**Homebrew has been fixed in the latest version and works perfectly.**
|
||||||
```bash
|
```bash
|
||||||
brew tap owo-network/brew
|
brew tap owo-network/brew
|
||||||
brew install deeplx
|
brew install deeplx
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ go 1.19
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/abadojack/whatlanggo v1.0.1
|
github.com/abadojack/whatlanggo v1.0.1
|
||||||
|
github.com/andybalholm/brotli v1.0.5
|
||||||
github.com/gin-contrib/cors v1.4.0
|
github.com/gin-contrib/cors v1.4.0
|
||||||
github.com/gin-gonic/gin v1.9.1
|
github.com/gin-gonic/gin v1.9.1
|
||||||
github.com/tidwall/gjson v1.14.3
|
github.com/tidwall/gjson v1.14.3
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
|
||||||
github.com/bytedance/sonic v1.9.1 // indirect
|
github.com/bytedance/sonic v1.9.1 // indirect
|
||||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||||
@@ -31,10 +31,10 @@ require (
|
|||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||||
golang.org/x/arch v0.3.0 // indirect
|
golang.org/x/arch v0.3.0 // indirect
|
||||||
golang.org/x/crypto v0.9.0 // indirect
|
golang.org/x/crypto v0.14.0 // indirect
|
||||||
golang.org/x/net v0.10.0 // indirect
|
golang.org/x/net v0.17.0 // indirect
|
||||||
golang.org/x/sys v0.8.0 // indirect
|
golang.org/x/sys v0.13.0 // indirect
|
||||||
golang.org/x/text v0.9.0 // indirect
|
golang.org/x/text v0.13.0 // indirect
|
||||||
google.golang.org/protobuf v1.30.0 // indirect
|
google.golang.org/protobuf v1.30.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -100,24 +100,24 @@ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUu
|
|||||||
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
|
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
|
||||||
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
|
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||||
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
|
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
|
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
|||||||
@@ -1,3 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Vincent Young
|
||||||
|
* @Date: 2023-07-01 21:45:34
|
||||||
|
* @LastEditors: Vincent Young
|
||||||
|
* @LastEditTime: 2023-10-28 22:42:08
|
||||||
|
* @FilePath: /DeepLX/main.go
|
||||||
|
* @Telegram: https://t.me/missuo
|
||||||
|
*
|
||||||
|
* Copyright © 2023 by Vincent, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -9,6 +20,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,6 +32,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var port int
|
var port int
|
||||||
|
var token string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
const (
|
const (
|
||||||
@@ -29,6 +42,7 @@ func init() {
|
|||||||
|
|
||||||
flag.IntVar(&port, "port", defaultPort, usage)
|
flag.IntVar(&port, "port", defaultPort, usage)
|
||||||
flag.IntVar(&port, "p", defaultPort, usage)
|
flag.IntVar(&port, "p", defaultPort, usage)
|
||||||
|
flag.StringVar(&token, "token", "", "set the access token for /translate endpoint")
|
||||||
|
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
}
|
}
|
||||||
@@ -110,146 +124,196 @@ 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
|
// Check if the token is set in the environment variable
|
||||||
|
if token == "" {
|
||||||
|
envToken, ok := os.LookupEnv("TOKEN")
|
||||||
|
if ok {
|
||||||
|
token = envToken
|
||||||
|
fmt.Println("Access token is set from the environment variable.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if token == "" {
|
||||||
|
fmt.Println("Access token is not set. You can set it using the -token flag or the TOKEN environment variable.")
|
||||||
|
} else {
|
||||||
|
fmt.Println("Access token is set. Use the Authorization: Bearer <token> header to access /translate.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
if token != "" {
|
||||||
|
providedToken := c.GetHeader("Authorization")
|
||||||
|
if providedToken != "Bearer "+token {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
|
"code": http.StatusUnauthorized,
|
||||||
|
"message": "Invalid access token",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// by default, listen and serve on 0.0.0.0:1188
|
|
||||||
r.Run(fmt.Sprintf(":%v", port))
|
// 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")
|
||||||
|
if ok {
|
||||||
|
r.Run(":" + envPort)
|
||||||
|
} else {
|
||||||
|
r.Run(fmt.Sprintf(":%v", port))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user