Replace Google Translate API package
| @@ -24,7 +24,7 @@ | |||
| 24 | "csrf-csrf": "^2.2.3", | 24 | "csrf-csrf": "^2.2.3", |
| 25 | "express": "^4.21.0", | 25 | "express": "^4.21.0", |
| 26 | "form-data": "^4.0.0", | 26 | "form-data": "^4.0.0", |
| 27 | "google-translate-api-browser": "^3.0.1", | 27 | "google-translate-api-x": "^10.7.1", |
| 28 | "helmet": "^7.1.0", | 28 | "helmet": "^7.1.0", |
| 29 | "html-entities": "^2.5.2", | 29 | "html-entities": "^2.5.2", |
| 30 | "iconv-lite": "^0.6.3", | 30 | "iconv-lite": "^0.6.3", |
| @@ -3689,11 +3689,18 @@ | |||
| 3689 | "url": "https://github.com/sponsors/sindresorhus" | 3689 | "url": "https://github.com/sponsors/sindresorhus" |
| 3690 | } | 3690 | } |
| 3691 | }, | 3691 | }, |
| 3692 | "node_modules/google-translate-api-browser": { | 3692 | "node_modules/google-translate-api-x": { |
| 3693 | "version": "3.0.1", | 3693 | "version": "10.7.1", |
| 3694 | "resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz", | 3694 | "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz", |
| 3695 | "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==", | 3695 | "integrity": "sha512-OdZDS6jRWzn1woOk62aOKQ5OyVaJSA+eyc6CktOWxo36IWfstOjwG/dkvnGl3Z2Sbpmk1A+jc2WwrBiRjqaY2A==", |
| 3696 | "license": "MIT" | 3696 | "license": "MIT", |
| 3697 | "engines": { | ||
| 3698 | "node": ">=14.0.0" | ||
| 3699 | }, | ||
| 3700 | "funding": { | ||
| 3701 | "type": "github", | ||
| 3702 | "url": "https://github.com/sponsors/AidanWelch" | ||
| 3703 | } | ||
| 3697 | }, | 3704 | }, |
| 3698 | "node_modules/gopd": { | 3705 | "node_modules/gopd": { |
| 3699 | "version": "1.0.1", | 3706 | "version": "1.0.1", |
| @@ -14,7 +14,7 @@ | |||
| 14 | "csrf-csrf": "^2.2.3", | 14 | "csrf-csrf": "^2.2.3", |
| 15 | "express": "^4.21.0", | 15 | "express": "^4.21.0", |
| 16 | "form-data": "^4.0.0", | 16 | "form-data": "^4.0.0", |
| 17 | "google-translate-api-browser": "^3.0.1", | 17 | "google-translate-api-x": "^10.7.1", |
| 18 | "helmet": "^7.1.0", | 18 | "helmet": "^7.1.0", |
| 19 | "html-entities": "^2.5.2", | 19 | "html-entities": "^2.5.2", |
| 20 | "iconv-lite": "^0.6.3", | 20 | "iconv-lite": "^0.6.3", |
| @@ -1,14 +1,9 @@ | |||
| 1 | import https from 'node:https'; | 1 | import https from 'node:https'; |
| 2 | import { createRequire } from 'node:module'; | ||
| 3 | import { Buffer } from 'node:buffer'; | ||
| 4 | 2 | ||
| 5 | import fetch from 'node-fetch'; | 3 | import fetch from 'node-fetch'; |
| 6 | import express from 'express'; | 4 | import express from 'express'; |
| 7 | import iconv from 'iconv-lite'; | ||
| 8 | import bingTranslateApi from 'bing-translate-api'; | 5 | import bingTranslateApi from 'bing-translate-api'; |
| 9 | 6 | import googleTranslateApi from 'google-translate-api-x'; | |
| 10 | const require = createRequire(import.meta.url); | ||
| 11 | const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser'); | ||
| 12 | 7 | ||
| 13 | import { readSecret, SECRET_KEYS } from './secrets.js'; | 8 | import { readSecret, SECRET_KEYS } from './secrets.js'; |
| 14 | import { getConfigValue, uuidv4 } from '../util.js'; | 9 | import { getConfigValue, uuidv4 } from '../util.js'; |
| @@ -17,20 +12,6 @@ import { jsonParser } from '../express-common.js'; | |||
| 17 | const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate'; | 12 | const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate'; |
| 18 | const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate'; | 13 | const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate'; |
| 19 | 14 | ||
| 20 | /** | ||
| 21 | * Tries to decode a Node.js Buffer to a string using iconv-lite for UTF-8. | ||
| 22 | * @param {Buffer} buffer Node.js Buffer | ||
| 23 | * @returns {string} Decoded string | ||
| 24 | */ | ||
| 25 | function decodeBuffer(buffer) { | ||
| 26 | try { | ||
| 27 | return iconv.decode(buffer, 'utf-8'); | ||
| 28 | } catch (error) { | ||
| 29 | console.log('Failed to decode buffer:', error); | ||
| 30 | return buffer.toString('utf-8'); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | export const router = express.Router(); | 15 | export const router = express.Router(); |
| 35 | 16 | ||
| 36 | router.post('/libre', jsonParser, async (request, response) => { | 17 | router.post('/libre', jsonParser, async (request, response) => { |
| @@ -100,31 +81,12 @@ router.post('/google', jsonParser, async (request, response) => { | |||
| 100 | 81 | ||
| 101 | console.log('Input text: ' + text); | 82 | console.log('Input text: ' + text); |
| 102 | 83 | ||
| 103 | const url = generateRequestUrl(text, { to: lang }); | 84 | const result = await googleTranslateApi(text, { to: lang, forceBatch: false }); |
| 104 | 85 | const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text; | |
| 105 | https.get(url, (resp) => { | ||
| 106 | const data = []; | ||
| 107 | |||
| 108 | resp.on('data', (chunk) => { | ||
| 109 | data.push(chunk); | ||
| 110 | }); | ||
| 111 | 86 | ||
| 112 | resp.on('end', () => { | ||
| 113 | try { | ||
| 114 | const decodedData = decodeBuffer(Buffer.concat(data)); | ||
| 115 | const result = normaliseResponse(JSON.parse(decodedData)); | ||
| 116 | console.log('Translated text: ' + result.text); | ||
| 117 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); | 87 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); |
| 118 | return response.send(result.text); | 88 | console.log('Translated text: ' + translatedText); |
| 119 | } catch (error) { | 89 | return response.send(translatedText); |
| 120 | console.log('Translation error', error); | ||
| 121 | return response.sendStatus(500); | ||
| 122 | } | ||
| 123 | }); | ||
| 124 | }).on('error', (err) => { | ||
| 125 | console.log('Translation error: ' + err.message); | ||
| 126 | return response.sendStatus(500); | ||
| 127 | }); | ||
| 128 | } catch (error) { | 90 | } catch (error) { |
| 129 | console.log('Translation error', error); | 91 | console.log('Translation error', error); |
| 130 | return response.sendStatus(500); | 92 | return response.sendStatus(500); |