Skip to content

Commit

Permalink
v0.1
Browse files Browse the repository at this point in the history
add more model
  • Loading branch information
SIPC committed Nov 11, 2024
1 parent f0cd730 commit cbc6bc4
Show file tree
Hide file tree
Showing 10 changed files with 896 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/deepl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// deepl.js
// https://github.com/OwO-Network/DeepLX
import { query } from '@ifyour/deeplx';

async function deeplTranslate(text, source_lang, target_lang) {
try {
const result = await query({ text, source_lang, target_lang })

return JSON.stringify({
"model": "deepl",
"raw": text,
"text": result.data,
"from": result.source_lang,
"to": result.target_lang
});
} catch (error) {
console.error('Deepl error:', error);
return null;
}
}

export { deeplTranslate };
21 changes: 21 additions & 0 deletions lib/google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// google.js
import translate from 'google-translate-api-x';


async function googleTranslate(text, source_lang, target_lang) {
try {
const result = await translate(text, { from: source_lang, to: target_lang })
return JSON.stringify({
"model": "google",
"raw": text,
"text": result.text,
"from": result.raw[1][3],
"to": result.raw[1][1]
});
} catch (error) {
console.error('Google error:', error);
return null;
}
}

export { googleTranslate };
38 changes: 38 additions & 0 deletions lib/microsoft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// microsoft.js
import axios from "axios";
import { decode } from "jsonwebtoken";

var token;

async function TestToken() {
if (!token || decode(token).exp <= Date.now() / 1000) {
const auth = await axios.get("https://edge.microsoft.com/translate/auth");
token = auth.data;
}
}

async function microsoftTranslate(text, source_lang, target_lang) {
try {
await TestToken();
const apiUrl = `https://api.cognitive.microsofttranslator.com/translate?from=${source_lang}&to=${target_lang}&api-version=3.0&includeSentenceLength=true`;
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
};
const textObject = [{ text: text }];
const response = await axios.post(apiUrl, textObject, { headers });
response.data.code = response.status
return JSON.stringify({
"model": "microsoft",
"raw": text,
"text": response.data[0].translations[0].text,
"from": source_lang,
"to": response.data[0].translations[0].to
});
} catch (error) {
console.error('Microsoft error:', error);
return null;
}
}

export { microsoftTranslate };
43 changes: 43 additions & 0 deletions lib/transmart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// transmart.js
import axios from "axios";

function initData(text, from, to) {
return {
header: {
fn: "auto_translation",
client_key:
"browser-edge-chromium-123.0.0-Windows_10-793961b6-556c-46fd-8092-8f975f2335d3-1708529948457",
},
type: "plain",
model_category: "normal",
source: { lang: from, text_list: [text] },
target: { lang: to },
};
}

async function transmartTranslate(text, source_lang, target_lang) {
try {
const post_str = JSON.stringify(initData(text, source_lang, target_lang));
const response = await axios.post("https://yi.qq.com/api/imt", post_str, {
headers: {
"Content-Type": "application/json",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
referer: "https://yi.qq.com/zh-CN/index",
},
});
response.data.code = response.status
return JSON.stringify({
"model": "transmart",
"raw": text,
"text": response.data.auto_translation[0],
"from": response.data.src_lang,
"to": response.data.tgt_lang
});
} catch (error) {
console.error('Transmart error:', error);
return null;
}
}

export { transmartTranslate };
93 changes: 93 additions & 0 deletions lib/youdao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// youdao.js
// 参考 https://blog.bingyue.top/2024/08/24/youdao_fanyi
import { createHash, createDecipheriv } from "crypto";
import axios from "axios";

async function get_sign() {
const headers = {
"Referer": "https://fanyi.youdao.com/",
"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
};

const mysticTime = Math.floor(Date.now());
const sign = createHash('md5').update(`client=fanyideskweb&mysticTime=${mysticTime}&product=webfanyi&key=asdjnjfenknafdfsdfsd`).digest("hex");

const url = "https://dict.youdao.com/webtranslate/key?" +
`keyid=webfanyi-key-getter&sign=${sign}` +
"&client=fanyideskweb" +
"&product=webfanyi" +
"&appVersion=1.0.0&vendor=web" +
"&pointParam=client,mysticTime,product" +
`&mysticTime=${mysticTime}&keyfrom=fanyi.web` +
"&mid=1" +
"&screen=1" +
"&model=1" +
"&network=wifi" +
"&abtest=0" +
"&yduuid=abcdefg";

const response = await axios.get(url, { headers });
return response.data["data"];
}

function T(o) {
return createHash('md5').update(o).digest();
}

async function Translate(text, source_lang, target_lang) {
const v = await get_sign();
const aeskey = v["aesKey"];
const aesiv = v["aesIv"];
const url = "https://dict.youdao.com/webtranslate";
const mysticTime = Math.floor(Date.now());
const sign = createHash('md5').update(`client=fanyideskweb&mysticTime=${mysticTime}&product=webfanyi&key=fsdsogkndfokasodnaso`).digest("hex");

const m = `i=${text}&from=${source_lang}&to=${target_lang}&useTerm=false&dictResult=true&keyid=webfanyi&sign=${sign}&client=fanyideskweb&product=webfanyi&appVersion=1.0.0&vendor=web&pointParam=client%2CmysticTime%2Cproduct&mysticTime=${mysticTime}&keyfrom=fanyi.web&mid=1&screen=1&model=1&network=wifi&abtest=0&yduuid=abcdefg`;

const headers = {
"Content-type": "application/x-www-form-urlencoded",
"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
"Referer": "https://fanyi.youdao.com/",
"Cookie": "[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=1;"
};

const resp = await axios.post(url, m, { headers });
const resp_text = resp.data;

const a = Buffer.from(T(aeskey).toJSON().data);
const n = Buffer.from(T(aesiv).toJSON().data);
const r = createDecipheriv('aes-128-cbc', a, n);
let l = r.update(resp_text, 'base64', 'utf-8');
l += r.final('utf-8');

const t = JSON.parse(l);

let ans = "";
for (let index = 0; index < t["translateResult"][0].length; index++) {
const translation = t["translateResult"][0][index];
if (translation && translation["tgt"]) {
ans += translation["tgt"];
}
}

return ans
}

async function youdaoTranslate(text, source_lang, target_lang) {
try {
const result = await Translate(text, source_lang, target_lang)
return JSON.stringify({
"model": "youdao",
"raw": text,
"text": result,
"from": source_lang,
"to": target_lang
});

} catch (error) {
console.error('Youdao error:', error);
return null;
}
}

export { youdaoTranslate };
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Fanyi-Node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "node index.js"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@ifyour/deeplx": "^0.1.17",
"axios": "^1.7.7",
"fastify": "^5.1.0",
"fastify-lcache": "^2.2.1",
"google-translate-api-x": "^10.7.1",
"jsonwebtoken": "^9.0.2"
}
}
Loading

0 comments on commit cbc6bc4

Please sign in to comment.