feat: 翻译插件新增G翻译 & 🐧翻译(默认)

This commit is contained in:
zhiyu1998 2023-03-21 14:23:35 +08:00
parent 3f90a5701b
commit ba7f42f205
2 changed files with 153 additions and 18 deletions

View File

@ -3,7 +3,6 @@ import fetch from "node-fetch";
import fs from "node:fs"; import fs from "node:fs";
import { segment } from "oicq"; import { segment } from "oicq";
// 其他库 // 其他库
import md5 from "md5";
import axios from "axios"; import axios from "axios";
import _ from "lodash"; import _ from "lodash";
import tunnel from "tunnel"; import tunnel from "tunnel";
@ -15,6 +14,7 @@ import { parseUrl, parseM3u8, downloadM3u8Videos, mergeAcFileToMp4 } from "../ut
import { transMap, douyinTypeMap, TEN_THOUSAND, XHS_CK } from "../utils/constant.js"; import { transMap, douyinTypeMap, TEN_THOUSAND, XHS_CK } from "../utils/constant.js";
import { getIdVideo, generateRandomStr } from "../utils/common.js"; import { getIdVideo, generateRandomStr } from "../utils/common.js";
import config from "../model/index.js"; import config from "../model/index.js";
import Translate from "../utils/transStrategy.js";
export class tools extends plugin { export class tools extends plugin {
constructor() { constructor() {
@ -81,10 +81,6 @@ export class tools extends plugin {
this.proxyAddr = this.toolsConfig.proxyAddr; this.proxyAddr = this.toolsConfig.proxyAddr;
this.proxyPort = this.toolsConfig.proxyPort; this.proxyPort = this.toolsConfig.proxyPort;
this.myProxy = `http://${this.proxyAddr}:${this.proxyPort}`; this.myProxy = `http://${this.proxyAddr}:${this.proxyPort}`;
// console.log(this.myProxy)
// 加载百度翻译配置
this.translateAppId = this.toolsConfig.translateAppId;
this.translateSecret = this.toolsConfig.translateSecret;
// 加载twitter配置 // 加载twitter配置
this.bearerToken = this.toolsConfig.bearerToken; this.bearerToken = this.toolsConfig.bearerToken;
} }
@ -101,19 +97,26 @@ export class tools extends plugin {
return; return;
} }
const place = msg.replace(language[0], "").trim(); const place = msg.replace(language[0], "").trim();
// let url = /[\u4E00-\u9FFF]+/g.test(place) const translateEngine = new Translate({
// let url = `http://api.fanyi.baidu.com/api/trans/vip/translate?from=auto&to=${ transMap[language[1]] }&appid=APP ID&salt=自定义&sign=${ md5("APP ID" + place + "自定义" + "密钥") }&q=${ place }`; translateAppId: this.toolsConfig.translateAppId,
let url = `http://api.fanyi.baidu.com/api/trans/vip/translate?from=auto&to=${ translateSecret: this.toolsConfig.translateSecret,
transMap[language[1]] proxy: this.myProxy
}&appid=${this.translateAppId}&salt=rconsole&sign=${md5( });
this.translateAppId + place + "rconsole" + this.translateSecret, // 如果没有百度那就Google
)}&q=${place}`; let translateResult;
// console.log(url) if (_.isEmpty(this.toolsConfig.translateAppId) || _.isEmpty(this.toolsConfig.translateSecret)) {
await fetch(url) try {
.then(resp => resp.json()) translateResult = await translateEngine.google(place, language[1]);
.then(text => text.trans_result) } catch (err) {
.then(res => this.reply(`${res[0].dst}`, true)) console.err("谷歌翻译失败,", err);
.catch(err => logger.error(err)); }
// 腾讯交互式进行补充
translateResult += "\n\n🐧翻译" + await translateEngine.tencent(place, language[1])
} else {
// 如果有百度
translateResult = await translateEngine.baidu(place, language[1]);
}
e.reply(translateResult, true);
return true; return true;
} }

132
utils/transStrategy.js Normal file
View File

@ -0,0 +1,132 @@
import {transMap} from "./constant.js";
import md5 from "md5";
import fetch from "node-fetch";
import HttpProxyAgent from "https-proxy-agent";
/**
* 翻译插件策略模式
*/
export default class Translate {
config = {
/**
* 百度翻译appid
*/
translateAppId: "",
/**
* 百度翻译密匙
*/
translateSecret: "",
/**
* 魔法
*/
proxy: ""
}
constructor(config) {
this.config = config;
}
/**
* 百度翻译
* @param query 查询句子
* @param targetLanguage 目标语言
* @returns {Promise<unknown>}
*/
async baidu(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}`;
return fetch(url)
.then(resp => resp.json())
.then(text => text.trans_result)
.then(res => res[0].dst)
.catch(err => logger.error(err));
}
/**
* google翻译
* @param query
* @param targetLanguage
* @returns {Promise<string>}
*/
async google(query, targetLanguage) {
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=auto&tl=${transMap[targetLanguage]}&q=${query}`;
return fetch(url, {
method: "GET",
headers: {
"USER-AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
},
agent: new HttpProxyAgent(this.config.proxy || "http://127.0.0.1:7890"),
})
.then(resp => resp.text())
.then(res => JSON.parse(res))
.then(res => res[0][0][0])
}
/**
* 腾选交互式翻译
* @param query
* @param targetLanguage
* @returns {Promise<Response>}
*/
async tencent(query, targetLanguage) {
const url = `https://transmart.qq.com/api/imt`
const sourceLanguage = await fetch(url, {
method: "POST",
headers: {
"USER-AGENT": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/111.0"
},
body: JSON.stringify({
"header": {
"fn": "text_analysis",
"client_key": "browser-firefox-111.0.0-Mac OS-d35fca23-eb48-45ba-9913-114f1177b02b-1679376552800"
},
"text": "s",
"type": "plain",
"normalize": {
"merge_broken_line": false
}
})
}).then(async resp => {
const data = JSON.parse(await resp.text());
if (data.header.ret_code !== 'succ') {
return "en"
}
return data.language;
})
return fetch(url, {
method: "POST",
headers: {
"USER-AGENT": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/111.0"
},
body: JSON.stringify({
"header": {
"fn": "auto_translation",
"client_key": "browser-firefox-111.0.0-Mac OS-d35fca23-eb48-45ba-9913-114f1177b02b-1679376552800"
},
"type": "plain",
"model_category": "normal",
"text_domain": "general",
"source": {
"lang": sourceLanguage,
"text_list": [
"",
query,
""
]
},
"target": {
"lang": transMap[targetLanguage]
}
})
}).then(async resp => {
const data = JSON.parse(await resp.text());
if (data.header.ret_code !== 'succ') {
return "翻译失败"
}
return data.auto_translation?.[1];
})
}
}