Skip to content

Commit 395f9dc

Browse files
authored
Merge dev to main (#47)
2 parents 327dcbb + dea242d commit 395f9dc

File tree

5 files changed

+35
-48
lines changed

5 files changed

+35
-48
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# default: 6119
44
PORT=6119
55

6-
# 是否允许请求备选翻译
6+
# 是否返回备选翻译
77
# type: Boolean
88
# default: true
9-
# 不允许时报错
9+
# 如果文本数量巨大关闭可节省流量。
1010
ALTERNATIVE=true
1111

1212
# 跨域请求 (待完善)

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# DeepLX Serverless
22

33
<p align="center">
4-
<a href="https://github.com/guobao2333/MT-syntax-highlight"><img alt="Repository" src="https://img.shields.io/badge/Github-%230A0A0A.svg?&style=flat-square&logo=Github&logoColor=white"/></a>
4+
<a href="https://github.com/guobao2333/DeepLX-Serverless"><img alt="Repository" src="https://img.shields.io/badge/Github-%230A0A0A.svg?&style=flat-square&logo=Github&logoColor=white"/></a>
55
</p>
66

7-
***本项目3.0版本开始完全基于[OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)和DeepL网页端API进行重写。***
8-
**2.0及之前版本在[LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)的基础上进行重构。**
9-
10-
DeepLX Serverless是一个基于DeepL翻译且无需令牌的网页API的Serverless版本,与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`(不过嘛凡事总有例外¯\\\_(ツ)_/¯)
7+
DeepLX Serverless是一个基于DeepL翻译网页版且无需令牌的Serverless版本,与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`(不过嘛凡事总有例外¯\\\_(ツ)_/¯)
118

12-
请勿滥用本项目!如果您有大量内容需要翻译,请购买DeepL的付费服务,项目使用的网页版接口会受到DeepL政策限制。
9+
**3.0版本开始完全基于[OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)和DeepL网页版数据进行重写。**
10+
**2.0及之前版本在[LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)的基础上进行重构。**
1311

1412
## Prerequisites | 准备工作
1513
> [!IMPORTANT]
@@ -46,7 +44,7 @@ docker run -d -p 6119:6119 deeplxs
4644

4745
### Startup Parameters | 启动参数
4846
> [!IMPORTANT]
49-
> 此功能在 2.0.0 及以上版本中可用。
47+
> 此功能在 2.0.0 及以上版本中可用。
5048
> 启动参数会覆盖`.env`
5149
5250
有时你可能想在启动服务时添加参数。
@@ -93,7 +91,7 @@ translate('how are you?', 'en', 'zh', '', false, false)
9391
## Contribute | 贡献
9492
1. 获取`dev``main`分支的代码
9593
2. 提交你的更改并描述提交内容
96-
3. 打开一个`Pull Requests`
94+
3. 创建一个`Pull Requests`
9795

9896
如果你是第一次贡献,那么请查看[《如何为开源做贡献》](https://opensource.guide/how-to-contribute/)
9997

@@ -104,3 +102,8 @@ translate('how are you?', 'en', 'zh', '', false, false)
104102
1. [OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)
105103
2. [LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)
106104
3. [bropines/Deeplx-vercel](https://github.com/bropines/Deeplx-vercel)
105+
106+
## Disclaimer | 免责声明
107+
请勿依赖本项目,因基于DeepL网页版数据,可能随时罢工。如果您有大量内容需要翻译,请购买DeepL官方翻译API,DeepLXS始终受到DeepL政策限制。
108+
109+
本项目目前处于开发状态,实现及其简陋,部分功能已编写文档不代表已实现。

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deeplx-serverless",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "DeepL free API for Serverless",
55
"type": "module",
66
"main": "src/server.js",

src/server.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const argv = yargs(hideBin(process.argv))
1717
})
1818
.option('alt', {
1919
alias: 'a',
20-
describe: 'Allow request alternatives translation',
20+
describe: 'Return alternatives translation',
2121
type: 'boolean',
2222
default: Boolean(process.env.ALTERNATIVE) || true
2323
})
@@ -33,7 +33,7 @@ const argv = yargs(hideBin(process.argv))
3333
// 定义配置
3434
const app = express(),
3535
PORT = argv.port,
36-
allowAlternative = argv.alt,
36+
returnAlternative = argv.alt,
3737
CORS = {
3838
origin: argv.cors,
3939
methods: 'GET,POST',
@@ -55,7 +55,7 @@ async function post(req, res) {
5555
target_lang = target_lang.toUpperCase();
5656

5757
// 检查请求体
58-
if (!req.body || !text || !target_lang || (alt_count !== undefined && typeof alt_count !== 'number')) {
58+
if (!req.body || !text || !target_lang || alt_count !== undefined && typeof alt_count !== 'number' || alt_count > 3 || alt_count < 0) {
5959
const duration = Date.now() - startTime;
6060
console.log(`[WARN] ${new Date().toISOString()} | POST "translate" | 400 | Bad Request | ${duration}ms`);
6161
return res.status(400).json({
@@ -64,19 +64,9 @@ async function post(req, res) {
6464
});
6565
}
6666

67-
// 是否允许备选翻译
68-
if (!allowAlternative && alt_count !== undefined) {
69-
const duration = Date.now() - startTime;
70-
console.log(`[LOG] ${new Date().toISOString()} | POST "translate" | 405 | Alternative Not Allowed | ${duration}ms`);
71-
return res.status(405).json({
72-
code: 405,
73-
message: "Alternative Translate Not Allowed"
74-
});
75-
}
76-
7767
try {
78-
const result = await translate(text, source_lang, target_lang);
79-
// const result = await translate(text, source_lang, target_lang, alt_count);
68+
const result = await translate(text, source_lang, target_lang, alt_count);
69+
// const result = await translate(text, source_lang, target_lang);
8070
/*result = brotliDecompress(result, (err, decompressedData) => {
8171
if (err) console.error(err);
8272
return decompressedData;
@@ -106,11 +96,11 @@ async function post(req, res) {
10696
const responseData = {
10797
code: result.code,
10898
id: result.id,
109-
data: result.data, // 取第一个翻译结果
99+
data: result.data,
110100
method: "Free",
111101
source_lang: result.source_lang,
112102
target_lang,
113-
alternatives: result.alternatives
103+
alternatives: (returnAlternative ? result.alternatives : "[]")
114104
};
115105

116106
/*brotliCompress(responseData, (err, compressedData) => {

src/translate.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,18 @@ function formatPostString(postData) {
1313
return body;
1414
}
1515

16-
async function sendRequest(postData, urlMethod, dlSession, printResult) {
17-
const urlFull = `${baseURL}/jsonrpc?client=chrome-extension,1.28.0&method=${encodeURIComponent(urlMethod)}`;
16+
async function sendRequest(postData, urlMethod, dlSession, tagHandling, printResult) {
17+
const urlFull = `${baseURL}/jsonrpc?`;
1818

1919
const headers = {
2020
'Content-Type': 'application/json',
21-
"User-Agent": "DeepLBrowserExtension/1.28.0 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
22-
"Accept": "*/*",
23-
"Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7,zh-HK;q=0.6,zh;q=0.5",
24-
"Authorization": "None",
25-
"Cache-Control": "no-cache",
26-
"DNT": "1",
27-
"Origin": "chrome-extension://cofdbpoegempjloogbagkncekinflcnj",
28-
"Pragma": "no-cache",
29-
"Priority": "u=1, i",
30-
"Referer": "https://www.deepl.com/",
31-
"Sec-Fetch-Dest": "empty",
32-
"Sec-Fetch-Mode": "cors",
33-
"Sec-Fetch-Site": "none",
34-
"Sec-GPC": "1",
21+
'User-Agent': 'DeepLBrowserExtension/1.28.0 Mozilla/5.0',
22+
Accept: '*/*',
23+
'Accept-Language': 'en-US,en;q=0.9',
24+
Origin: 'https://www.deepl.com',
25+
Referer: 'https://www.deepl.com/',
26+
Pragma: 'no-cache',
27+
'Cache-Control': 'no-cache',
3528
...(dlSession && {'Cookie': `dl_session=${dlSession}`})
3629
};
3730
postData = formatPostString(postData);
@@ -157,7 +150,7 @@ async function translate(text, sourceLang, targetLang, dlSession, tagHandling, p
157150
let alternatives = [], translatedText = '';
158151

159152
// 获取备选翻译
160-
if (response.result.translations.length > 0) {
153+
if (response.result.translations != '' && response.result.translations.length > 0) {
161154
response.result.translations[0].beams.forEach(beam => {
162155
alternatives.push(beam.sentences[0].text);
163156
});
@@ -171,14 +164,15 @@ async function translate(text, sourceLang, targetLang, dlSession, tagHandling, p
171164
}
172165

173166
const ret = {
174-
code: postData.status,
167+
code: postData.status || 200,
175168
id: postData.id,
176169
method: "Free",
177170
data: translatedText,
178-
alternatives: alternatives,
179171
source_lang: sourceLang.toUpperCase(),
180-
target_lang: targetLang.toUpperCase()
172+
target_lang: targetLang.toUpperCase(),
173+
alternatives: alternatives
181174
}
175+
if(printResult) console.log(response);
182176
if(printResult) console.log(ret);
183177
return ret;
184178
} catch (err) {

0 commit comments

Comments
 (0)