voc-cli
Version:
download and play English vocabularies' audio via command line
159 lines (127 loc) • 4.08 kB
JavaScript
;
var _async_ = require('co').wrap;
var fetch = require('node-fetch');
var cheerio = require('cheerio');
var normalize = require('../lib/normalize');
var urlformat = require('url').format;
var HOST = 'http://www.thefreedictionary.com';
// 1. search word
var isWordExist = _async_(regeneratorRuntime.mark(function _callee(word) {
var url, res, data, list;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
url = HOST + '/_/search/suggest.ashx' + urlformat({
query: {
query: word
}
});
_context.next = 3;
return fetch(url, { timeout: 10 * 1000 });
case 3:
res = _context.sent;
if (!(res.status !== 200)) {
_context.next = 6;
break;
}
throw new Error('request to ' + url + ' failed, status code = ' + res.status + ' (' + res.statusText + ')');
case 6:
_context.next = 8;
return res.json();
case 8:
data = _context.sent;
list = data[1].map(function (v) {
return v.toLowerCase();
});
// word is exist or not
return _context.abrupt('return', list.indexOf(word) >= 0);
case 11:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
// 2. get audio list
var getAudioList = _async_(regeneratorRuntime.mark(function _callee2(word) {
var url, res, html, $, set, list, err;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
url = HOST + '/' + word;
_context2.next = 3;
return fetch(url, { timeout: 10 * 1000 });
case 3:
res = _context2.sent;
if (!(res.status !== 200)) {
_context2.next = 6;
break;
}
throw new Error('request to ' + url + ' failed, status code = ' + res.status + ' (' + res.statusText + ')');
case 6:
_context2.next = 8;
return res.text();
case 8:
html = _context2.sent;
$ = cheerio.load(html);
set = {};
$('.snd2').each(function (index, element) {
var reg = /^en\/US\//i;
var snd = $(element).attr('data-snd');
if (reg.test(snd)) {
var audio = 'http://img2.tfd.com/pron/mp3/' + snd + '.mp3';
set[audio] = true;
}
});
list = Object.keys(set);
if (!(list.length === 0)) {
_context2.next = 17;
break;
}
err = new Error('\'' + word + '\' has no audio from freedictionary');
err.code = 'ENOENT';
throw err;
case 17:
// show all the audio url
if (list.length > 1) {
list.forEach(function (audio, i) {
console.log(i + 1 + '. ' + audio);
});
}
// return the first audio from list
return _context2.abrupt('return', list[0]);
case 19:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
module.exports = _async_(regeneratorRuntime.mark(function _callee3(word) {
var err;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
word = normalize(word);
_context3.next = 3;
return isWordExist(word);
case 3:
if (!_context3.sent) {
_context3.next = 5;
break;
}
return _context3.abrupt('return', getAudioList(word));
case 5:
err = new Error('\'' + word + '\' is not found from freedictionary');
err.code = 'ENOENT';
throw err;
case 8:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));