Skip to content

Commit

Permalink
fix: Translate source "Bing" unavailable
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
chunibyocola committed Sep 27, 2021
1 parent 03ff2a3 commit 837196e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
90 changes: 46 additions & 44 deletions src/public/translate/bing/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ export const audio = async ({ text, from = '', com = true }: AudioParams) => {
};

let authorization = {
expiry: '0',
expiry: 0,
region: '',
token: ''
};
const getAuthorization = async (com: boolean) => {
if (Math.floor(new Date().getTime() / 1000) < Number.parseInt(authorization.expiry)) return authorization;
const timestamp = Number(new Date());

const url = `https://${com ? 'www' : 'cn'}.bing.com/tfetspktok`;
if (timestamp < authorization.expiry) { return authorization; }

const { token, key } = await getTokenAndKey(com);
const { token, key, IG, IID } = await getTokenAndKey(com);

const url = `https://${com ? 'www' : 'cn'}.bing.com/tfetspktok?isVertical=1&IG=${IG}&IID=${IID}`;

const searchParams = new URLSearchParams();
searchParams.append('token', token);
Expand All @@ -74,7 +76,7 @@ const getAuthorization = async (com: boolean) => {
try {
const data = await res.json();

authorization = { expiry: data.expiry, region: data.region, token: data.token };
authorization = { expiry: timestamp + Number(data.expiryDurationInMS), region: data.region, token: data.token };

return authorization;
} catch (err) {
Expand All @@ -86,45 +88,45 @@ const getXMLParams = (langCode: string) => {
let t = '', i = '', r = '';

switch (langCode) {
case "ar": t = "ar-SA"; i = "Male"; r = "ar-SA-Naayf"; break;
case "bg": t = "bg-BG"; i = "Male"; r = "bg-BG-Ivan"; break;
case "ca": t = "ca-ES"; i = "Female"; r = "ca-ES-HerenaRUS"; break;
case "cs": t = "cs-CZ"; i = "Male"; r = "cs-CZ-Jakub"; break;
case "da": t = "da-DK"; i = "Female"; r = "da-DK-HelleRUS"; break;
case "de": t = "de-DE"; i = "Female"; r = "de-DE-Hedda"; break;
case "el": t = "el-GR"; i = "Male"; r = "el-GR-Stefanos"; break;
case "en": t = "en-US"; i = "Female"; r = "en-US-JessaRUS"; break;
case "es": t = "es-ES"; i = "Female"; r = "es-ES-Laura-Apollo"; break;
case "fi": t = "fi-FI"; i = "Female"; r = "fi-FI-HeidiRUS"; break;
case "fr": t = "fr-FR"; i = "Female"; r = "fr-FR-Julie-Apollo"; break;
case "he": t = "he-IL"; i = "Male"; r = "he-IL-Asaf"; break;
case "hi": t = "hi-IN"; i = "Female"; r = "hi-IN-Kalpana-Apollo"; break;
case "hr": t = "hr-HR"; i = "Male"; r = "hr-HR-Matej"; break;
case "hu": t = "hu-HU"; i = "Male"; r = "hu-HU-Szabolcs"; break;
case "id": t = "id-ID"; i = "Male"; r = "id-ID-Andika"; break;
case "it": t = "it-IT"; i = "Male"; r = "it-IT-Cosimo-Apollo"; break;
case "ja": t = "ja-JP"; i = "Female"; r = "ja-JP-Ayumi-Apollo"; break;
case "ko": t = "ko-KR"; i = "Female"; r = "ko-KR-HeamiRUS"; break;
case "ms": t = "ms-MY"; i = "Male"; r = "ms-MY-Rizwan"; break;
case "nl": t = "nl-NL"; i = "Female"; r = "nl-NL-HannaRUS"; break;
case "nb": t = "nb-NO"; i = "Female"; r = "nb-NO-HuldaRUS"; break;
case "no": t = "nb-NO"; i = "Female"; r = "nb-NO-HuldaRUS"; break;
case "pl": t = "pl-PL"; i = "Female"; r = "pl-PL-PaulinaRUS"; break;
case "pt": t = "pt-PT"; i = "Female"; r = "pt-PT-HeliaRUS"; break;
case "pt-pt": t = "pt-PT"; i = "Female"; r = "pt-PT-HeliaRUS"; break;
case "ro": t = "ro-RO"; i = "Male"; r = "ro-RO-Andrei"; break;
case "ru": t = "ru-RU"; i = "Female"; r = "ru-RU-Irina-Apollo"; break;
case "sk": t = "sk-SK"; i = "Male"; r = "sk-SK-Filip"; break;
case "sl": t = "sl-SL"; i = "Male"; r = "sl-SI-Lado"; break;
case "sv": t = "sv-SE"; i = "Female"; r = "sv-SE-HedvigRUS"; break;
case "ta": t = "ta-IN"; i = "Female"; r = "ta-IN-Valluvar"; break;
case "te": t = "te-IN"; i = "Male"; r = "te-IN-Chitra"; break;
case "th": t = "th-TH"; i = "Male"; r = "th-TH-Pattara"; break;
case "tr": t = "tr-TR"; i = "Female"; r = "tr-TR-SedaRUS"; break;
case "vi": t = "vi-VN"; i = "Male"; r = "vi-VN-An"; break;
case "zh-Hans": t = "zh-CN"; i = "Female"; r = "zh-CN-HuihuiRUS"; break;
case "zh-Hant": t = "zh-CN"; i = "Female"; r = "zh-CN-HuihuiRUS"; break;
case "yue": t = "zh-HK"; i = "Female"; r = "zh-HK-TracyRUS"; break;
case "ar": t = "ar-SA"; i = "Male"; r = "ar-SA-HamedNeural"; break;
case "bg": t = "bg-BG"; i = "Male"; r = "bg-BG-BorislavNeural"; break;
case "ca": t = "ca-ES"; i = "Female"; r = "ca-ES-JoanaNeural"; break;
case "cs": t = "cs-CZ"; i = "Male"; r = "cs-CZ-AntoninNeural"; break;
case "da": t = "da-DK"; i = "Female"; r = "da-DK-ChristelNeural"; break;
case "de": t = "de-DE"; i = "Female"; r = "de-DE-KatjaNeural"; break;
case "el": t = "el-GR"; i = "Male"; r = "el-GR-NestorasNeural"; break;
case "en": t = "en-US"; i = "Female"; r = "en-US-AriaNeural"; break;
case "es": t = "es-ES"; i = "Female"; r = "es-ES-ElviraNeural"; break;
case "fi": t = "fi-FI"; i = "Female"; r = "fi-FI-NooraNeural"; break;
case "fr": t = "fr-FR"; i = "Female"; r = "fr-FR-DeniseNeural"; break
case "fr-CA": t = "fr-CA"; i = "Female"; r = "fr-CA-SylvieNeural"; break;
case "he": t = "he-IL"; i = "Male"; r = "he-IL-AvriNeural"; break;
case "hi": t = "hi-IN"; i = "Female"; r = "hi-IN-SwaraNeural"; break;
case "hr": t = "hr-HR"; i = "Male"; r = "hr-HR-SreckoNeural"; break;
case "hu": t = "hu-HU"; i = "Male"; r = "hu-HU-TamasNeural"; break;
case "id": t = "id-ID"; i = "Male"; r = "id-ID-ArdiNeural"; break;
case "it": t = "it-IT"; i = "Male"; r = "it-IT-DiegoNeural"; break;
case "ja": t = "ja-JP"; i = "Female"; r = "ja-JP-NanamiNeural"; break;
case "ko": t = "ko-KR"; i = "Female"; r = "ko-KR-SunHiNeural"; break;
case "ms": t = "ms-MY"; i = "Male"; r = "ms-MY-OsmanNeural"; break;
case "nl": t = "nl-NL"; i = "Female"; r = "nl-NL-ColetteNeural"; break;
case "nb": t = "nb-NO"; i = "Female"; r = "nb-NO-PernilleNeural"; break;
case "pl": t = "pl-PL"; i = "Female"; r = "pl-PL-ZofiaNeural"; break;
case "pt": t = "pt-BR"; i = "Female"; r = "pt-BR-FranciscaNeural"; break;
case "pt-PT": t = "pt-PT"; i = "Female"; r = "pt-PT-FernandaNeural"; break;
case "ro": t = "ro-RO"; i = "Male"; r = "ro-RO-EmilNeural"; break;
case "ru": t = "ru-RU"; i = "Female"; r = "ru-RU-DariyaNeural"; break;
case "sk": t = "sk-SK"; i = "Male"; r = "sk-SK-LukasNeural"; break;
case "sl": t = "sl-SI"; i = "Male"; r = "sl-SI-RokNeural"; break;
case "sv": t = "sv-SE"; i = "Female"; r = "sv-SE-SofieNeural"; break;
case "ta": t = "ta-IN"; i = "Female"; r = "ta-IN-PallaviNeural"; break;
case "te": t = "te-IN"; i = "Male"; r = "te-IN-ShrutiNeural"; break;
case "th": t = "th-TH"; i = "Male"; r = "th-TH-NiwatNeural"; break;
case "tr": t = "tr-TR"; i = "Female"; r = "tr-TR-EmelNeural"; break;
case "vi": t = "vi-VN"; i = "Male"; r = "vi-VN-NamMinhNeural"; break;
case "zh-Hans": t = "zh-CN"; i = "Female"; r = "zh-CN-XiaoxiaoNeural"; break;
case "zh-Hant": t = "zh-CN"; i = "Female"; r = "zh-CN-XiaoxiaoNeural"; break;
case "yue": t = "zh-HK"; i = "Female"; r = "zh-HK-HiuGaaiNeural"; break;
default: break;
};

Expand Down
4 changes: 2 additions & 2 deletions src/public/translate/bing/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { getTokenAndKey } from './getTokenAndKey';
import { DetectParams } from '../translate-types';

export const detect = async ({ text, com = true }: DetectParams) => {
const url = `https://${com ? 'www' : 'cn'}.bing.com/ttranslatev3`;
const { token, key, IG, IID } = await getTokenAndKey(com);

const { token, key } = await getTokenAndKey(com);
const url = `https://${com ? 'www' : 'cn'}.bing.com/ttranslatev3?isVertical=1&IG=${IG}&IID=${IID}`;

let searchParams = new URLSearchParams();
searchParams.append('fromLang', 'auto-detect');
Expand Down
7 changes: 5 additions & 2 deletions src/public/translate/bing/getTokenAndKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ let token = '';
let key = 0;
let duration = 0;
let expiry = 0;
let IG = 'IG';
const IID = 'translator.5023.1';

export const getTokenAndKey = async (com: boolean) => {
const currentTime = Number(new Date());

if (token && key && expiry && expiry > currentTime) { return { key, token }; }
if (token && key && expiry && expiry > currentTime) { return { key, token, IG, IID }; }

const res = await fetchData(`https://${com ? 'www' : 'cn'}.bing.com/translator`);
const text = await res.text();
const code = text.match(/params_RichTranslateHelper = \[.*?\]/g)![0].split('[')[1].replace(/"|\]/g, '');
IG = text.match(/(?<=,IG:")[a-zA-Z0-9]+(?=")/)![0];
const [tKey, tToken, tDuration] = code.split(',');
key = Number(tKey);
duration = Number(tDuration);
expiry = currentTime + duration;
token = tToken;

return { key, token };
return { key, token, IG, IID };
};
8 changes: 4 additions & 4 deletions src/public/translate/bing/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ type FetchFromBingParams = {
};

const fetchDictFromBing = async ({ text, from, to, com }: FetchFromBingParams) => {
const url = `https://${com ? 'www' : 'cn'}.bing.com/tlookupv3`;
const { token, key, IG, IID } = await getTokenAndKey(com);

const { token, key } = await getTokenAndKey(com);
const url = `https://${com ? 'www' : 'cn'}.bing.com/tlookupv3?isVertical=1&IG=${IG}&IID=${IID}`;

const searchParams = new URLSearchParams();
searchParams.append('from', from);
Expand All @@ -90,9 +90,9 @@ const fetchDictFromBing = async ({ text, from, to, com }: FetchFromBingParams) =
};

const fetchResultFromBing = async ({ text, from, to, com }: FetchFromBingParams) => {
const url = `https://${com ? 'www' : 'cn'}.bing.com/ttranslatev3`;
const { token, key, IG, IID } = await getTokenAndKey(com);

const { token, key } = await getTokenAndKey(com);
const url = `https://${com ? 'www' : 'cn'}.bing.com/ttranslatev3?isVertical=1&IG=${IG}&IID=${IID}`;

const searchParams = new URLSearchParams();
searchParams.append('fromLang', from);
Expand Down

0 comments on commit 837196e

Please sign in to comment.