From e134f031b6ba792d97fc39397faa2d684005a75c Mon Sep 17 00:00:00 2001 From: Matheus Date: Fri, 1 Jul 2016 23:47:11 -0300 Subject: [PATCH] Use matheuss/google-translate-token --- index.js | 6 +-- package.json | 2 +- test.js | 8 ---- tk.js | 131 --------------------------------------------------- 4 files changed, 4 insertions(+), 143 deletions(-) delete mode 100644 tk.js diff --git a/index.js b/index.js index 1b99b765..3503ae22 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ var querystring = require('querystring'); var got = require('got'); -var tk = require('./tk'); +var token = require('google-translate-token'); function translate(text, opts) { opts = opts || {}; - return tk(text).then(function (tk) { + return token.get(text).then(function (token) { var url = 'https://translate.google.com/translate_a/single'; var data = { client: 't', @@ -18,9 +18,9 @@ function translate(text, opts) { ssel: 0, tsel: 0, kc: 7, - tk: opts.tk || tk, q: text }; + data[token.name] = token.value; return url + '?' + querystring.stringify(data); }).then(function (url) { diff --git a/package.json b/package.json index a1e8a335..fb588bde 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "homepage": "https://github.com/matheuss/google-translate-api#readme", "dependencies": { "configstore": "^2.0.0", + "google-translate-token": "latest", "got": "^6.3.0" }, "devDependencies": { @@ -47,7 +48,6 @@ "no-eval": 1 }, "ignores": [ - "tk.js", "languages.js" ] } diff --git a/test.js b/test.js index b6943fd8..b9cc8a3d 100644 --- a/test.js +++ b/test.js @@ -56,14 +56,6 @@ test('translate some misspelled english text to dutch', async t => { } }); -test('translate some text with an invalid tk', async t => { - try { - await translate('vertaler', {tk: 0}); - } catch (err) { - t.is(err.code, 'BAD_REQUEST'); - } -}); - test.todo('try to translate some text without an internet connection'); test('translate some text and get the raw output alongside', async t => { diff --git a/tk.js b/tk.js deleted file mode 100644 index 845cd74c..00000000 --- a/tk.js +++ /dev/null @@ -1,131 +0,0 @@ -/** - * - * Last update: 2016/05/11 - * https://translate.google.com/translate/releases/twsfe_w_20160502_RC00/r/js/desktop_module_main.js - * - * The propose of this code is to generate the 'tk' parameter that is used by translate.google.com servers to validate - * that the requests are actually coming from the official apps – and not from a tool like this API. - * - * Everything between 'BEGIN' and 'END' comments was copied and pasted from the url above. - * - */ -var got = require('got'), - Configstore = require('configstore'); - -// BEGIN - -function sM(a) { - var b; - if (null !== yr) - b = yr; - else { - b = wr(String.fromCharCode(84)); - var c = wr(String.fromCharCode(75)); - b = [b(), b()]; - b[1] = c(); - b = (yr = window[b.join(c())] || "") || "" - } - var d = wr(String.fromCharCode(116)) - , c = wr(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 = xr(a, "+-a^+6"); - a = xr(a, "+-3^+b+-f"); - a ^= Number(d[1]) || 0; - 0 > a && (a = (a & 2147483647) + 2147483648); - a %= 1E6; - return c + (a.toString() + "." + (a ^ b)) -} - -var yr = null; -var wr = function(a) { - return function() { - return a - } -} - , xr = 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 t = 'a'; -var Yb = '+'; - -var qM = function (a, b) { - 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 -}; - -// END - -var config = new Configstore('google-translate-api'); - -var window = { - TKK: config.get('TKK') || '0' -}; - -function updateTKK() { - return new Promise(function (resolve, reject) { - var now = Math.floor(Date.now() / 3600000); - - if (Number(window.TKK.split('.')[0]) == now) { - resolve(); - } else { - got('https://translate.google.com').then(function (res) { - var code = res.body.match(/TKK=(.*?)\(\)\)'\);/g); - - if (code) { - eval(code[0]); - if (typeof TKK != 'undefined') { - window.TKK = TKK; - config.set('TKK', TKK); - } - } - - /** - * Note: If the regex or the eval fail, there is no need to worry. The server will accept - * relatively old seeds. - */ - - resolve(); - }).catch(function (ignored) { - var e = new Error(); - e.code = 'BAD_NETWORK'; - reject(e); - }); - } - }); -} - -module.exports = function get(text) { - return updateTKK().then(function () { - var tk = sM(text); - tk = tk.replace('&tk=', ''); - return tk; - }).catch(function (err) { - throw err; - }); -};