All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hn.translation.impl.GoogleTranslator Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.translation.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.script.ScriptUtil;
import com.hn.translation.AbstractOnlineTranslator;
import com.hn.translation.LANG;
import com.hn.translation.annotation.TranslatorComponent;
import org.apache.http.params.HttpParams;

import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptEngineFactory;
import javax.script.SimpleBindings;
import java.util.HashMap;
import java.util.Map;

@TranslatorComponent(id="google")
public final class GoogleTranslator
  extends AbstractOnlineTranslator
{

  public GoogleTranslator()
  {
    this.langMap.put(LANG.EN, "en");
    this.langMap.put(LANG.ZH, "zh-CN");
    this.langMap.put(LANG.RU, "ru");
  }

  protected String getResponse(LANG from, LANG targ, String query)
    throws Exception
  {
    Map params = new HashMap<>();
    String tk = tk(query);

    params.put("client", "t");
    params.put("sl", (String)this.langMap.get(from));
    params.put("tl", (String)this.langMap.get(targ));
    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("srcrom", "1");
    params.put("ssel", "0");
    params.put("tsel", "0");
    params.put("kc", "11");
    params.put("tk", tk);
    params.put("q", query);
    return HttpUtil.get("http://translate.google.cn/translate_a/single",params);
  }

  protected String parseString(String jsonString)
  {
    JSONArray jsonArray = JSONUtil.parseArray(jsonString);
    JSONArray segments = jsonArray.getJSONArray(0);
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < segments.size(); i++)
    {
      String string = segments.getJSONArray(i).getStr(0);
      if (string != null) {
        result.append(string);
      }
    }
    return new String(result);
  }

  private String tk(String val)
    throws Exception
  {
    String script = "function tk(a) {var TKK = ((function() {var a = 561666268;var b = 1526272306;return 406398 + '.' + (a + b); })());\nfunction b(a, b) { for (var d = 0; d < b.length - 2; d += 3) { var c = b.charAt(d + 2), c = 'a' <= c ? c.charCodeAt(0) - 87 : Number(c), c = '+' == b.charAt(d + 1) ? a >>> c : a << c; a = '+' == b.charAt(d) ? a + c & 4294967295 : a ^ c } return a }\nfor (var e = TKK.split('.'), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {var c = a.charCodeAt(f);128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)}a = h;for (d = 0; d < g.length; d++) a += g[d], a = b(a, '+-a^+6');a = b(a, '+-3^+b+-f');a ^= Number(e[1]) || 0;0 > a && (a = (a & 2147483647) + 2147483648);a %= 1E6;return a.toString() + '.' + (a ^ h)\n};tk(result)";
//    build.loadJs(script);
      SimpleBindings simpleBindings = new SimpleBindings();
      simpleBindings.put("result",val);
      Object eval =  ScriptUtil.eval(script,simpleBindings);
    return eval.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy