espn-ff
Version:
ESPN Fantasy Football Scraper
52 lines • 2.09 kB
JavaScript
;
var cheerio = require('cheerio');
var ParserService = (function () {
function ParserService() {
this.parsers = new Map();
// Push all known parsers into the parser map
this.addParsers('./Clubhouse');
this.addParsers('./Owners');
this.addParsers('./Scoreboard');
}
ParserService.prototype.parseHtmlContent = function (key, html) {
var selector = cheerio.load(html);
var rootParser = this.parsers.get(key);
if (!rootParser) {
throw new Error("Unable to find parser '" + key + "' avaliable parsers -> [" + this.parsers.keys().map(function (k) { return ("'" + k + "'"); }).join(', ') + "]");
}
return rootParser.parse(new ParseContext(selector, this));
};
ParserService.prototype.getParser = function (key) {
return this.parsers.get(key);
};
ParserService.prototype.addParsers = function (path) {
var _this = this;
var ParserImplCtor = require(path);
ParserImplCtor = ParserImplCtor.default ? ParserImplCtor.default : ParserImplCtor;
var instance = new ParserImplCtor();
// TODO: Could make this better
if (instance.parse && instance.name) {
this.parsers.set(instance.name, instance);
}
if (instance.provide) {
var instances = instance.provide();
if (instances) {
instances.forEach(function (childParser) { return _this.parsers.set(childParser.name, childParser); });
}
}
};
return ParserService;
}());
exports.ParserService = ParserService;
var ParseContext = (function () {
function ParseContext(selector, parent) {
this.selector = selector;
this.parent = parent;
}
ParseContext.prototype.parseFragment = function (key, selector) {
var parser = this.parent.getParser(key);
parser.parse(new ParseContext(selector, this.parent));
};
return ParseContext;
}());
//# sourceMappingURL=ParserService.js.map