From 89c0b0e1cda75035853ab73ff0b902d3662bd370 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 12 Oct 2024 10:47:05 +0300 Subject: [PATCH] Replace Google Translate API package --- package-lock.json | 19 ++++++++++----- package.json | 2 +- src/endpoints/translate.js | 50 +++++--------------------------------- 3 files changed, 20 insertions(+), 51 deletions(-) diff --git a/package-lock.json b/package-lock.json index cdbaa662a..9109342b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 2e702c499..30aaa9d96 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/endpoints/translate.js b/src/endpoints/translate.js index 57b7a9a5a..57387b63c 100644 --- a/src/endpoints/translate.js +++ b/src/endpoints/translate.js @@ -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);