免费(po-jie) Google翻译接口java源码,国内可以访问

免费(po-jie) Google翻译接口源码 (不用*,在国内也可以访问,不过时间需要几秒钟)

源码来源
https://blog.csdn.net/hujingshuang/article/details/80178152

注:原文的代码没问题也齐全,不过小白可能会不明白 Google.js 在哪、怎么放?
针对小白我在最后面给出了工程源码 下载直接运行就可以验证使用

maven依赖

  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.12</version>
        </dependency>

Google.js

项目根目录下 新建个目录js , 然后新建一个文件 Google.js

代码中访问Google.js的路径: System.getProperty(“user.dir”) + “/js/Google.js”

免费(po-jie) Google翻译接口java源码,国内可以访问
Google.js 源码

function token(a) {
    var k = "";
    var b = 406644;
    var b1 = 3293161072;

    var jd = ".";
    var sb = "+-a^+6";
    var Zb = "+-3^+b+-f";

    for (var e = [], f = 0, g = 0; g < a.length; g++) {
        var m = a.charCodeAt(g);
        128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
            e[f++] = m >> 18 | 240,
            e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
            e[f++] = m >> 6 & 63 | 128),
            e[f++] = m & 63 | 128)
    }
    a = b;
    for (f = 0; f < e.length; f++)
        a += e[f],
            a = RL(a, sb);
    a = RL(a, Zb);
    a ^= b1 || 0;
    0 > a && (a = (a & 2147483647) + 2147483648);
    a %= 1E6;
    return a.toString() + jd + (a ^ b)
};

function RL(a, b) {
    var t = "a";
    var Yb = "+";
    for (var c = 0; c < b.length - 2; c += 3) {
        var d = b.charAt(c + 2),
            d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
            d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
        a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
    }
    return a
}

Google翻译接口

GoogleTranslate 源码

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

public class GoogleTranslate {
    public static void main(String[] args) throws Exception {
        long time = System.currentTimeMillis();
        String from = "zh-CN";
        String to = "en";
        String q = "我是谁,我在哪儿?";
        String url = "http://translate.google.cn/translate_a/single";

        String tk = token(q);
        Map<String, String> params = new HashMap<String, String>();
        params.put("client", "t");
        params.put("sl", from);
        params.put("tl", to);
        params.put("hl", "zh-CN");
        params.put("dt", "at");
        params.put("dt", "bd");
        params.put("dt", "ex");
        params.put("dt", "ld");
        params.put("dt", "md");
        params.put("dt", "qca");
        params.put("dt", "rw");
        params.put("dt", "rm");
        params.put("dt", "ss");
        params.put("dt", "t");
        params.put("ie", "UTF-8");
        params.put("oe", "UTF-8");
        params.put("source", "btn");
        params.put("ssel", "0");
        params.put("tsel", "0");
        params.put("kc", "0");
        params.put("tk", tk);
        params.put("q", q);

        CloseableHttpClient httpClient = HttpClients.createDefault();

        URIBuilder uri = new URIBuilder(url);
        for (String key : params.keySet()) {
            String value = params.get(key);
            uri.addParameter(key, value);
        }
        HttpUriRequest request = new HttpGet(uri.toString());
        CloseableHttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();

        String result = EntityUtils.toString(entity, "utf-8");

        System.out.println(result);

        EntityUtils.consume(entity);
        response.getEntity().getContent().close();
        response.close();

        System.out.println("耗时(ms): " + (System.currentTimeMillis()-time));
    }

    private static String token(String value) {
        String userDir = System.getProperty("user.dir");
        String result = "";
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
        try {
            FileReader reader = new FileReader(userDir+"/js/Google.js");
            engine.eval(reader);

            if (engine instanceof Invocable) {
                Invocable invoke = (Invocable)engine;
                result = String.valueOf(invoke.invokeFunction("token", value));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

运行结果 可以看到翻译内容: “Who am I, where am I?”,“我是谁,我在哪儿?”
免费(po-jie) Google翻译接口java源码,国内可以访问

最后,工程demo源码

当前能正常使用的时间点 2020-12-17 ,时间若太久了不知道接口会不会被河蟹,自己验证可用后再使用

百度网盘链接:https://pan.baidu.com/s/1Z6j6gmGG89UdKRqtCPQArA
提取码:sag4

上一篇:计数训练之二


下一篇:matrix(DP杂题,思维题)