Files
kiss-translator/src/apis/index.js

377 lines
7.0 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import queryString from "query-string";
2023-08-05 18:15:01 +08:00
import { fetchPolyfill } from "../libs/fetch";
2023-07-20 13:45:41 +08:00
import {
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
2023-09-02 16:57:09 +08:00
OPT_TRANS_DEEPL,
2023-10-20 17:44:48 +08:00
OPT_TRANS_DEEPLFREE,
2023-10-17 11:33:26 +08:00
OPT_TRANS_DEEPLX,
2023-10-20 17:44:48 +08:00
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
2023-07-20 13:45:41 +08:00
OPT_TRANS_OPENAI,
2023-09-06 00:25:46 +08:00
OPT_TRANS_CUSTOMIZE,
2023-10-20 17:44:48 +08:00
URL_CACHE_TRAN,
2023-07-20 13:45:41 +08:00
OPT_LANGS_SPECIAL,
PROMPT_PLACE_FROM,
PROMPT_PLACE_TO,
2023-08-20 23:30:08 +08:00
KV_SALT_SYNC,
2023-10-10 18:03:05 +08:00
URL_BAIDU_LANGDETECT,
2023-10-20 17:44:48 +08:00
URL_MICROSOFT_TRAN,
2023-10-10 18:03:05 +08:00
OPT_LANGS_BAIDU,
2023-07-20 13:45:41 +08:00
} from "../config";
2023-08-31 13:38:06 +08:00
import { tryDetectLang } from "../libs";
2023-08-20 23:30:08 +08:00
import { sha256 } from "../libs/utils";
2023-10-20 17:44:48 +08:00
import { apiDeepLFreeTranslate } from "./deepl";
import { apiBaiduTranslate } from "./baidu";
import { apiTencentTranslate } from "./tencent";
2023-07-20 13:45:41 +08:00
2023-07-31 15:08:51 +08:00
/**
* 同步数据
* @param {*} url
* @param {*} key
* @param {*} data
* @returns
*/
2023-09-20 17:47:23 +08:00
export const apiSyncData = async (url, key, data) =>
2023-08-21 23:46:42 +08:00
fetchPolyfill(url, {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`,
2023-07-31 15:08:51 +08:00
},
2023-08-21 23:46:42 +08:00
method: "POST",
body: JSON.stringify(data),
});
2023-07-31 15:08:51 +08:00
2023-08-30 18:05:37 +08:00
/**
2023-09-08 21:41:32 +08:00
* 下载数据
2023-08-30 18:05:37 +08:00
* @param {*} url
* @returns
*/
2023-09-20 17:47:23 +08:00
export const apiFetch = (url) => fetchPolyfill(url);
2023-08-30 18:05:37 +08:00
2023-07-20 13:45:41 +08:00
/**
* 谷歌翻译
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
*/
2023-09-06 14:57:02 +08:00
const apiGoogleTranslate = async (
translator,
text,
to,
from,
{ url, key, useCache = true }
) => {
2023-07-20 13:45:41 +08:00
const params = {
client: "gtx",
dt: "t",
dj: 1,
ie: "UTF-8",
sl: from,
tl: to,
q: text,
};
2023-09-06 00:25:46 +08:00
const input = `${url}?${queryString.stringify(params)}`;
2023-09-06 14:57:02 +08:00
const res = await fetchPolyfill(input, {
headers: {
"Content-type": "application/json",
2023-07-20 13:45:41 +08:00
},
2023-09-06 14:57:02 +08:00
useCache,
usePool: true,
translator,
2023-09-06 00:25:46 +08:00
token: key,
});
2023-09-06 14:57:02 +08:00
const trText = res.sentences.map((item) => item.trans).join(" ");
const isSame = to === res.src;
return [trText, isSame];
2023-07-20 13:45:41 +08:00
};
/**
* 微软翻译
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
*/
2023-09-06 14:57:02 +08:00
const apiMicrosoftTranslate = async (
translator,
text,
to,
from,
2023-10-20 17:44:48 +08:00
{ useCache = true }
2023-09-06 14:57:02 +08:00
) => {
2023-07-20 13:45:41 +08:00
const params = {
from,
to,
"api-version": "3.0",
};
2023-10-20 17:44:48 +08:00
const input = `${URL_MICROSOFT_TRAN}?${queryString.stringify(params)}`;
2023-09-06 14:57:02 +08:00
const res = await fetchPolyfill(input, {
headers: {
"Content-type": "application/json",
2023-07-20 13:45:41 +08:00
},
method: "POST",
body: JSON.stringify([{ Text: text }]),
2023-09-06 14:57:02 +08:00
useCache,
usePool: true,
translator,
});
2023-09-06 14:57:02 +08:00
const trText = res[0].translations[0].text;
const isSame = to === res[0].detectedLanguage?.language;
return [trText, isSame];
2023-07-20 13:45:41 +08:00
};
2023-09-02 16:57:09 +08:00
/**
* DeepL翻译
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
*/
2023-09-06 14:57:02 +08:00
const apiDeepLTranslate = async (
translator,
text,
to,
from,
{ url, key, useCache = true }
) => {
2023-09-02 16:57:09 +08:00
const data = {
text: [text],
target_lang: to,
// split_sentences: "0",
2023-09-02 16:57:09 +08:00
};
if (from) {
data.source_lang = from;
}
2023-09-06 14:57:02 +08:00
const res = await fetchPolyfill(url, {
2023-09-02 16:57:09 +08:00
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
2023-09-06 14:57:02 +08:00
useCache,
2023-09-02 16:57:09 +08:00
usePool: true,
translator,
2023-09-06 00:25:46 +08:00
token: key,
2023-09-02 16:57:09 +08:00
});
2023-09-06 14:57:02 +08:00
const trText = res.translations.map((item) => item.text).join(" ");
const isSame = to === res.translations[0].detected_source_language;
return [trText, isSame];
2023-09-02 16:57:09 +08:00
};
2023-10-17 11:33:26 +08:00
/**
* DeepLX翻译
* https://github.com/OwO-Network/DeepLX
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
*/
const apiDeepLXTranslate = async (
translator,
text,
to,
from,
{ url, key, useCache = true }
) => {
const data = {
text,
target_lang: to,
};
if (from) {
data.source_lang = from;
}
const res = await fetchPolyfill(url, {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
useCache,
usePool: true,
translator,
token: key,
});
const trText = res.data;
const isSame = to === res.source_lang;
return [trText, isSame];
};
2023-07-20 13:45:41 +08:00
/**
* OpenAI 翻译
2023-07-31 03:10:09 +08:00
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
2023-07-20 13:45:41 +08:00
*/
2023-09-06 14:57:02 +08:00
const apiOpenaiTranslate = async (
translator,
text,
to,
from,
{ url, key, model, prompt, useCache = true }
) => {
2023-09-06 00:25:46 +08:00
prompt = prompt
2023-07-20 13:45:41 +08:00
.replaceAll(PROMPT_PLACE_FROM, from)
.replaceAll(PROMPT_PLACE_TO, to);
2023-09-06 14:57:02 +08:00
const res = await fetchPolyfill(url, {
headers: {
"Content-type": "application/json",
2023-07-20 13:45:41 +08:00
},
method: "POST",
body: JSON.stringify({
2023-09-06 00:25:46 +08:00
model: model,
messages: [
{
role: "system",
content: prompt,
},
{
role: "user",
content: text,
},
],
temperature: 0,
max_tokens: 256,
}),
2023-09-06 14:57:02 +08:00
useCache,
usePool: true,
translator,
2023-09-06 00:25:46 +08:00
token: key,
});
2023-09-06 14:57:02 +08:00
const trText = res?.choices?.[0].message.content;
const sLang = await tryDetectLang(text);
const tLang = await tryDetectLang(trText);
const isSame = text === trText || (sLang && tLang && sLang === tLang);
return [trText, isSame];
2023-09-06 00:25:46 +08:00
};
/**
* 自定义接口 翻译
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
*/
2023-09-06 14:57:02 +08:00
const apiCustomTranslate = async (
translator,
text,
to,
from,
{ url, key, useCache = true }
) => {
const res = await fetchPolyfill(url, {
2023-09-06 00:25:46 +08:00
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify({
text,
from,
to,
}),
2023-09-06 14:57:02 +08:00
useCache,
2023-09-06 00:25:46 +08:00
usePool: true,
translator,
token: key,
});
2023-09-06 14:57:02 +08:00
const trText = res.text;
const isSame = to === res.from;
return [trText, isSame];
2023-07-20 13:45:41 +08:00
};
2023-10-10 18:03:05 +08:00
/**
* 百度语言识别
* @param {*} text
* @returns
*/
export const apiBaiduLangdetect = async (text) => {
const res = await fetchPolyfill(URL_BAIDU_LANGDETECT, {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify({
query: text,
}),
useCache: true,
});
if (res.error === 0) {
return OPT_LANGS_BAIDU.get(res.lan) ?? res.lan;
}
return "";
};
2023-07-20 13:45:41 +08:00
/**
* 统一翻译接口
* @param {*} param0
* @returns
*/
2023-10-20 17:44:48 +08:00
export const apiTranslate = async ({
2023-08-30 18:05:37 +08:00
translator,
2023-09-06 14:57:02 +08:00
text,
2023-08-30 18:05:37 +08:00
fromLang,
toLang,
2023-10-20 17:44:48 +08:00
apiSetting = {},
useCache = true,
usePool = true,
2023-08-30 18:05:37 +08:00
}) => {
2023-10-20 17:44:48 +08:00
let trText = "";
let isSame = false;
2023-10-13 10:48:01 +08:00
2023-10-20 17:44:48 +08:00
const transOpts = {
translator,
text,
fromLang,
toLang,
};
2023-10-13 10:48:01 +08:00
2023-10-20 17:44:48 +08:00
const res = await fetchPolyfill(
`${URL_CACHE_TRAN}?${queryString.stringify(transOpts)}`,
{
useCache,
usePool,
transOpts,
apiSetting,
}
);
2023-07-20 13:45:41 +08:00
2023-09-06 14:57:02 +08:00
switch (translator) {
case OPT_TRANS_GOOGLE:
2023-10-20 17:44:48 +08:00
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_MICROSOFT:
2023-10-20 17:44:48 +08:00
trText = res[0].translations[0].text;
isSame = toLang === res[0].detectedLanguage?.language;
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_DEEPL:
2023-10-20 17:44:48 +08:00
break;
case OPT_TRANS_DEEPLFREE:
break;
2023-10-17 11:33:26 +08:00
case OPT_TRANS_DEEPLX:
2023-10-20 17:44:48 +08:00
break;
case OPT_TRANS_BAIDU:
break;
case OPT_TRANS_TENCENT:
trText = res.auto_translation;
isSame = text === trText;
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_OPENAI:
2023-10-20 17:44:48 +08:00
break;
2023-09-06 14:57:02 +08:00
case OPT_TRANS_CUSTOMIZE:
2023-10-20 17:44:48 +08:00
break;
2023-09-06 14:57:02 +08:00
default:
2023-10-20 17:44:48 +08:00
break;
2023-07-20 13:45:41 +08:00
}
2023-10-20 17:44:48 +08:00
return [trText, isSame];
2023-07-20 13:45:41 +08:00
};