Comparing version 1.6.1 to 1.6.2
@@ -5,2 +5,4 @@ 'use strict'; | ||
require('babel-polyfill'); | ||
var fs = require('fs'); | ||
@@ -7,0 +9,0 @@ var util = require('util'); |
'use strict'; | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } | ||
var util = require('util'); | ||
var UrlFormat = require('url').format; | ||
var fetch = require('node-fetch'); | ||
var cheerio = require('cheerio'); | ||
function token(str, key) { | ||
function RL(num, str) { | ||
for (var i = 0; i < str.length - 2; i += 3) { | ||
var d = str.charAt(i + 2); | ||
d = d >= 'a' ? d.charCodeAt(0) - 87 : Number(d); | ||
d = str.charAt(i + 1) === '+' ? num >>> d : num << d; | ||
num = str.charAt(i) === '+' ? num + d & 4294967295 : num ^ d; | ||
} | ||
return num; | ||
} | ||
var num = key; | ||
for (var i = 0; i < str.length; i++) { | ||
num = RL(num + str.charCodeAt(i), '+-a^+6'); | ||
} | ||
num = RL(num, '+-3^+b+-f'); | ||
0 > num && (num = (num & 2147483647) + 2147483648); | ||
num %= 1E6; | ||
return num.toString() + '.' + (num ^ key); | ||
} | ||
module.exports = function () { | ||
var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(word) { | ||
var HOST, res, msg, $, scripts, TKK, key, code, querystring; | ||
var key = function () { | ||
var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { | ||
var res, TKK, html; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -39,16 +10,3 @@ while (1) { | ||
case 0: | ||
if (!(typeof word !== 'string' || word.length === 0)) { | ||
_context.next = 2; | ||
break; | ||
} | ||
throw new TypeError('word should be a string'); | ||
case 2: | ||
// replace '_' to ' ', and convert to lower case | ||
word = word.replace(/_/g, ' ').toLowerCase(); | ||
HOST = 'https://translate.google.com'; | ||
_context.next = 6; | ||
_context.next = 2; | ||
return fetch(HOST, { | ||
@@ -58,61 +16,151 @@ timeout: 10 * 1000 | ||
case 6: | ||
case 2: | ||
res = _context.sent; | ||
if (!(res.status !== 200)) { | ||
_context.next = 10; | ||
_context.next = 5; | ||
break; | ||
} | ||
msg = util.format('request to %s failed, status code = %d (%s)', HOST, res.status, res.statusText); | ||
throw new Error(msg); | ||
throw new Error('request to ' + HOST + ' failed, status code = ' + res.status + ' (' + res.statusText + ')'); | ||
case 10: | ||
_context.t0 = cheerio; | ||
_context.next = 13; | ||
case 5: | ||
TKK = null; | ||
_context.next = 8; | ||
return res.text(); | ||
case 8: | ||
html = _context.sent; | ||
_context.prev = 9; | ||
eval(html.match(/TKK=eval\(\'\(.*\)\'\);/g)[0]); // TKK = '405291.1334555331' | ||
if (!(TKK === null)) { | ||
_context.next = 13; | ||
break; | ||
} | ||
throw null; | ||
case 13: | ||
_context.t1 = _context.sent; | ||
$ = _context.t0.load.call(_context.t0, _context.t1); | ||
scripts = $('#gt-c script').text(); | ||
_context.next = 18; | ||
break; | ||
// get key | ||
case 15: | ||
_context.prev = 15; | ||
_context.t0 = _context['catch'](9); | ||
throw new Error('get key failed from google'); | ||
key = NaN; | ||
code = scripts.match(/TKK=eval\(\'\(.*\)\'\);/gi)[0]; | ||
case 18: | ||
return _context.abrupt('return', TKK); | ||
eval(code); | ||
key = parseFloat(TKK); | ||
case 19: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this, [[9, 15]]); | ||
})); | ||
if (!(isNaN(key) === true)) { | ||
_context.next = 22; | ||
return function key() { | ||
return ref.apply(this, arguments); | ||
}; | ||
}(); | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } | ||
var UrlFormat = require('url').format; | ||
var fetch = require('node-fetch'); | ||
var HOST = 'https://translate.google.com'; | ||
function XL(a, b) { | ||
for (var c = 0; c < b.length - 2; c += 3) { | ||
var d = b.charAt(c + 2); | ||
d = d >= 'a' ? d.charCodeAt(0) - 87 : Number(d); | ||
d = b.charAt(c + 1) == '+' ? a >>> d : a << d; | ||
a = b.charAt(c) == '+' ? a + d & 4294967295 : a ^ d; | ||
} | ||
return a; | ||
} | ||
/** | ||
* Generate API Token | ||
* | ||
* @param {String} text | ||
* @param {String} key | ||
* @return {String} token | ||
*/ | ||
function token(text, key) { | ||
var a = text, | ||
b = key, | ||
d = b.split('.'); | ||
b = Number(d[0]) || 0; | ||
for (var e = [], f = 0, g = 0; g < a.length; g++) { | ||
var m = a.charCodeAt(g); | ||
128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = m >> 18 | 240, e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, e[f++] = m >> 6 & 63 | 128), e[f++] = m & 63 | 128); | ||
} | ||
a = b; | ||
for (f = 0; f < e.length; f++) { | ||
a += e[f]; | ||
a = XL(a, '+-a^+6'); | ||
} | ||
a = XL(a, '+-3^+b+-f'); | ||
a ^= Number(d[1]) || 0; | ||
0 > a && (a = (a & 2147483647) + 2147483648); | ||
a = a % 1E6; | ||
return a.toString() + '.' + (a ^ b); | ||
} | ||
module.exports = function () { | ||
var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(word) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (!(typeof word !== 'string' || word.length === 0)) { | ||
_context2.next = 2; | ||
break; | ||
} | ||
throw new Error('key is not found'); | ||
throw new TypeError('word should be a string'); | ||
case 22: | ||
querystring = UrlFormat({ | ||
query: { | ||
ie: 'UTF-8', | ||
q: word, // encodeURIComponent | ||
tl: 'en', | ||
total: 1, | ||
idx: 0, | ||
textlen: word.length, | ||
tk: token(word, key), | ||
client: 't', | ||
prev: 'input', | ||
ttsspeed: 1 // slow = 0.24 | ||
} | ||
}); | ||
return _context.abrupt('return', HOST + '/translate_tts' + querystring); | ||
case 2: | ||
case 24: | ||
// replace '_' to ' ', and convert to lower case | ||
word = word.replace(/_/g, ' ').toLowerCase(); | ||
_context2.t0 = HOST + '/translate_tts'; | ||
_context2.t1 = word; | ||
_context2.t2 = word.length; | ||
_context2.t3 = word; | ||
_context2.next = 9; | ||
return key(); | ||
case 9: | ||
_context2.t4 = _context2.sent; | ||
_context2.t5 = token(_context2.t3, _context2.t4); | ||
_context2.t6 = { | ||
ie: 'UTF-8', | ||
q: _context2.t1, | ||
tl: 'en', | ||
total: 1, | ||
idx: 0, | ||
textlen: _context2.t2, | ||
tk: _context2.t5, | ||
client: 't', | ||
prev: 'input', | ||
ttsspeed: 1 | ||
}; | ||
_context2.t7 = { | ||
query: _context2.t6 | ||
}; | ||
_context2.t8 = UrlFormat(_context2.t7); | ||
return _context2.abrupt('return', _context2.t0 + _context2.t8); | ||
case 15: | ||
case 'end': | ||
return _context.stop(); | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
}, _callee2, this); | ||
})); | ||
@@ -119,0 +167,0 @@ |
{ | ||
"name": "voc-cli", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "download and play English vocabularies' audio via command line", | ||
@@ -5,0 +5,0 @@ "main": "bin/getAudio.js", |
# voc | ||
[![npm version](https://badge.fury.io/js/voc-cli.svg)](https://badge.fury.io/js/voc-cli) | ||
[![NPM](https://nodei.co/npm/voc-cli.png)](https://nodei.co/npm/voc-cli/) | ||
[![Travis-ci Status](https://travis-ci.org/zlargon/voc.svg)](https://travis-ci.org/zlargon/voc) | ||
[![Appveyor Status](https://ci.appveyor.com/api/projects/status/p6b7umww30aaf6pg/branch/master?svg=true)](https://ci.appveyor.com/project/zlargon/voc/branch/master) | ||
[![Coverage Status](https://coveralls.io/repos/github/zlargon/voc/badge.svg?branch=master)](https://coveralls.io/github/zlargon/voc?branch=master) | ||
[![Dependency Status](https://david-dm.org/zlargon/voc.svg)](https://david-dm.org/zlargon/voc) | ||
[![devDependency Status](https://david-dm.org/zlargon/voc/dev-status.svg)](https://david-dm.org/zlargon/voc#info=devDependencies) | ||
Download and play English vocabulary's audio via command line. | ||
[![Travis Status](https://travis-ci.org/zlargon/voc.svg)](https://travis-ci.org/zlargon/voc) | ||
[![Appveyor Status](https://ci.appveyor.com/api/projects/status/p6b7umww30aaf6pg?svg=true)](https://ci.appveyor.com/project/zlargon/voc) | ||
[![Coverage Status](https://coveralls.io/repos/github/zlargon/voc/badge.svg)](https://coveralls.io/github/zlargon/voc) | ||
The audio will be downloaded to directory `~/vocabulary` by default, and played by audio player command line. | ||
> Download and play English vocabulary's audio via command line. | ||
> The audio will be downloaded to directory `~/vocabulary` by default, and played by audio player command line. | ||
Dictionary: | ||
- [Webster](http://www.merriam-webster.com/) | ||
@@ -22,2 +23,3 @@ - [Collins](http://www.collinsdictionary.com/) | ||
Text-To-Speech: | ||
- [Google](https://translate.google.com/) | ||
@@ -24,0 +26,0 @@ - [iSpeech](http://www.ispeech.org/) |
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
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
43888
1060
98