From 039566ded5308a3c3d405d5bce0e13ae0e5df4dd Mon Sep 17 00:00:00 2001 From: Gabe Date: Wed, 1 Oct 2025 23:22:14 +0800 Subject: [PATCH] fix: deeplfree api --- src/apis/baidu.js | 19 +++++++------------ src/apis/deepl.js | 42 +++++++++++++++++------------------------- src/apis/trans.js | 16 +++++++++++++--- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/apis/baidu.js b/src/apis/baidu.js index 1d04c63..d1d735e 100644 --- a/src/apis/baidu.js +++ b/src/apis/baidu.js @@ -1,7 +1,6 @@ -import queryString from "query-string"; import { DEFAULT_USER_AGENT } from "../config"; -export const genBaidu = async ({ texts, from, to }) => { +export const genBaidu = ({ texts, from, to }) => { const data = { from, to, @@ -9,16 +8,12 @@ export const genBaidu = async ({ texts, from, to }) => { source: "txt", }; - const input = "https://fanyi.baidu.com/transapi"; - const init = { - headers: { - // Origin: "https://fanyi.baidu.com", - "content-type": "application/x-www-form-urlencoded; charset=UTF-8", - "User-Agent": DEFAULT_USER_AGENT, - }, - method: "POST", - body: queryString.stringify(data), + const url = "https://fanyi.baidu.com/transapi"; + const headers = { + // Origin: "https://fanyi.baidu.com", + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", + "User-Agent": DEFAULT_USER_AGENT, }; - return [input, init]; + return { url, data, headers }; }; diff --git a/src/apis/deepl.js b/src/apis/deepl.js index e93ced3..837355d 100644 --- a/src/apis/deepl.js +++ b/src/apis/deepl.js @@ -7,7 +7,9 @@ export const genDeeplFree = ({ texts, from, to }) => { timestamp = timestamp + (iCount - (timestamp % iCount)); id++; - let body = JSON.stringify({ + const url = "https://www2.deepl.com/jsonrpc"; + + const data = { jsonrpc: "2.0", method: "LMT_handle_texts", params: { @@ -29,30 +31,20 @@ export const genDeeplFree = ({ texts, from, to }) => { }, ], }, - }); - - body = body.replace( - 'method":"', - (id + 3) % 13 === 0 || (id + 5) % 29 === 0 ? 'method" : "' : 'method": "' - ); - - const input = "https://www2.deepl.com/jsonrpc"; - const init = { - headers: { - "Content-Type": "application/json", - Accept: "*/*", - "x-app-os-name": "iOS", - "x-app-os-version": "16.3.0", - "Accept-Language": "en-US,en;q=0.9", - "Accept-Encoding": "gzip, deflate, br", - "x-app-device": "iPhone13,2", - "User-Agent": "DeepL-iOS/2.9.1 iOS 16.3.0 (iPhone13,2)", - "x-app-build": "510265", - "x-app-version": "2.9.1", - }, - method: "POST", - body, }; - return [input, init]; + const headers = { + "Content-Type": "application/json", + Accept: "*/*", + "x-app-os-name": "iOS", + "x-app-os-version": "16.3.0", + "Accept-Language": "en-US,en;q=0.9", + "Accept-Encoding": "gzip, deflate, br", + "x-app-device": "iPhone13,2", + "User-Agent": "DeepL-iOS/2.9.1 iOS 16.3.0 (iPhone13,2)", + "x-app-build": "510265", + "x-app-version": "2.9.1", + }; + + return { url, data, headers }; }; diff --git a/src/apis/trans.js b/src/apis/trans.js index 341dc79..c2b4189 100644 --- a/src/apis/trans.js +++ b/src/apis/trans.js @@ -24,6 +24,7 @@ import { // INPUT_PLACE_TEXT, INPUT_PLACE_KEY, INPUT_PLACE_MODEL, + DEFAULT_USER_AGENT, } from "../config"; import { msAuth } from "../libs/auth"; import { genDeeplFree } from "./deepl"; @@ -227,8 +228,7 @@ const genTencent = ({ texts, from, to }) => { const url = "https://transmart.qq.com/api/imt"; const headers = { "Content-Type": "application/json", - "user-agent": - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36", + "user-agent": DEFAULT_USER_AGENT, referer: "https://transmart.qq.com/zh-CN/index", }; @@ -551,7 +551,17 @@ const genInit = ({ headers, }; if (method !== "GET" && method !== "HEAD" && data) { - Object.assign(init, { body: JSON.stringify(data) }); + let body = JSON.stringify(data); + const id = data?.params?.id; + if (id) { + body = body.replace( + 'method":"', + (id + 3) % 13 === 0 || (id + 5) % 29 === 0 + ? 'method" : "' + : 'method": "' + ); + } + Object.assign(init, { body }); } return [url, init, userMsg];