Skip to content

Instantly share code, notes, and snippets.

@WebKieth
Created January 2, 2023 15:53
Show Gist options
  • Save WebKieth/8dd0209830dbe40602e5f42dd139054c to your computer and use it in GitHub Desktop.
Save WebKieth/8dd0209830dbe40602e5f42dd139054c to your computer and use it in GitHub Desktop.
Using AI version of yandex translate with Node.js
const axios = require('axios')
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
/**
type IArticle = {
title: String,
text: String[],
url: String
}
texts: IArticle[] - array of articles to translate
*/
const translate = async (texts) => {
//Needs to preinstall yc utility. Used in ubuntu 22.04
const { stdout } = await exec('yc iam create-token')
const folderId = '<there is a place for yandex cloud folder ID>'
for (let [index, item] of texts.entries()) {
console.log('DEBUG data: ', item)
const res = await axios({
method: 'POST',
url: 'https://translate.api.cloud.yandex.net/translate/v2/translate',
data: {
targetLanguageCode: 'ru',
texts: item.text,
folderId
},
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${stdout.trim()}`
}
})
if (res?.data?.translations?.length > 0) {
// save translated text to article array item
const translated = [...res?.data?.translations.map(item => (item.text))]
texts[index]['translated'] = translated
}
}
// return translated array of articles
return texts
}
const TranslatorPromise = new Promise(async (resolve, reject) => {
try {
// for example, in test case I got the data presaved after scrapping
const file = await readFile('./saved/scrapped.json')
const data = JSON.parse(file)
let texts = []
Object.keys(data).forEach(sourceKey => {
const source = data[sourceKey]
if (!Array.isArray(source)) return
texts = [...source.map(item => {
const text = item.text.filter((item) => item !== 'ADVERTISEMENT' && item.length !== 0)
return {text, ...item}
}), ...texts]
})
const result = await translate(texts)
resolve(result)
} catch (e) {
if (e.response && e.response.status && e.response.data)
console.error('REQUEST ERROR: ', e.response.status, e.response.data)
}
});
TranslatorPromise.then(() => console.log('sucessfull')).catch(() => console.log('fail')).finally(() => console.log('completed'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment