Compare commits

..
11 Commits
Author SHA1 Message Date
Vincent Young ffdba23b03 style: return a uniform variable name 2023-02-25 22:17:13 +08:00
Vincent Young a3778c05fb refactor: rebuild the code format without logic
Merge pull request #7 from Kabochar/main
2023-02-25 22:10:59 +08:00
kabocha 81c94afb55 style(main.go): make it like golang code format 2023-02-25 22:03:51 +08:00
Vincent Young 48151fec8f docs: add a note 2023-02-24 21:37:01 +08:00
Vincent Young 154fb27895 Merge pull request #3 from wuyudi/add-aur
add aur
2023-02-24 15:14:47 +08:00
Vincent Young 08fd328e9d Update README.md 2023-02-20 16:46:48 +08:00
Vincent Young d8fbb7e07e Update README.md 2023-02-18 21:29:55 +08:00
Vincent Young d16d74b3e9 Update README.md 2023-02-18 21:01:05 +08:00
Vincent Young 8b0953e9f7 Update install.sh 2023-02-18 20:57:58 +08:00
Vincent Young 43565812b5 Update deeplx.service 2023-02-18 20:55:20 +08:00
AsukaMinato 7aa936e888 add aur 2023-02-16 21:38:33 +09:00
4 changed files with 90 additions and 67 deletions
+24 -4
View File
@@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2022-10-18 07:32:29
* @LastEditors: Vincent Young
* @LastEditTime: 2023-02-18 19:46:10
* @LastEditTime: 2023-02-24 21:36:36
* @FilePath: /DeepLX/README.md
* @Telegram: https://t.me/missuo
*
@@ -32,10 +32,16 @@ Permanently free DeepL API written in Golang
}
```
### Run on Linux Server
```bash
bash <(curl -Ls https://cpp.li/deeplx)
```
### Run on Mac
1. Download the latest release of DeepL X.
```bash
sudo mv deeplx_darwin_amd64 /usr/local/bin/deeplx
sudo chmod +x /usr/local/bin/deeplx
```
2. Download the `me.missuo.deeplx.plist` to `~/Library/LaunchAgents`.
@@ -48,15 +54,29 @@ launchctl load ~/Library/LaunchAgents/me.missuo.deeplx.plist
launchctl start ~/Library/LaunchAgents/me.missuo.deeplx.plist
```
### Install from AUR
```bash
paru -S deeplx-bin
```
After installation, start the daemon with the following command.
```bash
systemctl daemon-reload
systemctl enable deeplx
```
## Setup on [Bob App](https://bobtranslate.com/)
1. Install [bob-plugin-deeplx](https://github.com/clubxdev/bob-plugin-deeplx) on Bob.
2. Setup the API.
![9a75c26ad6e8bd9b7582c](https://telegraph.eowo.us/file/9a75c26ad6e8bd9b7582c.png)
![c5c19dd89df6fae1a256d](https://missuo.ru/file/c5c19dd89df6fae1a256d.png)
## Docker Backup for zu1k
## Backup the Docker Image of zu1k
```shell
docker run -itd -p 7373:80 missuo/deeplx-bk
docker run -itd -p 1188:80 missuo/deeplx-bk
```
**This docker image is not related to this project, as the original author deleted the image, it is only for backup.**
## Author
**DeepL X** © [Vincent Young](https://github.com/missuo) & [Leo Shen](https://github.com/sjlleo), Released under the [MIT](./LICENSE) License.<br>
+3 -5
View File
@@ -1,14 +1,12 @@
[Unit]
Description=DeepLX Service
After=network.target nss-lookup.target
Wants=network.target
After=network.target
[Service]
User=root
Group=root
Type=simple
Restart=always
WorkingDirectory=/usr/bin/
ExecStart=/usr/bin/deeplx
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
+2 -1
View File
@@ -22,6 +22,7 @@ install_deeplx(){
wget -q -N --no-check-certificate -O /etc/systemd/system/deeplx.service https://raw.githubusercontent.com/OwO-Network/DeepLX/main/deeplx.service
systemctl daemon-reload
systemctl enable deeplx
systemctl start deeplx
echo -e "Installed successfully, listening at 0.0.0.0:1188"
}
install_deeplx
install_deeplx
+61 -57
View File
@@ -15,6 +15,10 @@ import (
"github.com/tidwall/gjson"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
type Lang struct {
SourceLangUserSelected string `json:"source_lang_user_selected"`
TargetLang string `json:"target_lang"`
@@ -46,15 +50,15 @@ type PostData struct {
Params Params `json:"params"`
}
func init_data(source_lang string, target_lang string) *PostData {
func initData(sourceLang string, targetLang string) *PostData {
return &PostData{
Jsonrpc: "2.0",
Method: "LMT_handle_texts",
Params: Params{
Splitting: "newlines",
Lang: Lang{
SourceLangUserSelected: source_lang,
TargetLang: target_lang,
SourceLangUserSelected: sourceLang,
TargetLang: targetLang,
},
CommonJobParams: CommonJobParams{
WasSpoken: false,
@@ -65,8 +69,8 @@ func init_data(source_lang string, target_lang string) *PostData {
}
}
func get_i_count(translate_text string) int64 {
return int64(strings.Count(translate_text, "i"))
func getICount(translateText string) int64 {
return int64(strings.Count(translateText, "i"))
}
func getRandomNumber() int64 {
@@ -75,21 +79,20 @@ func getRandomNumber() int64 {
return num * 1000
}
func getTimeStamp(i_count int64) int64 {
func getTimeStamp(iCount int64) int64 {
ts := time.Now().UnixMilli()
if i_count != 0 {
i_count = i_count + 1
return ts - ts%i_count + i_count
if iCount != 0 {
iCount = iCount + 1
return ts - ts%iCount + iCount
} else {
return ts
}
}
type ResData struct {
Trans_Text string `json:"text"`
Source_Lang string `json:"source_lang"`
Target_Lang string `json:"target_lang"`
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
}
func main() {
@@ -106,8 +109,8 @@ func main() {
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"msg": "DeepL API, Made by sjlleo and missuo. Go to /translate with POST.",
"code": 200,
"message": "DeepL Free API, Made by sjlleo and missuo. Go to /translate with POST. http://github.com/OwO-Network/DeepLX",
})
})
@@ -115,41 +118,47 @@ func main() {
r.POST("/translate", func(c *gin.Context) {
reqj := ResData{}
c.BindJSON(&reqj)
source_lang := reqj.Source_Lang
target_lang := reqj.Target_Lang
if source_lang == "" {
source_lang = "ZH"
sourceLang := reqj.SourceLang
targetLang := reqj.TargetLang
if sourceLang == "" {
sourceLang = "ZH"
}
if target_lang == "" {
target_lang = "EN"
if targetLang == "" {
targetLang = "EN"
}
translate_text := reqj.Trans_Text
// fmt.Printf("%v", translate_text)
if translate_text != "" {
translateText := reqj.TransText
// fmt.Printf("%v", translateText)
if translateText == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": "No Translate Text Found",
})
} else {
url := "https://www2.deepl.com/jsonrpc"
id = id + 1
post_data := init_data(source_lang, target_lang)
postData := initData(sourceLang, targetLang)
text := Text{
Text: translate_text,
Text: translateText,
RequestAlternatives: 3,
}
// set id
post_data.ID = id
postData.ID = id
// set text
post_data.Params.Texts = append(post_data.Params.Texts, text)
postData.Params.Texts = append(postData.Params.Texts, text)
// set timestamp
post_data.Params.Timestamp = getTimeStamp(get_i_count(translate_text))
post_byte, _ := json.Marshal(post_data)
post_str := string(post_byte)
postData.Params.Timestamp = getTimeStamp(getICount(translateText))
post_byte, _ := json.Marshal(postData)
postStr := string(post_byte)
// add space if necessary
if (id+5)%29 == 0 || (id+3)%13 == 0 {
post_str = strings.Replace(post_str, "\"method\":\"", "\"method\" : \"", -1)
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\" : \"", -1)
} else {
post_str = strings.Replace(post_str, "\"method\":\"", "\"method\": \"", -1)
postStr = strings.Replace(postStr, "\"method\":\"", "\"method\": \"", -1)
}
post_byte = []byte(post_str)
post_byte = []byte(postStr)
reader := bytes.NewReader(post_byte)
request, err := http.NewRequest("POST", url, reader)
if err != nil {
@@ -177,38 +186,33 @@ func main() {
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
res := gjson.ParseBytes(body)
// display response
fmt.Println(res)
// fmt.Println(res)
if res.Get("error.code").String() == "-32600" {
log.Println(res.Get("error").String())
c.JSON(406, gin.H{
"code": 406,
"msg": "Invalid target_lang",
c.JSON(http.StatusNotAcceptable, gin.H{
"code": http.StatusNotAcceptable,
"message": "Invalid targetLang",
})
return
} else {
if resp.StatusCode == 429 {
c.JSON(429, gin.H{
"code": 429,
"message": "Too Many Requests",
})
} else {
c.JSON(200, gin.H{
"code": 200,
"id": id,
"data": res.Get("result.texts.0.text").String(),
})
}
}
} else {
c.JSON(404, gin.H{
"code": 404,
"message": "No Text Found",
})
if resp.StatusCode == http.StatusTooManyRequests {
c.JSON(http.StatusTooManyRequests, gin.H{
"code": http.StatusTooManyRequests,
"message": "Too Many Requests",
})
} else {
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": id,
"data": res.Get("result.texts.0.text").String(),
})
}
}
})
r.Run(":1188") // listen and serve on 0.0.0.0:1188
}