diff options
| author | Ben Beltran <ben@nsovocal.com> | 2020-04-18 14:20:53 +0200 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2020-04-18 14:20:53 +0200 |
| commit | da393520c4a1b33bffdfe26974e817f2c65267d3 (patch) | |
| tree | 1c7b0999dbf235ee54b864167baefbd0422a3331 /lib/Conjugator.js | |
| parent | e865f21be5685ef2fbc57e36460f8d6819e0fe34 (diff) | |
Update code for 2020
Diffstat (limited to 'lib/Conjugator.js')
| -rw-r--r-- | lib/Conjugator.js | 86 |
1 files changed, 37 insertions, 49 deletions
diff --git a/lib/Conjugator.js b/lib/Conjugator.js index 78dd713..c4e74f7 100644 --- a/lib/Conjugator.js +++ b/lib/Conjugator.js @@ -1,66 +1,54 @@ 'use strict'; +const Cheerio = require('cheerio'); +const Fetch = require('node-fetch'); -var Promise = require('bluebird'); -var cheerio = require('cheerio'); -require('neon'); -var request = require('request'); +const internals = { + kBaseUrl: 'http://www.spanishdict.com/conjugate/', + kSelector: 'table .ex-tip', + kThirdPersonPresentPosition: 10, + extractWord(node) { -var Conjugator = Class({}, "Conjugator")({ - prototype : { - _baseUrl: "http://www.spanishdict.com/conjugate/", - _selector: "table .ex-tip", + let word = ''; - init : function init(config) { - config = config || {}; + node.children.forEach(function (child) { - Object.keys(config).forEach(function (property) { - this[property] = config[property]; - }, this); - }, + if (child.type === 'text') { + word += child.data; + } else { + word += internals.extractWord(child); + } + }); - conjugate : function conjugate(verb) { - return new Promise(function (resolve, reject) { - request(this._baseUrl + verb, function (err, res, body) { - var $, result, finalVerb ; - if (err) { - return reject(err); - } - $ = cheerio.load(body); - result = $(this._selector)[10]; + word = word.split(',')[0]; // multiple conjugations, take 1 - if (!result) { - console.log("Verb not found: ", verb); - return reject(new Error("Not a valid verb")); - } + const components = word.split(' '); // some special cases have two words + // use the last, why not - finalVerb = this._extractWord(result); + return components[components.length - 1]; + } +}; - console.log(verb, finalVerb); - resolve(finalVerb); - }.bind(this)); - }.bind(this)); - }, +module.exports = { + async conjugate(verb) { - _extractWord : function _extractWord(node) { - var word = "", components; + console.debug(`Conjugating ${verb}`); - node.children.forEach(function (child) { - if (child.type === "text") { - word += child.data; - } else { - word += this._extractWord(child); - } - }, this); + const response = await Fetch(internals.kBaseUrl + verb); + const body = await response.text(); - word = word.split(",")[0]; // multiple conjugations, take 1 - components = word.split(" "); // some special cases have two words - // use the last, why not + const $ = Cheerio.load(body); + const result = $(internals.kSelector)[internals.kThirdPersonPresentPosition]; - return components[components.length - 1]; + if (!result) { + console.error('Verb not found: ', verb); + throw new Error('Not a valid verb'); } - } -}); -module.exports = Conjugator; + const plainTextVerb = internals.extractWord(result); + console.debug(verb, plainTextVerb); + + return plainTextVerb; + } +}; |