From 32cb90c49792c3a65c6893e2c3487b1a3a01c1e0 Mon Sep 17 00:00:00 2001 From: adizbek Date: Mon, 22 Jul 2019 14:05:31 +0500 Subject: [PATCH 1/4] Removed proxy support, as they make more complexity. Fixed token generator --- package.json | 3 +-- src/index.js | 21 +++++--------------- src/tokenGenerator.js | 45 ++++++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index eb0c976..5a79e25 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "url": "https://github.com/k3rn31p4nic/google-translate-api/issues" }, "dependencies": { - "got": "^9.2.2", - "tunnel": "0.0.6" + "got": "^9.2.2" }, "devDependencies": { "eslint": "^5.6.1" diff --git a/src/index.js b/src/index.js index 3d8db0d..19faf2d 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ const languages = require('./languages'); const tokenGenerator = require('./tokenGenerator'); const querystring = require('querystring'); const got = require('got'); -const tunnel = require('tunnel'); + /** * @function translate @@ -17,7 +17,7 @@ async function translate(text, options) { // Check if a lanugage is in supported; if not, throw an error object. let error; - [ options.from, options.to ].forEach((lang) => { + [options.from, options.to].forEach((lang) => { if (lang && !languages.isSupported(lang)) { error = new Error(); error.code = 400; @@ -47,7 +47,7 @@ async function translate(text, options) { sl: options.from, tl: options.to, hl: options.to, - dt: [ 'at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't' ], + dt: ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'], ie: 'UTF-8', oe: 'UTF-8', otf: 1, @@ -76,19 +76,9 @@ async function translate(text, options) { ]; } else { - requestOptions = [ url ]; + requestOptions = [url]; } - let proxies = []; - if (Array.isArray(options.proxies)) { - proxies = options.proxies; - - requestOptions[1] = { - agent: tunnel.httpOverHttp({ - proxy: proxies[Math.floor(Math.random() * proxies.length)] - }) - }; - } // Request translation from Google Translate. let response = await got(...requestOptions); @@ -147,8 +137,7 @@ async function translate(text, options) { } return result; - } - catch (e) { + } catch (e) { if (e.name === 'HTTPError') { let error = new Error(); error.name = e.name; diff --git a/src/tokenGenerator.js b/src/tokenGenerator.js index 5f11f1f..dc6e1bf 100644 --- a/src/tokenGenerator.js +++ b/src/tokenGenerator.js @@ -11,17 +11,19 @@ const got = require('got'); // BEGIN function zr(a) { let b; - if (null !== yr) b = yr; + if (null !== yr) { + b = yr; + } else { b = wr(String.fromCharCode(84)); let c = wr(String.fromCharCode(75)); - b = [ b(), b() ]; + b = [b(), b()]; b[1] = c(); b = (yr = window[b.join(c())] || '') || ''; } let d = wr(String.fromCharCode(116)); let c = wr(String.fromCharCode(107)); - d = [ d(), d() ]; + d = [d(), d()]; d[1] = c(); c = '&' + d.join('') + '='; d = b.split('.'); @@ -41,12 +43,12 @@ function zr(a) { } let yr = null; -let wr = function(a) { - return function() { +let wr = function (a) { + return function () { return a; }; }; -let xr = function(a, b) { +let xr = function (a, b) { for (let c = 0; c < b.length - 2; c += 3) { let d = b.charAt(c + 2); d = d >= 'a' ? d.charCodeAt(0) - 87 : Number(d); @@ -76,22 +78,29 @@ function updateTKK() { else { let res = await got('https://translate.google.com'); - const code = res.body.match(/TKK=(.*?)\(\)\)'\);/g); + const code = res.body.match(/tkk:'\d+.\d+'/g); + + if (code.length > 0) { + const xt = code[0].split(':')[1].replace('\'', '').replace('\'', ''); - if (code) { - eval(code[0]); - /* eslint-disable no-undef */ - if (typeof TKK !== 'undefined') { - window.TKK = TKK; - config.set('TKK', TKK); - } - /* eslint-enable no-undef */ + window.TKK = xt; + config.set('TKK', xt); } + // if (code) { + // eval(code[0]); + // /* eslint-disable no-undef */ + // if (typeof TKK !== 'undefined') { + // window.TKK = TKK; + // config.set('TKK', TKK); + // } + // /* eslint-enable no-undef */ + // } + /** - * Note: If the regex or the eval fail, there is no need to worry. The - * server will accept relatively old seeds. - */ + * Note: If the regex or the eval fail, there is no need to worry. The + * server will accept relatively old seeds. + */ resolve(); } From ade0649827b18b501849e360cd13a221796e31c7 Mon Sep 17 00:00:00 2001 From: adizbek Date: Mon, 22 Jul 2019 17:14:02 +0500 Subject: [PATCH 2/4] Reformat --- src/index.js | 9 +++++---- src/tokenGenerator.js | 20 ++++---------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index 19faf2d..c350f13 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ async function translate(text, options) { // Check if a lanugage is in supported; if not, throw an error object. let error; - [options.from, options.to].forEach((lang) => { + [ options.from, options.to ].forEach((lang) => { if (lang && !languages.isSupported(lang)) { error = new Error(); error.code = 400; @@ -47,7 +47,7 @@ async function translate(text, options) { sl: options.from, tl: options.to, hl: options.to, - dt: ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'], + dt: [ 'at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't' ], ie: 'UTF-8', oe: 'UTF-8', otf: 1, @@ -76,7 +76,7 @@ async function translate(text, options) { ]; } else { - requestOptions = [url]; + requestOptions = [ url ]; } // Request translation from Google Translate. @@ -137,7 +137,8 @@ async function translate(text, options) { } return result; - } catch (e) { + } + catch (e) { if (e.name === 'HTTPError') { let error = new Error(); error.name = e.name; diff --git a/src/tokenGenerator.js b/src/tokenGenerator.js index dc6e1bf..49f97ac 100644 --- a/src/tokenGenerator.js +++ b/src/tokenGenerator.js @@ -8,6 +8,7 @@ const got = require('got'); /* eslint-disable */ + // BEGIN function zr(a) { let b; @@ -79,29 +80,16 @@ function updateTKK() { let res = await got('https://translate.google.com'); const code = res.body.match(/tkk:'\d+.\d+'/g); + // code will extract something like tkk:'1232135.131231321312', we need only value if (code.length > 0) { - const xt = code[0].split(':')[1].replace('\'', '').replace('\'', ''); + // extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312 + const xt = code[0].split(':')[1].replace(/'/g, ''); window.TKK = xt; config.set('TKK', xt); } - // if (code) { - // eval(code[0]); - // /* eslint-disable no-undef */ - // if (typeof TKK !== 'undefined') { - // window.TKK = TKK; - // config.set('TKK', TKK); - // } - // /* eslint-enable no-undef */ - // } - - /** - * Note: If the regex or the eval fail, there is no need to worry. The - * server will accept relatively old seeds. - */ - resolve(); } } From 1a1f7cfbb29492da0bee72e8b8de7283722c659f Mon Sep 17 00:00:00 2001 From: adizbek Date: Tue, 23 Jul 2019 11:11:25 +0500 Subject: [PATCH 3/4] Autorefotmated code reverted back --- src/tokenGenerator.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/tokenGenerator.js b/src/tokenGenerator.js index 49f97ac..16c41dd 100644 --- a/src/tokenGenerator.js +++ b/src/tokenGenerator.js @@ -8,23 +8,20 @@ const got = require('got'); /* eslint-disable */ - // BEGIN function zr(a) { let b; - if (null !== yr) { - b = yr; - } + if (null !== yr) b = yr; else { b = wr(String.fromCharCode(84)); let c = wr(String.fromCharCode(75)); - b = [b(), b()]; + b = [ b(), b() ]; b[1] = c(); b = (yr = window[b.join(c())] || '') || ''; } let d = wr(String.fromCharCode(116)); let c = wr(String.fromCharCode(107)); - d = [d(), d()]; + d = [ d(), d() ]; d[1] = c(); c = '&' + d.join('') + '='; d = b.split('.'); @@ -44,12 +41,12 @@ function zr(a) { } let yr = null; -let wr = function (a) { - return function () { +let wr = function(a) { + return function() { return a; }; }; -let xr = function (a, b) { +let xr = function(a, b) { for (let c = 0; c < b.length - 2; c += 3) { let d = b.charAt(c + 2); d = d >= 'a' ? d.charCodeAt(0) - 87 : Number(d); From f1ef8e6ef84810841aaf7c1c23ec6232697200ce Mon Sep 17 00:00:00 2001 From: adizbek Date: Tue, 23 Jul 2019 11:42:11 +0500 Subject: [PATCH 4/4] Fixed bugs. Long text wasn't translating. Fixed Url stays the same while switching to POST, actually should remove q parameter from url. Fixed --- src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index c350f13..f706215 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,6 @@ const tokenGenerator = require('./tokenGenerator'); const querystring = require('querystring'); const got = require('got'); - /** * @function translate * @param {String} text The text to be translated. @@ -41,7 +40,7 @@ async function translate(text, options) { let token = await tokenGenerator.generate(text); // URL & query string required by Google Translate. - let url = 'https://translate.google.com/translate_a/single'; + let baseUrl = 'https://translate.google.com/translate_a/single'; let data = { client: 'gtx', sl: options.from, @@ -59,19 +58,20 @@ async function translate(text, options) { }; // Append query string to the request URL. - url = `${url}?${querystring.stringify(data)}`; + let url = `${baseUrl}?${querystring.stringify(data)}`; let requestOptions; // If request URL is greater than 2048 characters, use POST method. if (url.length > 2048) { delete data.q; requestOptions = [ - `${url}?${querystring.stringify(data)}`, + `${baseUrl}?${querystring.stringify(data)}`, { method: 'POST', - body: JSON.stringify({ + form: true, + body: { q: text - }) + } } ]; }