diff options
| author | Ben Beltran <ben@nsovocal.com> | 2015-09-05 19:51:29 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2015-09-05 19:51:29 -0500 |
| commit | 1a652aac0ac1efc9100dd25e7da1105a4d515dc4 (patch) | |
| tree | e1b4f113c08d498c7488d8427e333abeb99d32e1 /lib/Conjugator.js | |
| parent | 71164942dfbc59af88765fce7339fc61c77bad7b (diff) | |
Super dumb mega commit
Diffstat (limited to 'lib/Conjugator.js')
| -rw-r--r-- | lib/Conjugator.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/Conjugator.js b/lib/Conjugator.js new file mode 100644 index 0000000..5afdf15 --- /dev/null +++ b/lib/Conjugator.js @@ -0,0 +1,66 @@ +'use strict'; + + +var Promise = require('bluebird'); +var cheerio = require('cheerio'); +require('neon'); +var request = require('request'); + + +var Conjugator = Class({}, "Conjugator")({ + prototype : { + _baseUrl: "http://www.spanishdict.com/conjugate/", + _selector: ".aa .neoConj tr td", + + init : function init(config) { + config = config || {}; + + Object.keys(config).forEach(function (property) { + this[property] = config[property]; + }, this); + }, + + 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 = $('.vtable-word')[10]; + + if (!result) { + console.log("Verb not found: ", verb); + return reject(new Error("Not a valid verb")); + } + + finalVerb = this._extractWord(result); + + console.log(verb, finalVerb); + resolve(finalVerb); + }.bind(this)); + }.bind(this)); + }, + + _extractWord : function _extractWord(node) { + var word = "", components; + + node.children.forEach(function (child) { + if (child.type === "text") { + word += child.data; + } else { + word += this._extractWord(child); + } + }, this); + + word = word.split(",")[0]; // multiple conjugations, take 1 + components = word.split(" "); // some special cases have two words + // use the last, why not + + return components[components.length - 1]; + } + } +}); + +module.exports = Conjugator; |