X-Git-Url: https://git.r.bdr.sh/rbdr/generador-de-insultos/blobdiff_plain/da393520c4a1b33bffdfe26974e817f2c65267d3..c0d27c055055f4aada0a96b779e0bd661347160c:/lib/Conjugator.js diff --git a/lib/Conjugator.js b/lib/Conjugator.js deleted file mode 100644 index c4e74f7..0000000 --- a/lib/Conjugator.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -const Cheerio = require('cheerio'); -const Fetch = require('node-fetch'); - -const internals = { - kBaseUrl: 'http://www.spanishdict.com/conjugate/', - kSelector: 'table .ex-tip', - kThirdPersonPresentPosition: 10, - - extractWord(node) { - - let word = ''; - - node.children.forEach(function (child) { - - if (child.type === 'text') { - word += child.data; - } else { - word += internals.extractWord(child); - } - }); - - word = word.split(',')[0]; // multiple conjugations, take 1 - - const components = word.split(' '); // some special cases have two words - // use the last, why not - - return components[components.length - 1]; - } -}; - -module.exports = { - async conjugate(verb) { - - console.debug(`Conjugating ${verb}`); - - const response = await Fetch(internals.kBaseUrl + verb); - const body = await response.text(); - - const $ = Cheerio.load(body); - const result = $(internals.kSelector)[internals.kThirdPersonPresentPosition]; - - if (!result) { - console.error('Verb not found: ', verb); - throw new Error('Not a valid verb'); - } - - const plainTextVerb = internals.extractWord(result); - console.debug(verb, plainTextVerb); - - return plainTextVerb; - } -};