Merge pull request #1769 from berbant/patch-2

Lingva Patch 2
This commit is contained in:
Cohee 2024-01-31 22:32:08 +02:00 committed by GitHub
commit 5f159e7c6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 40 additions and 0 deletions

View File

@ -102,6 +102,46 @@ router.post('/google', jsonParser, async (request, response) => {
}
});
router.post('/lingva', jsonParser, async (request, response) => {
try {
const text = request.body.text;
const lang = request.body.lang;
if (!text || !lang) {
return response.sendStatus(400);
}
console.log('Input text: ' + text);
const url = readSecret(SECRET_KEYS.LINGVA_URL) + "/auto/" + lang + "/" + encodeURIComponent(text);
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
try {
const result = JSON.parse(data)
console.log('Translated text: ' + result.translation);
return response.send(result.translation);
} catch (error) {
console.log('Translation error', error);
return response.sendStatus(500);
}
});
}).on('error', (err) => {
console.log('Translation error: ' + err.message);
return response.sendStatus(500);
});
} catch (error) {
console.log('Translation error', error);
return response.sendStatus(500);
}
});
router.post('/deepl', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.DEEPL);