translator-promise
Advanced tools
Comparing version 1.0.2 to 1.0.4
@@ -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 @@ // } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
8713
0
5
167
0
111
1
1
- Removedrequest@^2.88.0
- Removedrequest-promise@^4.2.2
- Removedurlencode@^1.1.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedbluebird@3.7.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedlodash@4.17.21(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.13.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedrequest-promise@4.2.6(transitive)
- Removedrequest-promise-core@1.1.4(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstealthy-require@1.1.1(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedurlencode@1.1.0(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)