Java 破解谷歌翻譯 免費 api 呼叫
阿新 • • 發佈:2018-11-19
在公司大佬的指點下, 寫了個破解谷歌翻譯的工具類,能破解谷歌翻譯, 思路如下:
1 獲取 tkk
2 根據 tkk,和 輸入內容 獲取 tk
3 根據 word,tk ,組裝 url 訪問 谷歌翻譯 api
------------------------------------------- 2018-10-28 --------------------------------
時間太久了 原始碼樓主找不到了,需要 jar 包的話 自行 下載,自己反編譯下。
連結:https://pan.baidu.com/s/1Ow-T0V_HQK_VWRteEuD5RQ
提取碼:iojc
呼叫如下:
public static void main(String[] args) {
/*GoogleApi googleApi = new GoogleApi();*/
GoogleApi googleApi = new GoogleApi("122.224.227.202", 3128);
String result = googleApi.translate("Many applications within the enterprise domain ", "", "zh");
System.out.println(result);
}
輸出:
企業領域內的許多應用程式
程式碼如下
package org.trans; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URLEncoder; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import org.apache.commons.lang3.StringUtils; import com.alibaba.fastjson.JSONArray; public class GoogleApi { private static final String PATH = "/gettk.js"; static ScriptEngine engine = null; private Browser browser = null; static{ ScriptEngineManager maneger = new ScriptEngineManager(); engine = maneger.getEngineByName("javascript"); FileInputStream fileInputStream = null; Reader scriptReader = null; try{ scriptReader = new InputStreamReader(GoogleApi.class.getResourceAsStream(PATH), "utf-8"); engine.eval(scriptReader); }catch(Exception e){ e.printStackTrace(); }finally{ if(fileInputStream != null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(scriptReader != null){ try { scriptReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } public GoogleApi(){ this.browser = new Browser(); } public GoogleApi(String ip, Integer port){ this.browser = new Browser(); this.browser.setProxy(ip, port); } public String getTKK(){ browser.setUrl("https://translate.google.cn/"); try{ String result = browser.executeGet(); if(StringUtils.isNotBlank(result)){ if(result.indexOf("TKK") > -1){ String tkk = result.split("TKK")[1]; tkk = tkk.split("\\)\\;")[0]; tkk = tkk + ");"; tkk = tkk.substring(1, tkk.length()); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); return (String) engine.eval(tkk); } } }catch(Exception e){ e.printStackTrace(); } return null; } public static String getTK(String word, String tkk){ String result = null; try{ if (engine instanceof Invocable){ Invocable invocable = (Invocable) engine; result = (String) invocable.invokeFunction("tk", new Object[]{word, tkk}); } }catch(Exception e){ e.printStackTrace(); } return result; } public String translate(String word, String from, String to){ if(StringUtils.isBlank(word)){ return null; } String _tkk = getTKK(); if(StringUtils.isBlank(_tkk)){ return null; } String _tk = getTK(word, _tkk); try{ word = URLEncoder.encode(word, "UTF-8"); }catch(Exception e){ e.printStackTrace(); } StringBuffer buffer = new StringBuffer("https://translate.google.cn/translate_a/single?client=t"); buffer.append("&sl=" + from); buffer.append("&tl=" + to); buffer.append("&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=btn&kc=0"); buffer.append("&tk=" + _tk); buffer.append("&q=" + word); browser.setUrl(buffer.toString()); try{ String result = browser.executeGet(); JSONArray array = (JSONArray) JSONArray.parse(result); JSONArray r_array = array.getJSONArray(0); StringBuffer r_buffer = new StringBuffer(); for(int i = 0; i < r_array.size(); i++){ String _r = r_array.getJSONArray(i).getString(0); if(StringUtils.isNotBlank(_r)){ r_buffer.append(_r); } } return r_buffer.toString(); }catch(Exception e){ e.printStackTrace(); return null; } } public static void main(String[] args) { /*GoogleApi googleApi = new GoogleApi();*/ GoogleApi googleApi = new GoogleApi("122.224.227.202", 3128); String result = googleApi.translate("Many applications within the enterprise domain ", "", "zh"); System.out.println(result); } }
需要的js
var b = function (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
}
var tk = function (a,TKK) {
//console.log(a,TKK);
for (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)
}
jar 包下載:
jar 包呼叫
public static void main(String[] args) {
/*GoogleApi googleApi = new GoogleApi();*/
GoogleApi googleApi = new GoogleApi("122.224.227.202", 3128);
String result = googleApi.translate("Test", "", "zh");
System.out.println(result);
}
jar 包輸出
測試
寫在最後:
但是 封ip 的速度太快了, 各位如果需要用於生產環境,必須加上ip 代理,如果實在不行。。。就換百度吧。。。雖然有字數限制。。
------------------------------------------- 2018-5-16 -------------------------------------------------------------
應讀者要求給出 Browser, 其實就是包裝了下 http, 有需要的小夥伴可以直接下載上面的 jar包,如果積分不夠 。。私聊樓主
package org.trans;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import org.trans.util.HttpClientUtil;
public class Browser
{
public Proxy proxy;
public String url;
public String getUrl()
{
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public Proxy getProxy() {
return this.proxy;
}
public void setProxy(String ip, Integer port) {
this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port.intValue()));
}
public String executeGet()
throws Exception
{
String result;
if (this.proxy != null)
result = HttpClientUtil.doGetWithProxy(this.url, this.proxy);
else {
result = HttpClientUtil.doGet(this.url);
}
return result;
}
}