fix: 更新百度翻译接口;一些小修改

This commit is contained in:
2026-01-23 22:50:42 +08:00
parent 2b8f30b72d
commit 36bd572e38
6 changed files with 23 additions and 11 deletions

View File

@ -111,8 +111,15 @@ public class HttpUtil {
}
public String postForm(String uri, JsonNode header, JsonNode param) throws IOException, InterruptedException {
return postForm(uri, header, param, null);
}
public String postForm(String uri, JsonNode header, JsonNode param, JsonNode body) throws IOException, InterruptedException {
HttpRequest.Builder request = getRequest(uri + createFormParams(param), header)
.POST(HttpRequest.BodyPublishers.noBody());
.POST(body == null ?
HttpRequest.BodyPublishers.noBody() :
HttpRequest.BodyPublishers.ofString(JsonUtil.toJsonString(body))
);
HttpResponse<String> response = httpClient.send(request.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
return response.body();

View File

@ -6,8 +6,8 @@ import cn.octopusyan.dmt.common.util.JsonUtil;
import cn.octopusyan.dmt.model.UpgradeConfig;
import cn.octopusyan.dmt.task.base.BaseTask;
import cn.octopusyan.dmt.task.listener.DefaultTaskListener;
import org.apache.commons.lang3.Strings;
import tools.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
/**
* 检查更新任务
@ -31,7 +31,7 @@ public class UpgradeTask extends BaseTask<UpgradeTask.UpgradeListener> {
String newVersion = response.get("tag_name").asString();
if (listener != null)
listener.onChecked(!StringUtils.equals(upgradeConfig.getVersion(), newVersion), newVersion);
listener.onChecked(!Strings.CS.equals(upgradeConfig.getVersion(), newVersion), newVersion);
}
/**

View File

@ -23,6 +23,7 @@ public class FreeBaiduTranslateProcessor extends AbstractTranslateProcessor {
static {
header.put("Origin", "https://fanyi.baidu.com");
header.put("acs-token", "");
header.put("Referer", "https://fanyi.baidu.com");
header.put("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
header.put("Accept","*/*");
@ -70,8 +71,6 @@ public class FreeBaiduTranslateProcessor extends AbstractTranslateProcessor {
private void checkCookie() throws IOException, InterruptedException {
// 短时大量请求会被ban需要添加验证cookie
if (header.containsKey("Cookie")) return;
List<HttpCookie> cookieList = CookieManager.getStore().get(URI.create("https://baidu.com"));
boolean noneMatch = cookieList.stream()
.filter(cookie -> "ab_sr".equals(cookie.getName()) || "BAIDUID".equals(cookie.getName()))

View File

@ -34,18 +34,24 @@ public class FreeGoogleTranslateProcessor extends AbstractTranslateProcessor {
@Override
public String customTranslate(String source) throws IOException, InterruptedException {
Map<String, Object> header = new HashMap<>();
header.put("User-Agent", "Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0");
header.put("Referer", "https://translate.google.com/");
header.put("Host", "translate.googleapis.com");
Map<String, Object> form = new HashMap<>();
form.put("client", "gtx");
form.put("dt", "t");
form.put("sl", "auto");
form.put("tl", "zh-CN");
form.put("q", source);
Map<String, Object> header = new HashMap<>();
Map<String, Object> body = new HashMap<>();
body.put("q", source);
StringBuilder retStr = new StringBuilder();
// TODO 短时大量请求会被ban需要浏览器验证添加cookie
String resp = httpUtil.get(url(), JsonUtil.parseJsonObject(header), JsonUtil.parseJsonObject(form));
String resp = httpUtil.postForm(url(), JsonUtil.parseJsonObject(header), JsonUtil.parseJsonObject(form), JsonUtil.parseJsonObject(body));
JsonNode json = JsonUtil.parseJsonObject(resp);
for (JsonNode o : json.get(0)) {

View File

@ -37,8 +37,8 @@ public class PBOUtil {
private static final ProcessesUtil processesUtil = ProcessesUtil.init(Constants.BIN_DIR_PATH);
private static final String UNPACK_COMMAND = STR."\"\{Constants.PBOC_FILE}\" unpack -o \"\{Constants.TMP_DIR_PATH}\" {}";
private static final String PACK_COMMAND = STR."\"\{Constants.PBOC_FILE}\" pack -o {} {}";
private static final String UNPACK_COMMAND = STR."\"\{Constants.PBOC_FILE}\" unpack {} -o \"\{Constants.TMP_DIR_PATH}\"";
private static final String PACK_COMMAND = STR."\"\{Constants.PBOC_FILE}\" pack {} -o {}";
private static final String CFG_COMMAND = STR."\"\{Constants.CFG_CONVERT_FILE}\" {} -dst {} {}";
private static final String FILE_NAME_STRING_TABLE = "stringtable.csv";
@ -122,7 +122,7 @@ public class PBOUtil {
FileUtils.deleteQuietly(packFile);
}
String command = ProcessesUtil.format(PACK_COMMAND, Constants.TMP_DIR_PATH, unpackPath);
String command = ProcessesUtil.format(PACK_COMMAND, unpackPath, Constants.TMP_DIR_PATH);
consoleLog.debug(STR."pack command ==> [\{command}]");
boolean exec = processesUtil.exec(command);