Compare commits

...
14 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
Vincent Young f8a41debe6 Merge branch 'main' of https://github.com/OwO-Network/DeepLX 2023-02-18 20:46:26 +08:00
Vincent Young e593bceb97 fix: disable cgo 2023-02-18 20:46:16 +08:00
Vincent Young 9e558516ef Update README.md 2023-02-18 20:28:10 +08:00
AsukaMinato 7aa936e888 add aur 2023-02-16 21:38:33 +09:00
6 changed files with 98 additions and 93 deletions
+5 -11
View File
@@ -3,7 +3,7 @@
# @Author: Vincent Young # @Author: Vincent Young
# @Date: 2022-10-20 02:19:06 # @Date: 2022-10-20 02:19:06
# @LastEditors: Vincent Young # @LastEditors: Vincent Young
# @LastEditTime: 2022-10-20 09:51:57 # @LastEditTime: 2023-02-18 20:45:52
# @FilePath: /DeepLX/.cross_compile.sh # @FilePath: /DeepLX/.cross_compile.sh
# @Telegram: https://t.me/missuo # @Telegram: https://t.me/missuo
# #
@@ -29,16 +29,10 @@ for pl in ${PLATFORMS}; do
echo "build => ${TARGET}" echo "build => ${TARGET}"
if [ "${DEBUG_MODE}" == "debug" ]; then if [ "${DEBUG_MODE}" == "debug" ]; then
go build -trimpath -gcflags "all=-N -l" -o ${TARGET} \ CGO_ENABLED=0 go build -trimpath -gcflags "all=-N -l" -o ${TARGET} \
-ldflags "-X 'main.version=${BUILD_VERSION}' \ -ldflags "-w -s" main.go
-X 'main.buildDate=${BUILD_DATE}' \
-X 'main.commitID=${COMMIT_SHA1}'\
-w -s"
else else
go build -trimpath -o ${TARGET} \ CGO_ENABLED=0 go build -trimpath -o ${TARGET} \
-ldflags "-X 'main.version=${BUILD_VERSION}' \ -ldflags "-w -s" main.go
-X 'main.buildDate=${BUILD_DATE}' \
-X 'main.commitID=${COMMIT_SHA1}'\
-w -s"
fi fi
done done
+2 -14
View File
@@ -13,7 +13,7 @@ jobs:
- uses: actions/setup-go@v2 - uses: actions/setup-go@v2
with: with:
go-version: "1.18" go-version: "1.19"
- run: bash .cross_compile.sh - run: bash .cross_compile.sh
@@ -22,18 +22,6 @@ jobs:
with: with:
draft: false draft: false
files: | files: |
dist/deeplx_darwin_amd64 dist/*
dist/deeplx_darwin_arm64
dist/deeplx_linux_386
dist/deeplx_linux_amd64
dist/deeplx_linux_arm64
dist/deeplx_linux_armv7
dist/deeplx_linux_mips
dist/deeplx_openbsd_amd64
dist/deeplx_openbsd_arm64
dist/deeplx_freebsd_amd64
dist/deeplx_freebsd_arm64
dist/deeplx_windows_amd64.exe
dist/deeplx_windows_386.exe
env: env:
GITHUB_TOKEN: ${{ secrets.GT_Token }} GITHUB_TOKEN: ${{ secrets.GT_Token }}
+25 -5
View File
@@ -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-02-18 19:46:10 * @LastEditTime: 2023-02-24 21:36:36
* @FilePath: /DeepLX/README.md * @FilePath: /DeepLX/README.md
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* *
@@ -32,13 +32,19 @@ Permanently free DeepL API written in Golang
} }
``` ```
### Run on Linux Server
```bash
bash <(curl -Ls https://cpp.li/deeplx)
```
### Run on Mac ### Run on Mac
1. Download the latest release of DeepL X. 1. Download the latest release of DeepL X.
```bash ```bash
sudo mv deeplx_darwin_amd64 /usr/local/bin/deeplx sudo mv deeplx_darwin_amd64 /usr/local/bin/deeplx
sudo chmod +x /usr/local/bin/deeplx
``` ```
2. Download the `me.missuo.deeplx.plist` to `/Users/YOUR_USERNAME/Library/LaunchAgents`. 2. Download the `me.missuo.deeplx.plist` to `~/Library/LaunchAgents`.
```bash ```bash
wget https://raw.githubusercontent.com/OwO-Network/DeepLX/main/me.missuo.deeplx.plist -O ~/Library/LaunchAgents/me.missuo.deeplx.plist wget https://raw.githubusercontent.com/OwO-Network/DeepLX/main/me.missuo.deeplx.plist -O ~/Library/LaunchAgents/me.missuo.deeplx.plist
``` ```
@@ -48,15 +54,29 @@ launchctl load ~/Library/LaunchAgents/me.missuo.deeplx.plist
launchctl start ~/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/) ## Setup on [Bob App](https://bobtranslate.com/)
1. Install [bob-plugin-deeplx](https://github.com/clubxdev/bob-plugin-deeplx) on Bob. 1. Install [bob-plugin-deeplx](https://github.com/clubxdev/bob-plugin-deeplx) on Bob.
2. Setup the API. 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 ```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 ## Author
**DeepL X** © [Vincent Young](https://github.com/missuo) & [Leo Shen](https://github.com/sjlleo), Released under the [MIT](./LICENSE) License.<br> **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] [Unit]
Description=DeepLX Service Description=DeepLX Service
After=network.target nss-lookup.target After=network.target
Wants=network.target
[Service] [Service]
User=root
Group=root
Type=simple Type=simple
Restart=always
WorkingDirectory=/usr/bin/ WorkingDirectory=/usr/bin/
ExecStart=/usr/bin/deeplx ExecStart=/usr/bin/deeplx
[Install] [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 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 daemon-reload
systemctl enable deeplx systemctl enable deeplx
systemctl start deeplx
echo -e "Installed successfully, listening at 0.0.0.0:1188" 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" "github.com/tidwall/gjson"
) )
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
type Lang struct { type Lang struct {
SourceLangUserSelected string `json:"source_lang_user_selected"` SourceLangUserSelected string `json:"source_lang_user_selected"`
TargetLang string `json:"target_lang"` TargetLang string `json:"target_lang"`
@@ -46,15 +50,15 @@ type PostData struct {
Params Params `json:"params"` Params Params `json:"params"`
} }
func init_data(source_lang string, target_lang string) *PostData { func initData(sourceLang string, targetLang string) *PostData {
return &PostData{ return &PostData{
Jsonrpc: "2.0", Jsonrpc: "2.0",
Method: "LMT_handle_texts", Method: "LMT_handle_texts",
Params: Params{ Params: Params{
Splitting: "newlines", Splitting: "newlines",
Lang: Lang{ Lang: Lang{
SourceLangUserSelected: source_lang, SourceLangUserSelected: sourceLang,
TargetLang: target_lang, TargetLang: targetLang,
}, },
CommonJobParams: CommonJobParams{ CommonJobParams: CommonJobParams{
WasSpoken: false, WasSpoken: false,
@@ -65,8 +69,8 @@ func init_data(source_lang string, target_lang string) *PostData {
} }
} }
func get_i_count(translate_text string) int64 { func getICount(translateText string) int64 {
return int64(strings.Count(translate_text, "i")) return int64(strings.Count(translateText, "i"))
} }
func getRandomNumber() int64 { func getRandomNumber() int64 {
@@ -75,21 +79,20 @@ func getRandomNumber() int64 {
return num * 1000 return num * 1000
} }
func getTimeStamp(i_count int64) int64 { func getTimeStamp(iCount int64) int64 {
ts := time.Now().UnixMilli() ts := time.Now().UnixMilli()
if i_count != 0 { if iCount != 0 {
i_count = i_count + 1 iCount = iCount + 1
return ts - ts%i_count + i_count return ts - ts%iCount + iCount
} else { } else {
return ts return ts
} }
} }
type ResData struct { type ResData struct {
Trans_Text string `json:"text"` TransText string `json:"text"`
Source_Lang string `json:"source_lang"` SourceLang string `json:"source_lang"`
Target_Lang string `json:"target_lang"` TargetLang string `json:"target_lang"`
} }
func main() { func main() {
@@ -106,8 +109,8 @@ func main() {
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,
"msg": "DeepL API, Made by sjlleo and missuo. Go to /translate with POST.", "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) { r.POST("/translate", func(c *gin.Context) {
reqj := ResData{} reqj := ResData{}
c.BindJSON(&reqj) c.BindJSON(&reqj)
source_lang := reqj.Source_Lang
target_lang := reqj.Target_Lang sourceLang := reqj.SourceLang
if source_lang == "" { targetLang := reqj.TargetLang
source_lang = "ZH" if sourceLang == "" {
sourceLang = "ZH"
} }
if target_lang == "" { if targetLang == "" {
target_lang = "EN" targetLang = "EN"
} }
translate_text := reqj.Trans_Text translateText := reqj.TransText
// fmt.Printf("%v", translate_text) // fmt.Printf("%v", translateText)
if translate_text != "" { if translateText == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": "No Translate Text Found",
})
} else {
url := "https://www2.deepl.com/jsonrpc" url := "https://www2.deepl.com/jsonrpc"
id = id + 1 id = id + 1
post_data := init_data(source_lang, target_lang) postData := initData(sourceLang, targetLang)
text := Text{ text := Text{
Text: translate_text, Text: translateText,
RequestAlternatives: 3, RequestAlternatives: 3,
} }
// set id // set id
post_data.ID = id postData.ID = id
// set text // set text
post_data.Params.Texts = append(post_data.Params.Texts, text) postData.Params.Texts = append(postData.Params.Texts, text)
// set timestamp // set timestamp
post_data.Params.Timestamp = getTimeStamp(get_i_count(translate_text)) postData.Params.Timestamp = getTimeStamp(getICount(translateText))
post_byte, _ := json.Marshal(post_data) post_byte, _ := json.Marshal(postData)
post_str := string(post_byte) postStr := string(post_byte)
// add space if necessary // add space if necessary
if (id+5)%29 == 0 || (id+3)%13 == 0 { 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 { } 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) reader := bytes.NewReader(post_byte)
request, err := http.NewRequest("POST", url, reader) request, err := http.NewRequest("POST", url, reader)
if err != nil { if err != nil {
@@ -177,38 +186,33 @@ func main() {
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
res := gjson.ParseBytes(body) res := gjson.ParseBytes(body)
// display response // display response
fmt.Println(res) // fmt.Println(res)
if res.Get("error.code").String() == "-32600" { if res.Get("error.code").String() == "-32600" {
log.Println(res.Get("error").String()) log.Println(res.Get("error").String())
c.JSON(406, gin.H{ c.JSON(http.StatusNotAcceptable, gin.H{
"code": 406, "code": http.StatusNotAcceptable,
"msg": "Invalid target_lang", "message": "Invalid targetLang",
}) })
return 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{ if resp.StatusCode == http.StatusTooManyRequests {
"code": 404, c.JSON(http.StatusTooManyRequests, gin.H{
"message": "No Text Found", "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 r.Run(":1188") // listen and serve on 0.0.0.0:1188
} }