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

302 lines
5.7 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-07-20 13:45:41 +08:00
OPT_TRANS_OPENAI,
2023-09-06 00:25:46 +08:00
OPT_TRANS_CUSTOMIZE,
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,
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-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,
{ url, useCache = true }
) => {
2023-07-20 13:45:41 +08:00
const params = {
from,
to,
"api-version": "3.0",
};
2023-09-06 14:57:02 +08:00
const input = `${url}?${queryString.stringify(params)}`;
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",
};
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-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-09-06 14:57:02 +08:00
export const apiTranslate = ({
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-09-06 14:57:02 +08:00
apiSetting,
2023-08-30 18:05:37 +08:00
}) => {
2023-10-13 10:48:01 +08:00
const from = OPT_LANGS_SPECIAL[translator].get(fromLang);
const to = OPT_LANGS_SPECIAL[translator].get(toLang);
if (!to) {
return ["", from === to];
}
2023-09-06 14:57:02 +08:00
const callApi = (api) => api(translator, text, to, from, apiSetting);
2023-07-20 13:45:41 +08:00
2023-09-06 14:57:02 +08:00
switch (translator) {
case OPT_TRANS_GOOGLE:
return callApi(apiGoogleTranslate);
case OPT_TRANS_MICROSOFT:
return callApi(apiMicrosoftTranslate);
case OPT_TRANS_DEEPL:
return callApi(apiDeepLTranslate);
case OPT_TRANS_OPENAI:
return callApi(apiOpenaiTranslate);
case OPT_TRANS_CUSTOMIZE:
return callApi(apiCustomTranslate);
default:
return ["", false];
2023-07-20 13:45:41 +08:00
}
};