Update bing translate API package, fix language codes
| @@ -18,7 +18,7 @@ | |||
| 18 | "@popperjs/core": "^2.11.8", | 18 | "@popperjs/core": "^2.11.8", |
| 19 | "@zeldafan0225/ai_horde": "^5.1.0", | 19 | "@zeldafan0225/ai_horde": "^5.1.0", |
| 20 | "archiver": "^7.0.1", | 20 | "archiver": "^7.0.1", |
| 21 | "bing-translate-api": "^2.9.1", | 21 | "bing-translate-api": "^4.0.2", |
| 22 | "body-parser": "^1.20.2", | 22 | "body-parser": "^1.20.2", |
| 23 | "bowser": "^2.11.0", | 23 | "bowser": "^2.11.0", |
| 24 | "command-exists": "^1.2.9", | 24 | "command-exists": "^1.2.9", |
| @@ -2161,9 +2161,9 @@ | |||
| 2161 | } | 2161 | } |
| 2162 | }, | 2162 | }, |
| 2163 | "node_modules/bing-translate-api": { | 2163 | "node_modules/bing-translate-api": { |
| 2164 | "version": "2.9.1", | 2164 | "version": "4.0.2", |
| 2165 | "resolved": "https://registry.npmjs.org/bing-translate-api/-/bing-translate-api-2.9.1.tgz", | 2165 | "resolved": "https://registry.npmjs.org/bing-translate-api/-/bing-translate-api-4.0.2.tgz", |
| 2166 | "integrity": "sha512-DaYqa7iupfj+fj/KeaeZSp5FUY/ZR4sZ6jqoTP0RHkYOUfo7wwoxlhYDkh4VcvBBzuVORnBEgdXBVQrfM4kk7g==", | 2166 | "integrity": "sha512-JJ8XUehnxzOhHU91oy86xEtp8OOMjVEjCZJX042fKxoO19NNvxJ5omeCcxQNFoPbDqVpBJwqiGVquL0oPdQm1Q==", |
| 2167 | "license": "MIT", | 2167 | "license": "MIT", |
| 2168 | "dependencies": { | 2168 | "dependencies": { |
| 2169 | "got": "^11.8.6" | 2169 | "got": "^11.8.6" |
| @@ -8,7 +8,7 @@ | |||
| 8 | "@popperjs/core": "^2.11.8", | 8 | "@popperjs/core": "^2.11.8", |
| 9 | "@zeldafan0225/ai_horde": "^5.1.0", | 9 | "@zeldafan0225/ai_horde": "^5.1.0", |
| 10 | "archiver": "^7.0.1", | 10 | "archiver": "^7.0.1", |
| 11 | "bing-translate-api": "^2.9.1", | 11 | "bing-translate-api": "^4.0.2", |
| 12 | "body-parser": "^1.20.2", | 12 | "body-parser": "^1.20.2", |
| 13 | "bowser": "^2.11.0", | 13 | "bowser": "^2.11.0", |
| 14 | "command-exists": "^1.2.9", | 14 | "command-exists": "^1.2.9", |
| @@ -1,9 +1,8 @@ | |||
| 1 | import https from 'node:https'; | ||
| 2 | import { createRequire } from 'node:module'; | 1 | import { createRequire } from 'node:module'; |
| 3 | 2 | ||
| 4 | import fetch from 'node-fetch'; | 3 | import fetch from 'node-fetch'; |
| 5 | import express from 'express'; | 4 | import express from 'express'; |
| 6 | import bingTranslateApi from 'bing-translate-api'; | 5 | import { translate as bingTranslate } from 'bing-translate-api'; |
| 7 | import iconv from 'iconv-lite'; | 6 | import iconv from 'iconv-lite'; |
| 8 | 7 | ||
| 9 | import { readSecret, SECRET_KEYS } from './secrets.js'; | 8 | import { readSecret, SECRET_KEYS } from './secrets.js'; |
| @@ -56,6 +55,10 @@ router.post('/libre', jsonParser, async (request, response) => { | |||
| 56 | request.body.lang = 'zt'; | 55 | request.body.lang = 'zt'; |
| 57 | } | 56 | } |
| 58 | 57 | ||
| 58 | if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') { | ||
| 59 | request.body.lang = 'pt'; | ||
| 60 | } | ||
| 61 | |||
| 59 | const text = request.body.text; | 62 | const text = request.body.text; |
| 60 | const lang = request.body.lang; | 63 | const lang = request.body.lang; |
| 61 | 64 | ||
| @@ -81,7 +84,7 @@ router.post('/libre', jsonParser, async (request, response) => { | |||
| 81 | if (!result.ok) { | 84 | if (!result.ok) { |
| 82 | const error = await result.text(); | 85 | const error = await result.text(); |
| 83 | console.log('LibreTranslate error: ', result.statusText, error); | 86 | console.log('LibreTranslate error: ', result.statusText, error); |
| 84 | return response.sendStatus(result.status); | 87 | return response.sendStatus(500); |
| 85 | } | 88 | } |
| 86 | 89 | ||
| 87 | /** @type {any} */ | 90 | /** @type {any} */ |
| @@ -130,6 +133,14 @@ router.post('/google', jsonParser, async (request, response) => { | |||
| 130 | 133 | ||
| 131 | router.post('/yandex', jsonParser, async (request, response) => { | 134 | router.post('/yandex', jsonParser, async (request, response) => { |
| 132 | try { | 135 | 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 | |||
| 133 | const chunks = request.body.chunks; | 144 | const chunks = request.body.chunks; |
| 134 | const lang = request.body.lang; | 145 | const lang = request.body.lang; |
| 135 | 146 | ||
| @@ -185,6 +196,14 @@ router.post('/lingva', jsonParser, async (request, response) => { | |||
| 185 | return response.sendStatus(400); | 196 | return response.sendStatus(400); |
| 186 | } | 197 | } |
| 187 | 198 | ||
| 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 | |||
| 188 | const text = request.body.text; | 207 | const text = request.body.text; |
| 189 | const lang = request.body.lang; | 208 | const lang = request.body.lang; |
| 190 | 209 | ||
| @@ -193,29 +212,24 @@ router.post('/lingva', jsonParser, async (request, response) => { | |||
| 193 | } | 212 | } |
| 194 | 213 | ||
| 195 | console.log('Input text: ' + text); | 214 | 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)}`; |
| 199 | let data = ''; | 218 | const result = await fetch(url); |
| 200 | 219 | ||
| 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} */ |
| 207 | const result = JSON.parse(data); | 226 | const data = await result.json(); |
| 208 | console.log('Translated text: ' + result.translation); | 227 | console.log('Translated text: ' + data.translation); |
| 209 | return response.send(result.translation); | 228 | return response.send(data.translation); |
| 210 | } catch (error) { | 229 | } catch (error) { |
| 211 | console.log('Translation error', error); | 230 | console.log('Translation error:', error); |
| 212 | return response.sendStatus(500); | ||
| 213 | } | ||
| 214 | }); | ||
| 215 | }).on('error', (err) => { | ||
| 216 | console.log('Translation error: ' + err.message); | ||
| 217 | return response.sendStatus(500); | 231 | return response.sendStatus(500); |
| 218 | }); | 232 | } |
| 219 | } catch (error) { | 233 | } catch (error) { |
| 220 | console.log('Translation error', error); | 234 | console.log('Translation error', error); |
| 221 | return response.sendStatus(500); | 235 | return response.sendStatus(500); |
| @@ -266,7 +280,7 @@ router.post('/deepl', jsonParser, async (request, response) => { | |||
| 266 | if (!result.ok) { | 280 | if (!result.ok) { |
| 267 | const error = await result.text(); | 281 | const error = await result.text(); |
| 268 | console.log('DeepL error: ', result.statusText, error); | 282 | console.log('DeepL error: ', result.statusText, error); |
| 269 | return response.sendStatus(result.status); | 283 | return response.sendStatus(500); |
| 270 | } | 284 | } |
| 271 | 285 | ||
| 272 | /** @type {any} */ | 286 | /** @type {any} */ |
| @@ -319,7 +333,7 @@ router.post('/onering', jsonParser, async (request, response) => { | |||
| 319 | if (!result.ok) { | 333 | if (!result.ok) { |
| 320 | const error = await result.text(); | 334 | const error = await result.text(); |
| 321 | console.log('OneRing error: ', result.statusText, error); | 335 | console.log('OneRing error: ', result.statusText, error); |
| 322 | return response.sendStatus(result.status); | 336 | return response.sendStatus(500); |
| 323 | } | 337 | } |
| 324 | 338 | ||
| 325 | /** @type {any} */ | 339 | /** @type {any} */ |
| @@ -375,7 +389,7 @@ router.post('/deeplx', jsonParser, async (request, response) => { | |||
| 375 | if (!result.ok) { | 389 | if (!result.ok) { |
| 376 | const error = await result.text(); | 390 | const error = await result.text(); |
| 377 | console.log('DeepLX error: ', result.statusText, error); | 391 | console.log('DeepLX error: ', result.statusText, error); |
| 378 | return response.sendStatus(result.status); | 392 | return response.sendStatus(500); |
| 379 | } | 393 | } |
| 380 | 394 | ||
| 381 | /** @type {any} */ | 395 | /** @type {any} */ |
| @@ -390,24 +404,34 @@ router.post('/deeplx', jsonParser, async (request, response) => { | |||
| 390 | }); | 404 | }); |
| 391 | 405 | ||
| 392 | router.post('/bing', jsonParser, async (request, response) => { | 406 | router.post('/bing', jsonParser, async (request, response) => { |
| 393 | const text = request.body.text; | 407 | try { |
| 394 | let lang = request.body.lang; | 408 | const text = request.body.text; |
| 409 | let lang = request.body.lang; | ||
| 395 | 410 | ||
| 396 | if (request.body.lang === 'zh-CN') { | 411 | if (request.body.lang === 'zh-CN') { |
| 397 | lang = 'zh-Hans'; | 412 | lang = 'zh-Hans'; |
| 398 | } | 413 | } |
| 399 | 414 | ||
| 400 | if (!text || !lang) { | 415 | if (request.body.lang === 'zh-TW') { |
| 401 | return response.sendStatus(400); | 416 | lang = 'zh-Hant'; |
| 402 | } | 417 | } |
| 403 | 418 | ||
| 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); | ||
| 405 | 428 | ||
| 406 | bingTranslateApi.translate(text, null, lang).then(result => { | 429 | const result = await bingTranslate(text, null, lang); |
| 407 | console.log('Translated text: ' + result.translation); | 430 | 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); | ||
| 411 | return response.sendStatus(500); | 435 | return response.sendStatus(500); |
| 412 | }); | 436 | } |
| 413 | }); | 437 | }); |