UNPKG

voc-cli

Version:

download and play English vocabularies' audio via command line

154 lines (123 loc) 3.96 kB
'use strict'; 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 = 'https://www.collinsdictionary.com'; // 1. search word var isWordExist = _async_(regeneratorRuntime.mark(function _callee(word) { var url, res, list; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: url = HOST + '/autocomplete/' + urlformat({ query: { dictCode: 'english', q: 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: _context.t0 = function (v) { return v.toLowerCase(); }; list = _context.sent.map(_context.t0); 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 + '/dictionary/english/' + word.replace(/ /g, '-'); _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 = {}; $('a.hwd_sound').each(function (index, element) { var audio = HOST + $(element).attr('data-src-mp3'); set[audio] = true; }); list = Object.keys(set); if (!(list.length === 0)) { _context2.next = 17; break; } err = new Error('\'' + word + '\' has no audio from collins'); 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 collins'); err.code = 'ENOENT'; throw err; case 8: case 'end': return _context3.stop(); } } }, _callee3, this); }));