Created
November 28, 2018 09:42
-
-
Save yuanliwei/0cf03e77e9c305ce9ebcaba9cba2f9bb to your computer and use it in GitHub Desktop.
google translate.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var buildTkparam = function (a) { | |
| var is = function (a) { | |
| return function () { | |
| return a | |
| } | |
| }, js = function (a, b) { | |
| for (var c = 0; c < b.length - 2; c += 3) { | |
| var d = b.charAt(c + 2); | |
| d = "a" <= d ? d.charCodeAt(0) - 87 : Number(d); | |
| d = "+" == b.charAt(c + 1) ? a >>> d : a << d; | |
| a = "+" == b.charAt(c) ? a + d & 4294967295 : a ^ d | |
| } | |
| return a | |
| }; | |
| var ks = TKK | |
| var b = ks | |
| var d = is(String.fromCharCode(116)); | |
| c = is(String.fromCharCode(107)); | |
| d = [d(), d()]; | |
| d[1] = c(); | |
| c = "&" + d.join("") + "="; | |
| d = b.split("."); | |
| b = Number(d[0]) || 0; | |
| for (var e = [], f = 0, g = 0; g < a.length; g++) { | |
| var l = a.charCodeAt(g); | |
| 128 > l ? e[f++] = l : (2048 > l ? e[f++] = l >> 6 | 192 : (55296 == (l & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (l = 65536 + ((l & 1023) << 10) + (a.charCodeAt(++g) & 1023), | |
| e[f++] = l >> 18 | 240, | |
| e[f++] = l >> 12 & 63 | 128) : e[f++] = l >> 12 | 224, | |
| e[f++] = l >> 6 & 63 | 128), | |
| e[f++] = l & 63 | 128) | |
| } | |
| a = b; | |
| for (f = 0; f < e.length; f++) | |
| a += e[f], | |
| a = js(a, "+-a^+6"); | |
| a = js(a, "+-3^+b+-f"); | |
| a ^= Number(d[1]) || 0; | |
| 0 > a && (a = (a & 2147483647) + 2147483648); | |
| a %= 1E6; | |
| return c + (a.toString() + "." + (a ^ b)) | |
| }; | |
| var findTKK = async function () { | |
| // https://translate.google.cn | |
| } | |
| var TKK = '428720.3557950936' | |
| var translate = async function (tk, string) { | |
| return new Promise((resolve, reject) => { | |
| // https://translate.google.cn/translate_a/single?client=t&sl=en&tl=zh-CN&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&pc=1&otf=1&ssel=0&tsel=0&kc=1&tk=581163.939136&q=This%20is%20our%20main%20function.%20It%20loads%20the%20MNIST%20data%2C%20trains%20the%20model | |
| const https = require('https'); | |
| const url = require('url'); | |
| const urlString = `https://translate.google.cn/translate_a/single | |
| ?client=t | |
| &sl=en | |
| &tl=zh-CN | |
| &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 &pc=1 &otf=1 &ssel=0 &tsel=0 &kc=1 | |
| ${tk} | |
| &q=${encodeURIComponent(string)}`.replace(/\s/g, '') | |
| console.log(urlString); | |
| var options = url.parse(urlString) | |
| options.headers = { | |
| "accept": "*/*", | |
| "accept-encoding": "gzip, deflate, br", | |
| "accept-language": "zh-CN,zh;q=0.9,en;q=0.8", | |
| "cookie": "_ga=GA1.3.527712905.1543375885; _gid=GA1.3.456108610.1543375885; _gat=1; 1P_JAR=2018-11-28-8; NID=148=iaWtyIBIgb4BIZd2Ego8CNtbt9uArJf7U8nS2I50oBZPqPUvtkH0GadT_HGPoYaLMx9th4ckfNzpnsyvpNt_2R0u3RnpsHz7bd0XvaBZ8P7X9abnOA1vzWK8ZQWGiNnMan4IWKAHe0ruALHJIU18UiYeG8BOFxsdWily-378rw8", | |
| "referer": "https://translate.google.cn/", | |
| "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36", | |
| // "x-client-data": "CJW2yQEIprbJAQjBtskBCIqSygEIqZ3KAQioo8oBGPmlygE=" | |
| } | |
| var chunks = []; | |
| var req = https.request(options, (res) => { | |
| res.on('data', (chunk) => { | |
| chunks.push(chunk) | |
| }) | |
| res.on('end', () => { | |
| const zlib = require('zlib'); | |
| const result = zlib.gunzipSync(Buffer.concat(chunks)).toString('utf-8') | |
| resolve(result) | |
| }) | |
| }) | |
| req.on('error', (e) => { | |
| console.log('onerror', e); | |
| reject(e); | |
| }) | |
| req.end() | |
| }) | |
| } | |
| async function start() { | |
| let string = 'To view, visit change 2053. To unsubscribe, or for help writing mail filters, visit settings. ' | |
| let tk = await buildTkparam(string) | |
| console.log('tk', tk); | |
| let result = await translate(tk, string) | |
| console.log(result); | |
| } | |
| start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment