Replace Google Translate API package

This commit is contained in:
Cohee 2024-10-12 10:47:05 +03:00
parent ffa8716d07
commit 89c0b0e1cd
3 changed files with 20 additions and 51 deletions

19
package-lock.json generated
View File

@ -24,7 +24,7 @@
"csrf-csrf": "^2.2.3",
"express": "^4.21.0",
"form-data": "^4.0.0",
"google-translate-api-browser": "^3.0.1",
"google-translate-api-x": "^10.7.1",
"helmet": "^7.1.0",
"html-entities": "^2.5.2",
"iconv-lite": "^0.6.3",
@ -3689,11 +3689,18 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/google-translate-api-browser": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz",
"integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==",
"license": "MIT"
"node_modules/google-translate-api-x": {
"version": "10.7.1",
"resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz",
"integrity": "sha512-OdZDS6jRWzn1woOk62aOKQ5OyVaJSA+eyc6CktOWxo36IWfstOjwG/dkvnGl3Z2Sbpmk1A+jc2WwrBiRjqaY2A==",
"license": "MIT",
"engines": {
"node": ">=14.0.0"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/AidanWelch"
}
},
"node_modules/gopd": {
"version": "1.0.1",

View File

@ -14,7 +14,7 @@
"csrf-csrf": "^2.2.3",
"express": "^4.21.0",
"form-data": "^4.0.0",
"google-translate-api-browser": "^3.0.1",
"google-translate-api-x": "^10.7.1",
"helmet": "^7.1.0",
"html-entities": "^2.5.2",
"iconv-lite": "^0.6.3",

View File

@ -1,14 +1,9 @@
import https from 'node:https';
import { createRequire } from 'node:module';
import { Buffer } from 'node:buffer';
import fetch from 'node-fetch';
import express from 'express';
import iconv from 'iconv-lite';
import bingTranslateApi from 'bing-translate-api';
const require = createRequire(import.meta.url);
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
import googleTranslateApi from 'google-translate-api-x';
import { readSecret, SECRET_KEYS } from './secrets.js';
import { getConfigValue, uuidv4 } from '../util.js';
@ -17,20 +12,6 @@ import { jsonParser } from '../express-common.js';
const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate';
const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
/**
* Tries to decode a Node.js Buffer to a string using iconv-lite for UTF-8.
* @param {Buffer} buffer Node.js Buffer
* @returns {string} Decoded string
*/
function decodeBuffer(buffer) {
try {
return iconv.decode(buffer, 'utf-8');
} catch (error) {
console.log('Failed to decode buffer:', error);
return buffer.toString('utf-8');
}
}
export const router = express.Router();
router.post('/libre', jsonParser, async (request, response) => {
@ -100,31 +81,12 @@ router.post('/google', jsonParser, async (request, response) => {
console.log('Input text: ' + text);
const url = generateRequestUrl(text, { to: lang });
const result = await googleTranslateApi(text, { to: lang, forceBatch: false });
const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text;
https.get(url, (resp) => {
const data = [];
resp.on('data', (chunk) => {
data.push(chunk);
});
resp.on('end', () => {
try {
const decodedData = decodeBuffer(Buffer.concat(data));
const result = normaliseResponse(JSON.parse(decodedData));
console.log('Translated text: ' + result.text);
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
return response.send(result.text);
} catch (error) {
console.log('Translation error', error);
return response.sendStatus(500);
}
});
}).on('error', (err) => {
console.log('Translation error: ' + err.message);
return response.sendStatus(500);
});
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
console.log('Translated text: ' + translatedText);
return response.send(translatedText);
} catch (error) {
console.log('Translation error', error);
return response.sendStatus(500);