From 3f0f9d22305d288ec2c3c449085f512a65184c42 Mon Sep 17 00:00:00 2001 From: guobao2333 Date: Sun, 24 Nov 2024 21:27:23 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=B0=83=E6=95=B4=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 1 - server.js => src/server.js | 0 translate.js => src/translate.js | 29 +++++++++++++++-------------- test.js | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) rename server.js => src/server.js (100%) rename translate.js => src/translate.js (81%) diff --git a/src/index.js b/src/index.js index 1473f75..e81e79d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ import express from 'express'; -import fetch from 'node-fetch'; import { post, get } from '../server.js'; const app = express(); diff --git a/server.js b/src/server.js similarity index 100% rename from server.js rename to src/server.js diff --git a/translate.js b/src/translate.js similarity index 81% rename from translate.js rename to src/translate.js index bde5610..1606cef 100644 --- a/translate.js +++ b/src/translate.js @@ -2,7 +2,6 @@ import axios from 'axios'; import lodash from 'lodash'; import { brotliDecompress } from 'zlib'; - const DEEPL_BASE_URL = 'https://www2.deepl.com/jsonrpc'/*, DEEPL_PRO_URL = 'https://api.deepl.com', DEEPL_FREE_URL = 'https://api-free.deepl.com'*/; @@ -28,8 +27,10 @@ function getICount(translateText) { return (translateText || '').split('i').length - 1; } -function getRandomNumber() { - return lodash.random(8300000, 8399998) * 1000; +function getRandomInt(min=8300000, max=8399998) { + min = Math.ceil(min); + max = Math.floor(max); + return (Math.floor(Math.random() * (max - min + 1)) + min) * 1000; } function getTimestamp(iCount) { @@ -49,21 +50,21 @@ async function translate( printResult = false, ) { const iCount = getICount(text); - const id = getRandomNumber(); + const id = getRandomInt(); alternativeCount = Math.max(Math.min(3, alternativeCount), 0); const postData = { - jsonrpc: '2.0', - method: 'LMT_split_text', + jsonrpc: "2.0", + method: "LMT_split_text", params: { texts: [text], commonJobParams: { - mode: 'translate', - textType: 'plaintext' + mode: "translate", + textType: "plaintext" }, lang: { - lang_user_selected: sourceLang.toUpperCase(), + lang_user_selected: "auto", lang_computed: targetLang.toUpperCase() }/*, timestamp: getTimestamp(iCount)*/ @@ -73,11 +74,11 @@ async function translate( let postDataStr = JSON.stringify(postData); - if ((id + 5) % 29 === 0 || (id + 3) % 13 === 0) { - postDataStr = postDataStr.replace('"method":"', '"method" : "'); - } else { - postDataStr = postDataStr.replace('"method":"', '"method": "'); - } + // if ((id + 5) % 29 === 0 || (id + 3) % 13 === 0) { + // postDataStr = postDataStr.replace('"method":"', '"method" : "'); + // } else { + // postDataStr = postDataStr.replace('"method":"', '"method": "'); + // } try { const response = await axios.post(DEEPL_BASE_URL, postDataStr, { diff --git a/test.js b/test.js index b1fbac7..8c735ca 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -import { translate } from './translate.js'; +import { translate } from 'src/translate.js'; (async () => { /**