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