Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# default: 6119
PORT=6119

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

# 跨域请求 (待完善)
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# DeepLX Serverless

<p align="center">
<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>
<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>
</p>

***本项目3.0版本开始完全基于[OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)和DeepL网页端API进行重写。***
**2.0及之前版本在[LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)的基础上进行重构。**

DeepLX Serverless是一个基于DeepL翻译且无需令牌的网页API的Serverless版本,与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`(不过嘛凡事总有例外¯\\\_(ツ)_/¯)
DeepLX Serverless是一个基于DeepL翻译网页版且无需令牌的Serverless版本,与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`(不过嘛凡事总有例外¯\\\_(ツ)_/¯)

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

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

### Startup Parameters | 启动参数
> [!IMPORTANT]
> 此功能在 2.0.0 及以上版本中可用。
> 此功能在 2.0.0 及以上版本中可用。
> 启动参数会覆盖`.env`

有时你可能想在启动服务时添加参数。
Expand Down Expand Up @@ -93,7 +91,7 @@ translate('how are you?', 'en', 'zh', '', false, false)
## Contribute | 贡献
1. 获取`dev`或`main`分支的代码
2. 提交你的更改并描述提交内容
3. 打开一个`Pull Requests`
3. 创建一个`Pull Requests`

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

Expand All @@ -104,3 +102,8 @@ translate('how are you?', 'en', 'zh', '', false, false)
1. [OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)
2. [LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)
3. [bropines/Deeplx-vercel](https://github.com/bropines/Deeplx-vercel)

## Disclaimer | 免责声明
请勿依赖本项目,因基于DeepL网页版数据,可能随时罢工。如果您有大量内容需要翻译,请购买DeepL官方翻译API,DeepLXS始终受到DeepL政策限制。

本项目目前处于开发状态,实现及其简陋,部分功能已编写文档不代表已实现。
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deeplx-serverless",
"version": "3.0.1",
"version": "3.1.0",
"description": "DeepL free API for Serverless",
"type": "module",
"main": "src/server.js",
Expand Down
24 changes: 7 additions & 17 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const argv = yargs(hideBin(process.argv))
})
.option('alt', {
alias: 'a',
describe: 'Allow request alternatives translation',
describe: 'Return alternatives translation',
type: 'boolean',
default: Boolean(process.env.ALTERNATIVE) || true
})
Expand All @@ -33,7 +33,7 @@ const argv = yargs(hideBin(process.argv))
// 定义配置
const app = express(),
PORT = argv.port,
allowAlternative = argv.alt,
returnAlternative = argv.alt,
CORS = {
origin: argv.cors,
methods: 'GET,POST',
Expand All @@ -55,7 +55,7 @@ async function post(req, res) {
target_lang = target_lang.toUpperCase();

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

// 是否允许备选翻译
if (!allowAlternative && alt_count !== undefined) {
const duration = Date.now() - startTime;
console.log(`[LOG] ${new Date().toISOString()} | POST "translate" | 405 | Alternative Not Allowed | ${duration}ms`);
return res.status(405).json({
code: 405,
message: "Alternative Translate Not Allowed"
});
}

try {
const result = await translate(text, source_lang, target_lang);
// const result = await translate(text, source_lang, target_lang, alt_count);
const result = await translate(text, source_lang, target_lang, alt_count);
// const result = await translate(text, source_lang, target_lang);
/*result = brotliDecompress(result, (err, decompressedData) => {
if (err) console.error(err);
return decompressedData;
Expand Down Expand Up @@ -106,11 +96,11 @@ async function post(req, res) {
const responseData = {
code: result.code,
id: result.id,
data: result.data, // 取第一个翻译结果
data: result.data,
method: "Free",
source_lang: result.source_lang,
target_lang,
alternatives: result.alternatives
alternatives: (returnAlternative ? result.alternatives : "[]")
};

/*brotliCompress(responseData, (err, compressedData) => {
Expand Down
34 changes: 14 additions & 20 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,18 @@ function formatPostString(postData) {
return body;
}

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

const headers = {
'Content-Type': 'application/json',
"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",
"Accept": "*/*",
"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",
"Authorization": "None",
"Cache-Control": "no-cache",
"DNT": "1",
"Origin": "chrome-extension://cofdbpoegempjloogbagkncekinflcnj",
"Pragma": "no-cache",
"Priority": "u=1, i",
"Referer": "https://www.deepl.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "none",
"Sec-GPC": "1",
'User-Agent': 'DeepLBrowserExtension/1.28.0 Mozilla/5.0',
Accept: '*/*',
'Accept-Language': 'en-US,en;q=0.9',
Origin: 'https://www.deepl.com',
Referer: 'https://www.deepl.com/',
Pragma: 'no-cache',
'Cache-Control': 'no-cache',
...(dlSession && {'Cookie': `dl_session=${dlSession}`})
};
postData = formatPostString(postData);
Expand Down Expand Up @@ -157,7 +150,7 @@ async function translate(text, sourceLang, targetLang, dlSession, tagHandling, p
let alternatives = [], translatedText = '';

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

const ret = {
code: postData.status,
code: postData.status || 200,
id: postData.id,
method: "Free",
data: translatedText,
alternatives: alternatives,
source_lang: sourceLang.toUpperCase(),
target_lang: targetLang.toUpperCase()
target_lang: targetLang.toUpperCase(),
alternatives: alternatives
}
if(printResult) console.log(response);
if(printResult) console.log(ret);
return ret;
} catch (err) {
Expand Down
Loading