Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

translator-promise

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

translator-promise - npm Package Compare versions

Comparing version 1.0.2 to 1.0.4

test.js

92

inc/translate.js

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

const request = require('request-promise').defaults({
simple: false,
resolveWithFullResponse: true,
});
const urlencode = require('urlencode');
const http = require('https')

@@ -12,4 +8,4 @@ let tkk = '429175.1243284773';

let url = 'https://translate.google.cn/';
let rsp = await get(url);
let tkkMat = rsp.body.match(/tkk:'([\d.]+)'/);
let body = await get(url);
let tkkMat = body.match(/tkk:'([\d.]+)'/);
tkk = tkkMat[1];

@@ -72,22 +68,28 @@ }

async function get(url) {
let options = {
url: url,
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36'
}
};
function get(url) {
return new Promise(function (resolve, reject){
http.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36'
}
}, function(rsp){
let body = [];
try {
let rsp = await request.get(options);
if (rsp.statusCode >= 400) {
throw 'Request API Failed. Status Code: ' + rsp.statusCode;
}
if (rsp.statusCode >= 400) {
throw 'Translate failed, please check your network.';
}
rsp.on('data', function(data) {
body = body.concat(Array.from(data));
});
return rsp;
} catch (err) {
console.error(err);
}
rsp.on('end', function() {
resolve(new Buffer(body).toString());
});
rsp.on('timeout', function() {
reject(new Error('NetWork Connect Timeout.'));
})
});
});
}

@@ -102,26 +104,8 @@

module.exports = async (word, to=null, from=null) => {
let lang = {
from: 'auto',
to: to || 'zh'
};
async function translate(word, lang) {
let url = `https://translate.google.cn/translate_a/single?client=webapp&sl=${lang.from}&tl=${lang.to}&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&pc=1&otf=1&ssel=0&tsel=0&kc=1&tk=${tk(word, tkk)}&q=${encodeURIComponent(word)}`
if (!from) {
let matChinese = word.match(/[\u4e00-\u9fa5]/g);
let matEng = word.match(/[a-zA-Z]/g);
if (!matEng || matChinese && matChinese.length > word.length / 3) {
lang = {
to: to || 'en',
from: 'auto'
};
}
} else if (to) {
lang = { to, from };
}
let url = `https://translate.google.cn/translate_a/single?client=webapp&sl=${lang.from}&tl=${lang.to}&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&pc=1&otf=1&ssel=0&tsel=0&kc=1&tk=${tk(word, tkk)}&q=${urlencode(word, 'utf-8')}`;
try {
let rsp = await get(url);
let tranWord = JSON.parse(rsp.body);
let body = await get(url);
let tranWord = JSON.parse(body);
let candidate = getCandidate(tranWord);

@@ -136,3 +120,17 @@ tranWord[0].pop();

throw 'Translate failed, please check your network.';
}
}
}
module.exports = async (word, to=null, from=null) => {
let lang = {
from: 'auto',
to: to || 'zh'
};
let result = await translate(word, lang);
if (result.text.replace(/\s/g, '') == word.replace(/\s/g, '')) {
lang.to = 'en';
result = await translate(word, lang);
}
return result;
};
{
"name": "translator-promise",
"version": "1.0.2",
"version": "1.0.4",
"description": "A node module based on Google Translate.",
"keywords": [
"translate",
"translator",
"google",
"chinese",
"promise",
"language"
],
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test.js"
},
"author": "Hancel.Lin",
"license": "ISC",
"dependencies": {
"request": "^2.88.0",
"request-promise": "^4.2.2",
"urlencode": "^1.1.0"
"dependencies": {},
"devDependencies": {
"tape": "~1.1.0"
},
"devDependencies": {},
"repository": {

@@ -22,5 +28,6 @@ "type": "git",

"bugs": {
"url": "https://github.com/imlinhanchao/translator-promise/issues"
"url": "https://github.com/imlinhanchao/translator-promise/issues",
"email": "imlinhanchao@gmail.com"
},
"homepage": "https://github.com/imlinhanchao/translator-promise#readme"
}
}

@@ -6,3 +6,3 @@ # Translate Node Module

```bash
npm install translate-node
npm install translator-promise
```

@@ -67,3 +67,3 @@

// "word": "word",
// "text": "ワード",
// "text": "語",
// "candidate": [

@@ -77,3 +77,4 @@ // "ワード",

// "一言半句",
// "口舌"
// "口舌",
// "一言"
// ]

@@ -80,0 +81,0 @@ // }

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc