espn-ff
Version:
ESPN Fantasy Football Scraper
105 lines • 3.98 kB
JavaScript
;
var BaseUrl = 'http://games.espn.com/ffl';
var request = require('request');
var ParserService_1 = require('./parsers/ParserService');
/**
* ESPN fantasy football root class
*
* @class EspnFantasyFootball
*/
var EspnFantasyFootball = (function () {
/**
* Creates an instance of EspnFantasyFootball.
*
* @param {ConstructorOptions} options
*
* @memberOf EspnFantasyFootball
*/
function EspnFantasyFootball(options) {
this.options = options;
this.parserService = new ParserService_1.ParserService();
}
/**
* Fetches all fantasy football teams within the fantasy league.
*
* @param {FetchParseCallback<types.IFantasyTeam[]>} callback
* @returns {void}
*
* @memberOf EspnFantasyFootball
*/
EspnFantasyFootball.prototype.getFantasyTeams = function (callback) {
return this.espnGetAndParse('owners', 'leaguesetup/ownerinfo', null, callback);
};
/**
* Gets the active roster for the specified fantasy team.
*
* @param {number} teamId - Can be null for the default team identity
* @param {FetchParseCallback<types.IRoster>} callback
* @returns {void}
*
* @memberOf EspnFantasyFootball
*/
EspnFantasyFootball.prototype.getRoster = function (teamId, callback) {
var query = {};
if (teamId) {
query.teamId = teamId;
}
return this.espnGetAndParse('clubhouse', 'clubhouse', query, callback);
};
/**
* Gets the fantasy matchups for the specified week within the league.
*
* @param {number} week - Week id (1 thru N), can be null for current week.
* @param {FetchParseCallback<types.IFantasyMatchup[]>} callback
* @returns {void}
*
* @memberOf EspnFantasyFootball
*/
EspnFantasyFootball.prototype.getMatchups = function (week, callback) {
var query = {};
if (week) {
query.matchupIdPeriod = week;
}
return this.espnGetAndParse('scoreboard', 'scoreboard', query, callback);
};
EspnFantasyFootball.prototype.espnGetAndParse = function (parser, fragment, urlQuery, callback) {
var _this = this;
return this.espnGetRequest(fragment, urlQuery, function (err, response, body) {
if (err)
return callback(err);
if (response.statusCode != 200) {
return callback(new Error("Got unexpected status code '" + response.statusCode + "' from request"));
}
else if (!body) {
return callback(new Error("Got unexpected empty body from request"));
}
var result = _this.parserService.parseHtmlContent(parser, body);
if (!result) {
return callback(new Error("Parser '" + parser + "' returned empty result"));
}
return callback(null, result);
});
};
EspnFantasyFootball.prototype.espnGetRequest = function (fragment, urlQuery, callback) {
fragment = Array.isArray(fragment) ? fragment.join('/') : fragment;
urlQuery = urlQuery || {};
urlQuery.leagueId = this.options.leagueId;
var options = {
method: 'GET',
url: BaseUrl + '/' + fragment,
qs: urlQuery,
headers: {
'Accept': 'text/html',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
}
};
if (this.options.cookie) {
options.headers['Cookie'] = this.options.cookie;
}
request(options, callback);
};
return EspnFantasyFootball;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = EspnFantasyFootball;
//# sourceMappingURL=EspnFantasyFootball.js.map