Update bing translate API package, fix language codes

c4a92c95e68478c685358118899ea63c33ca0b61

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +72 -48Ignore whitespace
package-lock.json+4 -4
@@ -18,7 +18,7 @@
1818 "@popperjs/core": "^2.11.8",
1919 "@zeldafan0225/ai_horde": "^5.1.0",
2020 "archiver": "^7.0.1",
2121 "bing-translate-api": "^24.90.12",
2222 "body-parser": "^1.20.2",
2323 "bowser": "^2.11.0",
2424 "command-exists": "^1.2.9",
@@ -2161,9 +2161,9 @@
21612161 }
21622162 },
21632163 "node_modules/bing-translate-api": {
21642164 "version": "24.90.12",
21652165 "resolved": "https://registry.npmjs.org/bing-translate-api/-/bing-translate-api-24.90.12.tgz",
21662166 "integrity": "sha512-DaYqa7iupfj+fj/KeaeZSp5FUY/ZR4sZ6jqoTP0RHkYOUfo7wwoxlhYDkh4VcvBBzuVORnBEgdXBVQrfM4kk7gJJ8XUehnxzOhHU91oy86xEtp8OOMjVEjCZJX042fKxoO19NNvxJ5omeCcxQNFoPbDqVpBJwqiGVquL0oPdQm1Q==",
21672167 "license": "MIT",
21682168 "dependencies": {
21692169 "got": "^11.8.6"
package.json+1 -1
@@ -8,7 +8,7 @@
88 "@popperjs/core": "^2.11.8",
99 "@zeldafan0225/ai_horde": "^5.1.0",
1010 "archiver": "^7.0.1",
1111 "bing-translate-api": "^24.90.12",
1212 "body-parser": "^1.20.2",
1313 "bowser": "^2.11.0",
1414 "command-exists": "^1.2.9",
src/endpoints/translate.js+67 -43
@@ -1,9 +1,8 @@
1-import https from 'node:https';
21import { createRequire } from 'node:module';
32
43import fetch from 'node-fetch';
54import express from 'express';
65import bingTranslateApi{ translate as bingTranslate } from 'bing-translate-api';
76import iconv from 'iconv-lite';
87
98import { readSecret, SECRET_KEYS } from './secrets.js';
@@ -56,6 +55,10 @@ router.post('/libre', jsonParser, async (request, response) => {
5655 request.body.lang = 'zt';
5756 }
5857
58+ if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
59+ request.body.lang = 'pt';
60+ }
61+
5962 const text = request.body.text;
6063 const lang = request.body.lang;
6164
@@ -81,7 +84,7 @@ router.post('/libre', jsonParser, async (request, response) => {
8184 if (!result.ok) {
8285 const error = await result.text();
8386 console.log('LibreTranslate error: ', result.statusText, error);
8487 return response.sendStatus(result.status500);
8588 }
8689
8790 /** @type {any} */
@@ -130,6 +133,14 @@ router.post('/google', jsonParser, async (request, response) => {
130133
131134router.post('/yandex', jsonParser, async (request, response) => {
132135 try {
136+ if (request.body.lang === 'pt-PT') {
137+ request.body.lang = 'pt';
138+ }
139+
140+ if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
141+ request.body.lang = 'zh';
142+ }
143+
133144 const chunks = request.body.chunks;
134145 const lang = request.body.lang;
135146
@@ -185,6 +196,14 @@ router.post('/lingva', jsonParser, async (request, response) => {
185196 return response.sendStatus(400);
186197 }
187198
199+ if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
200+ request.body.lang = 'zh';
201+ }
202+
203+ if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
204+ request.body.lang = 'pt';
205+ }
206+
188207 const text = request.body.text;
189208 const lang = request.body.lang;
190209
@@ -193,29 +212,24 @@ router.post('/lingva', jsonParser, async (request, response) => {
193212 }
194213
195214 console.log('Input text: ' + text);
196- const url = `${baseUrl}/auto/${lang}/${encodeURIComponent(text)}`;
215+
197-
216+ try {
198- https.get(url, (resp) => {
217+ const url = `${baseUrl}/auto/${lang}/${encodeURIComponent(text)}`;
199218 letconst dataresult = ''await fetch(url);
200219
201- resp.on('data', (chunk) => {
220+ if (!result.ok) {
202- data += chunk;
221+ const error = await result.text();
203- });
222+ console.log('Lingva error: ', result.statusText, error);
204-
223+ }
205- resp.on('end', () => {
224+
206- try {
225+ /** @type {any} */
207226 const resultdata = JSONawait result.parsejson(data);
208227 console.log('Translated text: ' + resultdata.translation);
209228 return response.send(resultdata.translation);
210229 } catch (error) {
211230 console.log('Translation error:', error);
212- return response.sendStatus(500);
213- }
214- });
215- }).on('error', (err) => {
216- console.log('Translation error: ' + err.message);
217231 return response.sendStatus(500);
218232 });
219233 } catch (error) {
220234 console.log('Translation error', error);
221235 return response.sendStatus(500);
@@ -266,7 +280,7 @@ router.post('/deepl', jsonParser, async (request, response) => {
266280 if (!result.ok) {
267281 const error = await result.text();
268282 console.log('DeepL error: ', result.statusText, error);
269283 return response.sendStatus(result.status500);
270284 }
271285
272286 /** @type {any} */
@@ -319,7 +333,7 @@ router.post('/onering', jsonParser, async (request, response) => {
319333 if (!result.ok) {
320334 const error = await result.text();
321335 console.log('OneRing error: ', result.statusText, error);
322336 return response.sendStatus(result.status500);
323337 }
324338
325339 /** @type {any} */
@@ -375,7 +389,7 @@ router.post('/deeplx', jsonParser, async (request, response) => {
375389 if (!result.ok) {
376390 const error = await result.text();
377391 console.log('DeepLX error: ', result.statusText, error);
378392 return response.sendStatus(result.status500);
379393 }
380394
381395 /** @type {any} */
@@ -390,24 +404,34 @@ router.post('/deeplx', jsonParser, async (request, response) => {
390404});
391405
392406router.post('/bing', jsonParser, async (request, response) => {
393- const text = request.body.text;
407+ try {
394408 let const langtext = request.body.langtext;
409+ let lang = request.body.lang;
395410
396411 if (request.body.lang === 'zh-CN') {
397412 lang = 'zh-Hans';
398413 }
399414
400- if (!text || !lang) {
415+ if (request.body.lang === 'zh-TW') {
401- return response.sendStatus(400);
416+ lang = 'zh-Hant';
402417 }
403418
404- console.log('Input text: ' + text);
419+ if (request.body.lang === 'pt-BR') {
420+ lang = 'pt';
421+ }
422+
423+ if (!text || !lang) {
424+ return response.sendStatus(400);
425+ }
426+
427+ console.log('Input text: ' + text);
405428
406- bingTranslateApi.translate(text, null, lang).then(result => {
429+ const result = await bingTranslate(text, null, lang);
407430 console.log('Translated text:const 'translatedText += result?.translation);
408- return response.send(result.translation);
431+ console.log('Translated text: ' + translatedText);
409- }).catch(err => {
432+ return response.send(translatedText);
410- console.log('Translation error: ' + err.message);
433+ } catch (error) {
434+ console.log('Translation error', error);
411435 return response.sendStatus(500);
412436 });
413437});