mirror of
https://github.com/Jerryplusy/rc-plugin.git
synced 2025-10-14 08:09:19 +00:00
✨ feat: 引入 Deepl 翻译策略
- 移除百度翻译相关的代码和配置 - 添加 Deepl 翻译策略实现 - 更新配置文件以支持 Deepl API
This commit is contained in:
parent
90f8373c60
commit
59d2fd069b
@ -61,7 +61,6 @@ import {
|
||||
downloadBFile,
|
||||
filterBiliDescLink,
|
||||
getBiliAudio,
|
||||
getBiliVideoWithSession,
|
||||
getDownloadUrl,
|
||||
getDynamic,
|
||||
getScanCodeData,
|
||||
@ -253,8 +252,7 @@ export class tools extends plugin {
|
||||
this.xiaohongshuCookie = this.toolsConfig.xiaohongshuCookie;
|
||||
// 翻译引擎
|
||||
this.translateEngine = new Translate({
|
||||
translateAppId: this.toolsConfig.translateAppId,
|
||||
translateSecret: this.toolsConfig.translateSecret,
|
||||
deeplApiUrls: this.toolsConfig.deeplApiUrls,
|
||||
proxy: this.myProxy,
|
||||
});
|
||||
// 并发队列
|
||||
@ -284,7 +282,6 @@ export class tools extends plugin {
|
||||
if (_.isEmpty(place)) {
|
||||
const reply = await e?.getReply();
|
||||
if (reply !== undefined) {
|
||||
logger.info(reply);
|
||||
place = reply.message.find(item => item.text !== undefined).text;
|
||||
} else {
|
||||
return;
|
||||
|
@ -4,8 +4,7 @@ proxyAddr: '127.0.0.1' # 魔法地址
|
||||
proxyPort: '7890' # 魔法端口
|
||||
identifyPrefix: '✅' # 识别前缀,比如你识别哔哩哔哩,那么就有:✅ 识别:哔哩哔哩
|
||||
|
||||
translateAppId: '' # 百度翻译APP ID
|
||||
translateSecret: '' # 百度翻译密匙
|
||||
deeplApiUrls: 'https://deeplx.papercar.top/translate,https://deeplx.llleman.com/translate'
|
||||
|
||||
biliSessData: '' # 哔哩哔哩的SESSDATA
|
||||
biliIntroLenLimit: 50 # 哔哩哔哩简介长度限制,填 0 或者 -1 可以不做任何限制,显示完整简介
|
||||
@ -30,4 +29,4 @@ autoclearTrashtime: '0 0 8 * * ?' #每天早上8点自动清理视频缓存,cr
|
||||
|
||||
aiBaseURL: '' # 用于识图的接口,kimi默认接口为:https://api.moonshot.cn,其他服务商自己填写
|
||||
aiApiKey: '' # 用于识图的api key,kimi接口申请:https://platform.moonshot.cn/console/api-keys
|
||||
aiModel: 'moonshot-v1-8k' # 模型,使用kimi不用填写,其他要填写
|
||||
aiModel: 'moonshot-v1-8k' # 模型,使用kimi不用填写,其他要填写
|
||||
|
@ -60,23 +60,13 @@ export function supportGuoba() {
|
||||
},
|
||||
},
|
||||
{
|
||||
field: "tools.translateAppId",
|
||||
label: "百度翻译APP ID",
|
||||
bottomHelpMessage: "使用百度翻译需要的APP ID(需要申请)",
|
||||
field: "tools.deeplApiUrls",
|
||||
label: "DeeplX API地址集合",
|
||||
bottomHelpMessage: "可以参考:https://github.com/OwO-Network/DeepLX,进行搭建,也可以使用内置",
|
||||
component: "Input",
|
||||
required: false,
|
||||
componentProps: {
|
||||
placeholder: "请输入APP ID",
|
||||
},
|
||||
},
|
||||
{
|
||||
field: "tools.translateSecret",
|
||||
label: "百度翻译密匙",
|
||||
bottomHelpMessage: "使用百度翻译需要的密匙(需要申请)",
|
||||
component: "Input",
|
||||
required: false,
|
||||
componentProps: {
|
||||
placeholder: "请输入密匙",
|
||||
placeholder: "请输入DeeplX API地址集合",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { transMap, tencentTransMap } from "../constants/constant.js";
|
||||
import md5 from "md5";
|
||||
import { tencentTransMap } from "../constants/constant.js";
|
||||
import fetch from "node-fetch";
|
||||
import _ from 'lodash'
|
||||
|
||||
@ -10,7 +9,7 @@ class TranslateStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
// 百度翻译策略
|
||||
// 企鹅翻译策略
|
||||
class TencentTranslateStrategy extends TranslateStrategy {
|
||||
constructor(config) {
|
||||
super();
|
||||
@ -79,19 +78,35 @@ class TencentTranslateStrategy extends TranslateStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
// 百度翻译策略
|
||||
class BaiduTranslateStrategy extends TranslateStrategy {
|
||||
// Deepl翻译策略
|
||||
class DeeplTranslateStrategy extends TranslateStrategy {
|
||||
constructor(config) {
|
||||
super();
|
||||
this.config = config;
|
||||
this.deeplUrls = this.config.deeplApiUrls.includes(",") ? this.config.deeplApiUrls.split(",") : [this.config.deeplApiUrls];
|
||||
}
|
||||
|
||||
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 {
|
||||
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();
|
||||
return data.trans_result[0].dst;
|
||||
return data.data;
|
||||
} catch (error) {
|
||||
logger.error("Error translating text:", error);
|
||||
return "翻译失败";
|
||||
@ -107,9 +122,9 @@ export default class Translate {
|
||||
}
|
||||
|
||||
selectStrategy() {
|
||||
if (!_.isEmpty(this.config.translateAppId) && !_.isEmpty(this.config.translateSecret)) {
|
||||
logger.info("[R插件][翻译策略]:当前选择 百度翻译")
|
||||
return new BaiduTranslateStrategy(this.config);
|
||||
if (!_.isEmpty(this.config.deeplApiUrls)) {
|
||||
logger.info("[R插件][翻译策略]:当前选择 Deepl翻译")
|
||||
return new DeeplTranslateStrategy(this.config);
|
||||
} else {
|
||||
logger.info("[R插件][翻译策略]:当前选择 企鹅翻译")
|
||||
return new TencentTranslateStrategy(this.config);
|
||||
|
Loading…
x
Reference in New Issue
Block a user