feat: 引入 Deepl 翻译策略

- 移除百度翻译相关的代码和配置
- 添加 Deepl 翻译策略实现
- 更新配置文件以支持 Deepl API
This commit is contained in:
zhiyu1998 2024-09-08 15:27:19 +08:00
parent 90f8373c60
commit 59d2fd069b
4 changed files with 33 additions and 32 deletions

View File

@ -61,7 +61,6 @@ import {
downloadBFile, downloadBFile,
filterBiliDescLink, filterBiliDescLink,
getBiliAudio, getBiliAudio,
getBiliVideoWithSession,
getDownloadUrl, getDownloadUrl,
getDynamic, getDynamic,
getScanCodeData, getScanCodeData,
@ -253,8 +252,7 @@ export class tools extends plugin {
this.xiaohongshuCookie = this.toolsConfig.xiaohongshuCookie; this.xiaohongshuCookie = this.toolsConfig.xiaohongshuCookie;
// 翻译引擎 // 翻译引擎
this.translateEngine = new Translate({ this.translateEngine = new Translate({
translateAppId: this.toolsConfig.translateAppId, deeplApiUrls: this.toolsConfig.deeplApiUrls,
translateSecret: this.toolsConfig.translateSecret,
proxy: this.myProxy, proxy: this.myProxy,
}); });
// 并发队列 // 并发队列
@ -284,7 +282,6 @@ export class tools extends plugin {
if (_.isEmpty(place)) { if (_.isEmpty(place)) {
const reply = await e?.getReply(); const reply = await e?.getReply();
if (reply !== undefined) { if (reply !== undefined) {
logger.info(reply);
place = reply.message.find(item => item.text !== undefined).text; place = reply.message.find(item => item.text !== undefined).text;
} else { } else {
return; return;

View File

@ -4,8 +4,7 @@ proxyAddr: '127.0.0.1' # 魔法地址
proxyPort: '7890' # 魔法端口 proxyPort: '7890' # 魔法端口
identifyPrefix: '✅' # 识别前缀,比如你识别哔哩哔哩,那么就有:✅ 识别:哔哩哔哩 identifyPrefix: '✅' # 识别前缀,比如你识别哔哩哔哩,那么就有:✅ 识别:哔哩哔哩
translateAppId: '' # 百度翻译APP ID deeplApiUrls: 'https://deeplx.papercar.top/translate,https://deeplx.llleman.com/translate'
translateSecret: '' # 百度翻译密匙
biliSessData: '' # 哔哩哔哩的SESSDATA biliSessData: '' # 哔哩哔哩的SESSDATA
biliIntroLenLimit: 50 # 哔哩哔哩简介长度限制,填 0 或者 -1 可以不做任何限制,显示完整简介 biliIntroLenLimit: 50 # 哔哩哔哩简介长度限制,填 0 或者 -1 可以不做任何限制,显示完整简介

View File

@ -60,23 +60,13 @@ export function supportGuoba() {
}, },
}, },
{ {
field: "tools.translateAppId", field: "tools.deeplApiUrls",
label: "百度翻译APP ID", label: "DeeplX API地址集合",
bottomHelpMessage: "使用百度翻译需要的APP ID需要申请", bottomHelpMessage: "可以参考https://github.com/OwO-Network/DeepLX进行搭建也可以使用内置",
component: "Input", component: "Input",
required: false, required: false,
componentProps: { componentProps: {
placeholder: "请输入APP ID", placeholder: "请输入DeeplX API地址集合",
},
},
{
field: "tools.translateSecret",
label: "百度翻译密匙",
bottomHelpMessage: "使用百度翻译需要的密匙(需要申请)",
component: "Input",
required: false,
componentProps: {
placeholder: "请输入密匙",
}, },
}, },
{ {

View File

@ -1,5 +1,4 @@
import { transMap, tencentTransMap } from "../constants/constant.js"; import { tencentTransMap } from "../constants/constant.js";
import md5 from "md5";
import fetch from "node-fetch"; import fetch from "node-fetch";
import _ from 'lodash' import _ from 'lodash'
@ -10,7 +9,7 @@ class TranslateStrategy {
} }
} }
// 百度翻译策略 // 企鹅翻译策略
class TencentTranslateStrategy extends TranslateStrategy { class TencentTranslateStrategy extends TranslateStrategy {
constructor(config) { constructor(config) {
super(); super();
@ -79,19 +78,35 @@ class TencentTranslateStrategy extends TranslateStrategy {
} }
} }
// 百度翻译策略 // Deepl翻译策略
class BaiduTranslateStrategy extends TranslateStrategy { class DeeplTranslateStrategy extends TranslateStrategy {
constructor(config) { constructor(config) {
super(); super();
this.config = config; this.config = config;
this.deeplUrls = this.config.deeplApiUrls.includes(",") ? this.config.deeplApiUrls.split(",") : [this.config.deeplApiUrls];
} }
async translate(query, targetLanguage) { async translate(query, targetLanguage) {
const url = `http://api.fanyi.baidu.com/api/trans/vip/translate?from=auto&to=${ transMap[targetLanguage] }&appid=${ this.config.translateAppId }&salt=rconsole&sign=${ md5(this.config.translateAppId + query + "rconsole" + this.config.translateSecret) }&q=${ query }`; const url = this.deeplUrls[Math.floor(Math.random() * this.deeplUrls.length)];
logger.info(`[R插件][Deepl翻译]当前使用的API${url}`);
try { try {
const response = await fetch(url); const source_lang = await new TencentTranslateStrategy(this.config).detectLanguage(query);
logger.info(`[R插件][Deepl翻译]:检测到的源语言:${source_lang}`);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
...this.commonHeaders
},
body: JSON.stringify({
text: query,
source_lang,
target_lang: tencentTransMap[targetLanguage]
}),
});
const data = await response.json(); const data = await response.json();
return data.trans_result[0].dst; return data.data;
} catch (error) { } catch (error) {
logger.error("Error translating text:", error); logger.error("Error translating text:", error);
return "翻译失败"; return "翻译失败";
@ -107,9 +122,9 @@ export default class Translate {
} }
selectStrategy() { selectStrategy() {
if (!_.isEmpty(this.config.translateAppId) && !_.isEmpty(this.config.translateSecret)) { if (!_.isEmpty(this.config.deeplApiUrls)) {
logger.info("[R插件][翻译策略]:当前选择 百度翻译") logger.info("[R插件][翻译策略]:当前选择 Deepl翻译")
return new BaiduTranslateStrategy(this.config); return new DeeplTranslateStrategy(this.config);
} else { } else {
logger.info("[R插件][翻译策略]:当前选择 企鹅翻译") logger.info("[R插件][翻译策略]:当前选择 企鹅翻译")
return new TencentTranslateStrategy(this.config); return new TencentTranslateStrategy(this.config);