Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX 403 Error #10

Merged
merged 4 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 6 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const languages = require('./languages');
const tokenGenerator = require('./tokenGenerator');
const querystring = require('querystring');
const got = require('got');
const tunnel = require('tunnel');

/**
* @function translate
Expand Down Expand Up @@ -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,
Expand All @@ -59,36 +58,27 @@ 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
})
}
}
];
}
else {
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);

Expand Down
22 changes: 8 additions & 14 deletions src/tokenGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,16 @@ 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);
iamtraction marked this conversation as resolved.
Show resolved Hide resolved
// code will extract something like tkk:'1232135.131231321312', we need only value

if (code) {
eval(code[0]);
/* eslint-disable no-undef */
if (typeof TKK !== 'undefined') {
window.TKK = TKK;
config.set('TKK', TKK);
}
/* eslint-enable no-undef */
}
if (code.length > 0) {
// extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312
const xt = code[0].split(':')[1].replace(/'/g, '');

/**
* Note: If the regex or the eval fail, there is no need to worry. The
* server will accept relatively old seeds.
*/
window.TKK = xt;
config.set('TKK', xt);
}

resolve();
}
Expand Down