Socket
Socket
Sign inDemoInstall

google-translate-api

Package Overview
Dependencies
30
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 1.0.0

.travis.yml

94

index.js

@@ -5,16 +5,14 @@ /**

var request = require("request"),
querystring = require("querystring"),
tk = require('./tk');
var querystring = require('querystring');
var got = require('got');
var tk = require('./tk');
module.exports = function translate(from, to, text, cb) {
tk(text, function (err, tk) {
if (err) return cb(err);
function translate(text, opts) {
return tk(text).then(function (tk) {
var url = 'https://translate.google.com/translate_a/single';
var data = {
client: 't',
sl: from,
tl: to,
hl: from,
sl: opts.from || 'auto',
tl: opts.to,
hl: opts.from || 'en',
dt: ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'],

@@ -27,33 +25,69 @@ ie: 'UTF-8',

kc: 7,
tk: tk,
tk: opts.tk || tk,
q: text
};
url += '?' + querystring.stringify(data);
request.get({
url: url
}, function (err, res, body) {
if (err) {
var e = new Error();
e.code = 'BAD_NETWORK';
return cb(e);
return url + '?' + querystring.stringify(data);
}).then(function (url) {
return got(url).then(function (res) {
if (opts.raw) {
return res.body;
}
if (res.statusCode != 200) {
var e = new Error();
e.code = 'BAD_REQUEST';
return cb(e);
var result = {
text: {
corrected: false,
correction: '',
value: ''
},
from: {
corrected: false,
iso: ''
}
};
if (opts.includeRaw) {
result.raw = res.body;
}
var result = '';
eval(body)[0].forEach(function (obj) {
if (obj[0] != undefined) {
result += obj[0];
var body = eval(res.body);
body[0].forEach(function (obj) {
if (obj[0] !== undefined) {
result.text.value += obj[0];
}
});
cb(null, result);
if (body[2] === body[8][0][0]) {
result.from.iso = body[2];
} else {
result.from.corrected = true;
result.from.iso = body[8][0][0];
}
if (body[7] !== undefined) {
var str = body[7][0];
str = str.replace(/<b><i>/g, '[');
str = str.replace(/<\/i><\/b>/g, ']');
result.text.corrected = true;
result.text.correction = str;
}
return result;
}).catch(function (err) {
var e;
if (err.statusCode !== undefined && err.statusCode !== 200) {
e = new Error();
e.code = 'BAD_REQUEST';
throw e;
} else {
e = new Error();
e.code = 'BAD_NETWORK';
throw e;
}
});
});
};
}
module.exports = translate;
{
"name": "google-translate-api",
"version": "0.1.1",
"version": "1.0.0",
"description": "A free and unlimited API for Google Translate",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "xo && ava"
},

@@ -34,4 +34,17 @@ "repository": {

"configstore": "^2.0.0",
"request": "^2.72.0"
"got": "^6.3.0"
},
"devDependencies": {
"ava": "^0.14.0",
"xo": "^0.15.1"
},
"xo": {
"space": 4,
"rules": {
"no-eval": 1
},
"ignores": [
"tk.js"
]
}
}

@@ -1,2 +0,2 @@

# google-translate-api
# google-translate-api [![Build Status](https://travis-ci.org/matheuss/vertaler.svg?branch=master)](https://travis-ci.org/matheuss/google-translate-api) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
A free and unlimited API for Google Translate :dollar::no_entry_sign:

@@ -13,3 +13,3 @@ /**

*/
var request = require('request'),
var got = require('got'),
Configstore = require('configstore');

@@ -90,15 +90,11 @@

function updateTKK(cb) {
var now = Math.floor(Date.now() / 3600000);
function updateTKK() {
return new Promise(function (resolve, reject) {
var now = Math.floor(Date.now() / 3600000);
if (Number(window.TKK.split('.')[0]) == now) {
cb(null);
} else {
request.get('https://translate.google.com', function (err, res, body) {
if (err) {
var e = new Error();
e.code = 'BAD_NETWORK';
return cb(e);
} else {
var code = body.match(/TKK=(.*?)\(\)\)'\);/g);
if (Number(window.TKK.split('.')[0]) == now) {
resolve();
} else {
got('https://translate.google.com').then(function (res) {
var code = res.body.match(/TKK=(.*?)\(\)\)'\);/g);

@@ -118,16 +114,20 @@ if (code) {

cb(null);
}
});
}
resolve();
}).catch(function (ignored) {
var e = new Error();
e.code = 'BAD_NETWORK';
reject(e);
});
}
});
}
module.exports = function get(text, cb) {
updateTKK(function (err) {
if (err) return cb(err);
module.exports = function get(text) {
return updateTKK().then(function () {
var tk = sM(text);
tk = tk.replace('&tk=', '');
cb(null, tk);
return tk;
}).catch(function (err) {
throw err;
});
};
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc