JAVA常用组件 – 有道翻译、谷歌翻译、必应翻译API破译

common-translate

??有道翻译、谷歌翻译、必应翻译API破译(仅用于研究学习)。

以有道翻译为例(谷歌、必应类似)

  1. 打开有道翻译
    在这里插入图片描述
    ??浏览器F12查看到,请求URL:
1
    http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule

??关键请求参数sign,猜测是md5签名。

  1. 查看页面源码,找到fanyi.min.js链接:
1
    http://shared.ydstatic.com/fanyi/newweb/v1.0.27/scripts/newweb/fanyi.min.js

??这里的版本号v1.0.27会变。

  1. 查看js源码,将其格式化。
    在这里插入图片描述
    ?? 签名的秘钥找到了,剩下就是按规则发起请求了。

关键处理代码(com.xiaojianhx.common.translate.YoudaoTranslater)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    public static String translate(String text, String from, String to, String charset, Proxy proxy) throws Throwable {

        var helper = HttpClientHelper.post(YoudaoTranslateConstants.API_URL);

        if (!CheckUtils.isNull(proxy)) {
            helper.proxy(proxy);
        }

        YoudaoTranslateRequestHead.API_HEAD_MAP.forEach(helper::addHeader);

        var salt = StringUtils.toString(System.currentTimeMillis());
        var sign = DigestUtils.md5Hex(YoudaoTranslateManager.getInstance().prefix() + text + salt + YoudaoTranslateManager.getInstance().salt());

        text = URLEncoder.encode(text, charset);

        helper.addBody("i", text);
        helper.addBody("from", from);
        helper.addBody("to", to);
        helper.addBody("smartresult", "dict");
        helper.addBody("client", "fanyideskweb");
        helper.addBody("salt", salt);
        helper.addBody("sign", sign);
        helper.addBody("ts", salt);
        helper.addBody("doctype", "json");
        helper.addBody("version", "2.1");
        helper.addBody("keyfrom", "fanyi.web");
        helper.addBody("action", "FY_BY_CLICKBUTTION");

        String body = null;
        try {
            body = helper.executeSync().bodyAsString();
        } catch (Throwable e) {
            throw e;
        }

        logger.debug(body);

        var data = JsonUtils.readValue(body, YoudaoTranslateData.class);

        var code = data.getErrorCode();

        if (YoudaoTranslateCode.SUCCESS.code == code) {
            return data.getTranslateResult().get(0).get(0).getTgt();
        }

        throw new Throwable(YoudaoTranslateCode.search(code));
    }

demo

1
2
3
4
5
6
7
8
    var text = YoudaoTranslater.translate("你好", YoudaoTranslateLanguageCode.AUTO, YoudaoTranslateLanguageCode.EN, Charset.UTF_8);
    Assert.assertEquals("hello", text);

    text = YoudaoTranslater.translate("你好", YoudaoTranslateLanguageCode.ZN, YoudaoTranslateLanguageCode.EN, Charset.UTF_8);
    Assert.assertEquals("hello", text);

    text = YoudaoTranslater.translate("你好", YoudaoTranslateLanguageCode.ZN, YoudaoTranslateLanguageCode.JA, Charset.UTF_8);
    Assert.assertEquals("こんにちは", text);

工程链接

工程介绍:https://blog.csdn.net/xiaojianhx/article/details/106275104
全局依赖:https://blog.csdn.net/xiaojianhx/article/details/106450065
全局常量:https://blog.csdn.net/xiaojianhx/article/details/106450382
工具校验:https://blog.csdn.net/xiaojianhx/article/details/106450419
http client:https://blog.csdn.net/xiaojianhx/article/details/106444692
json处理:https://blog.csdn.net/xiaojianhx/article/details/106463604
任务开关:https://blog.csdn.net/xiaojianhx/article/details/106509280
邮件服务:https://blog.csdn.net/xiaojianhx/article/details/106559657
redis:https://blog.csdn.net/xiaojianhx/article/details/106560464
翻译API:https://blog.csdn.net/xiaojianhx/article/details/106740934