2021-03-25 16:48:46 +01:00
|
|
|
import UserAgent from "user-agents";
|
2021-03-10 01:16:52 +01:00
|
|
|
import cheerio from "cheerio";
|
2022-02-24 19:53:31 +01:00
|
|
|
import { replaceBoth, LangCode } from "./language";
|
2021-03-10 01:16:52 +01:00
|
|
|
|
|
|
|
|
export async function googleScrape(
|
2022-02-24 19:53:31 +01:00
|
|
|
source: LangCode,
|
|
|
|
|
target: LangCode,
|
2021-03-10 01:16:52 +01:00
|
|
|
query: string
|
|
|
|
|
): Promise<{
|
2021-08-30 21:35:22 +02:00
|
|
|
translationRes: string
|
|
|
|
|
} | {
|
|
|
|
|
errorMsg: string
|
2021-03-10 01:16:52 +01:00
|
|
|
}> {
|
2021-03-10 21:31:50 +01:00
|
|
|
const parsed = replaceBoth("mapping", { source, target });
|
2021-03-12 16:18:26 +01:00
|
|
|
const res = await fetch(
|
2021-03-25 16:48:46 +01:00
|
|
|
`https://translate.google.com/m?sl=${parsed.source}&tl=${parsed.target}&q=${encodeURIComponent(query)}`,
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
"User-Agent": new UserAgent().toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
).catch(
|
|
|
|
|
() => null
|
|
|
|
|
);
|
2021-03-12 16:18:26 +01:00
|
|
|
|
2021-03-28 23:17:47 +02:00
|
|
|
if (!res?.ok)
|
2021-03-12 16:18:26 +01:00
|
|
|
return {
|
|
|
|
|
errorMsg: "An error occurred while retrieving the translation"
|
|
|
|
|
}
|
2021-03-10 01:16:52 +01:00
|
|
|
|
|
|
|
|
const html = await res.text();
|
2021-03-18 23:47:12 +01:00
|
|
|
const translationRes = cheerio.load(html)(".result-container").text().trim();
|
2021-03-12 16:18:26 +01:00
|
|
|
|
2021-03-18 23:47:12 +01:00
|
|
|
return translationRes
|
2021-03-12 16:18:26 +01:00
|
|
|
? {
|
2021-03-18 23:47:12 +01:00
|
|
|
translationRes
|
2021-03-12 16:18:26 +01:00
|
|
|
} : {
|
|
|
|
|
errorMsg: "An error occurred while parsing the translation"
|
|
|
|
|
};
|
2021-03-10 01:16:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function extractSlug(slug: string[]): {
|
|
|
|
|
source?: string,
|
|
|
|
|
target?: string,
|
|
|
|
|
query?: string
|
|
|
|
|
} {
|
|
|
|
|
const [p1, p2, p3] = slug;
|
|
|
|
|
switch (slug.length) {
|
|
|
|
|
case 1:
|
|
|
|
|
return { query: p1 };
|
|
|
|
|
case 2:
|
2021-03-12 16:18:26 +01:00
|
|
|
return { target: p1, query: p2 };
|
2021-03-10 01:16:52 +01:00
|
|
|
case 3:
|
2021-03-12 16:18:26 +01:00
|
|
|
return { source: p1, target: p2, query: p3 };
|
2021-03-10 01:16:52 +01:00
|
|
|
default:
|
2021-03-12 16:18:26 +01:00
|
|
|
return {};
|
2021-03-10 01:16:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-25 16:48:46 +01:00
|
|
|
|
2022-02-24 19:53:31 +01:00
|
|
|
export async function textToSpeechScrape(lang: LangCode, text: string) {
|
|
|
|
|
const { target: parsedLang } = replaceBoth("mapping", { source: "auto", target: lang });
|
2021-03-25 16:48:46 +01:00
|
|
|
|
|
|
|
|
const lastSpace = text.lastIndexOf(" ", 200);
|
|
|
|
|
const slicedText = text.slice(0, text.length > 200 && lastSpace !== -1 ? lastSpace : 200);
|
|
|
|
|
|
|
|
|
|
const res = await fetch(
|
2021-08-28 17:08:34 +03:00
|
|
|
`https://translate.google.com/translate_tts?tl=${parsedLang}&q=${encodeURIComponent(slicedText)}&textlen=${slicedText.length}&client=tw-ob`,
|
2021-03-25 16:48:46 +01:00
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
"User-Agent": new UserAgent().toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
).catch(
|
|
|
|
|
() => null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return res?.ok
|
|
|
|
|
? res.blob().then(blob => blob.arrayBuffer()).then(buffer => Array.from(new Uint8Array(buffer)))
|
|
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|