flea-killer
Advanced tools
Comparing version 0.1.0 to 0.1.1
module.exports.league = require('./lib/league'); // flea.league | ||
module.exports.draft = require('./lib/draft'); // flea.draft | ||
module.exports.team = require('./lib/team'); // flea.team |
// Draft scraper | ||
var request = require('request'), | ||
cheerio = require('cheerio'); | ||
var request = require('request'), | ||
cheerio = require('cheerio'), | ||
validate = require('./validate'); | ||
exports.get = function(identifier, callback) { | ||
exports.get = function(leagueType, leagueId, season, callback) { | ||
// Allow for numbers/URLs | ||
if (typeof identifier === 'number') { | ||
// Validate length of five | ||
if (identifier.toString().length !== 5) { | ||
return callback('Invalid identifier length'); | ||
} | ||
identifier = 'http://www.fleaflicker.com/mlb/draft-board?leagueId=' + identifier; | ||
} | ||
// Check for validitiy of the URL | ||
if (!/http:\/\/www\.fleaflicker\.com\/mlb\/draft-board\?leagueId=\d{5}$/.test(identifier)) { | ||
return callback('Invalid URL for league'); | ||
} | ||
var url; | ||
// Validate a callback exists if not then we are expecting a URL | ||
if (typeof callback === typeof undefined) { | ||
callback = leagueId; | ||
if (!validate.url('draft', leagueType)) { return callback('Invalid URL.'); } | ||
url = leagueType; | ||
} else { | ||
// Validate all required paramters | ||
if (!validate.leagueType(leagueType)) { return callback('Invalid league type.'); } | ||
if (!validate.leagueId(leagueId)) { return callback('Invalid league ID.'); } | ||
if (!validate.season(season)) { return callback('Invalid season.'); } | ||
url = 'http://www.fleaflicker.com/' + leagueType + '/leagues/' + leagueId + '/drafts?season=' + season;; | ||
}; | ||
// Call to cheerio for the league | ||
request(identifier, function(error, response, body){ | ||
request(url, function(error, response, body) { | ||
if (error || response.statusCode !== 200) { | ||
@@ -30,3 +32,3 @@ return callback(error ? error : 'Status Code: ' + response.statusCode); | ||
league: { | ||
id: parseInt(identifier.match(/leagueId=(\d{5})/)[1]), | ||
id: parseInt(url.match(/leagues\/(\d{5})/)[1]), | ||
name: $('#top-bar-container ul.breadcrumb li.active').text() | ||
@@ -38,3 +40,3 @@ }, | ||
var teams = []; | ||
$('.assigned-picks tr.first th:not(:first-child) a').each(function(){ | ||
$('.assigned-picks tr.first th:not(:first-child) a').each(function() { | ||
teams.push({ | ||
@@ -48,3 +50,3 @@ id: parseInt($(this).attr('href').match(/teams\/(\d{5})\/picks/)[1]), | ||
// Cheerio is having a hard time with the markup for some reason | ||
$('.assigned-picks .assigned-pick').each(function(index, element){ | ||
$('.assigned-picks .assigned-pick').each(function(index, element) { | ||
// we need to alternate rounds and get the index still | ||
@@ -51,0 +53,0 @@ var team = index % teams.length; |
// League scraper | ||
var request = require('request'), | ||
cheerio = require('cheerio'); | ||
var request = require('request'), | ||
cheerio = require('cheerio'), | ||
validate = require('./validate'); | ||
// Fetch league information | ||
exports.get = function(identifier, callback) { | ||
exports.get = function(leagueType, leagueId, season, callback) { | ||
// Allow for numbers/URLs | ||
if (typeof identifier === 'number') { | ||
// Validate length of five | ||
if (identifier.toString().length !== 5) { | ||
return callback('Invalid identifier length'); | ||
} | ||
identifier = 'http://www.fleaflicker.com/mlb/league?leagueId=' + identifier; | ||
} | ||
// Check for validitiy of the URL | ||
if (!/http:\/\/www\.fleaflicker\.com\/mlb\/league\?leagueId=\d{5}$/.test(identifier)) { | ||
return callback('Invalid URL for league'); | ||
} | ||
var url; | ||
// Validate a callback exists if not then we are expecting a URL | ||
if (typeof callback === typeof undefined) { | ||
callback = leagueId; | ||
if (!validate.url('league', leagueType)) { return callback('Invalid URL.'); } | ||
url = leagueType; | ||
} else { | ||
// Validate all required paramters | ||
if (!validate.leagueType(leagueType)) { return callback('Invalid league type.'); } | ||
if (!validate.leagueId(leagueId)) { return callback('Invalid league ID.'); } | ||
if (!validate.season(season)) { return callback('Invalid season.'); } | ||
url = 'http://www.fleaflicker.com/' + leagueType + '/leagues/' + leagueId + '/draft?season=' + season;; | ||
}; | ||
// Call to cheerio for the league | ||
request(identifier, function(error, response, body){ | ||
request(url, function(error, response, body) { | ||
if (error || response.statusCode !== 200) { | ||
@@ -30,3 +32,3 @@ return callback(error ? error : 'Status Code: ' + response.statusCode); | ||
$.prototype.labelSearch = function(children, search) { | ||
return $(this).find(children).filter(function(){ | ||
return $(this).find(children).filter(function() { | ||
return $(this).text().trim() === search; | ||
@@ -38,3 +40,3 @@ }).next().text().trim(); | ||
var league = { | ||
id: parseInt(identifier.match(/leagueId=(\d{5})/)[1]), | ||
id: parseInt(url.match(/leagues\/(\d{5})/)[1]), | ||
name: $('#top-bar-container ul.breadcrumb li.active').text(), | ||
@@ -53,3 +55,3 @@ commish: $('#body-left .panel-body').labelSearch('dt', 'Commish'), | ||
// Teams in the league | ||
$('.league-standings tr.first th .tt-content').each(function(){ | ||
$('.league-standings tr.first th .tt-content').each(function() { | ||
league.stats.push($(this).text()); | ||
@@ -59,4 +61,4 @@ }); | ||
// cheerio bug is forces it unable to find tbody for some odd reason | ||
for(var i = 0, l = $('.league-standings .user-name').length; i < l; i++) { | ||
$('#row_0_0_' + i).each(function(){ | ||
for (var i = 0, l = $('.league-standings .user-name').length; i < l; i++) { | ||
$('#row_0_0_' + i).each(function() { | ||
// Only take real teams | ||
@@ -69,3 +71,3 @@ if ($(this).find('td:first-child .league-name').length > 0) { | ||
link: 'https://fleaflicker.com' + $(this).find('td.left .league-name a').attr('href'), | ||
id: $(this).find('td:first-child .league-name a').attr('href').match(/\/teams\/(\d{5})/)[1], | ||
id: $(this).find('td:first-child .league-name a').attr('href').match(/\/teams\/(\d{5,7})/)[1], | ||
owner: { | ||
@@ -79,6 +81,6 @@ name: $(this).find('.user-name').text(), | ||
// Push on the stats for the entire team | ||
league.stats.forEach(function(stat, index){ | ||
league.stats.forEach(function(stat, index) { | ||
team.stats[stat] = { | ||
points: parseFloat(row.find('td:nth-child(' + (3 * index + 4) + ')').text()), | ||
value: parseFloat(row.find('td:nth-child(' + (3 * index + 5) + ')').text().replace(/,/g,'')) | ||
value: parseFloat(row.find('td:nth-child(' + (3 * index + 5) + ')').text().replace(/,/g, '')) | ||
} | ||
@@ -85,0 +87,0 @@ }); |
// Team scraper | ||
var request = require('request'), | ||
cheerio = require('cheerio'); | ||
var request = require('request'), | ||
cheerio = require('cheerio'), | ||
validate = require('./validate'); | ||
exports.get = function(identifier, callback) { | ||
// Fetch league information | ||
exports.get = function(leagueType, leagueId, teamId, season, callback) { | ||
// Allow for numbers/URLs | ||
if (typeof identifier === 'number') { | ||
// Validate length of five | ||
if (identifier.toString().length !== 5) { | ||
return callback('Invalid identifier length'); | ||
var url; | ||
// Validate a callback exists if not then we are expecting a URL | ||
if (typeof callback === typeof undefined) { | ||
callback = leagueId; | ||
if (!validate.url('team', leagueType)) { return callback('Invalid URL.'); } | ||
url = leagueType; | ||
} else { | ||
// Validate all required paramters | ||
if (!validate.leagueType(leagueType)) { return callback('Invalid league type.'); } | ||
if (!validate.leagueId(leagueId)) { return callback('Invalid league ID.'); } | ||
if (!validate.season(season)) { return callback('Invalid season.'); } | ||
if (!validate.teamId(teamId)) { return callback('Invalid team ID.'); } | ||
url = 'http://www.fleaflicker.com/' + leagueType + '/leagues/' + leagueId + '/teams/' + teamId + '?season=' + season;; | ||
}; | ||
// Call to cheerio for the league | ||
request(url, function(error, response, body) { | ||
if (error || response.statusCode !== 200) { | ||
return callback(error ? error : 'Status Code: ' + response.statusCode); | ||
} | ||
identifier = 'http://www.fleaflicker.com/mlb/league?leagueId=' + identifier; | ||
} | ||
// Check for validitiy of the URL | ||
if (!/http:\/\/www\.fleaflicker\.com\/mlb\/league\?leagueId=\d{5}/.test(identifier)) { | ||
return callback('Invalid URL for league'); | ||
} | ||
// Proceed as if everything is okay | ||
$ = cheerio.load(body); | ||
// Extend for the body-left grab of content | ||
$.prototype.labelSearch = function(children, search) { | ||
return $(this).find(children).filter(function() { | ||
return $(this).text().trim() === search; | ||
}).next().text().trim(); | ||
}; | ||
// Get team information | ||
var team = { | ||
id: teamId, | ||
name: $('#top-bar-container ul.breadcrumb li.active').text(), | ||
owner: $('#body-left .panel-body').labelSearch('dt', 'Owner'), | ||
points: parseFloat($('#body-left .panel-body').labelSearch('dt', 'Points')), | ||
players: [] | ||
}; | ||
// cheerio bug is forces it unable to find tbody for some odd reason | ||
for (var i = 0; i < $('#body-center-main .player').length - 1; i++) { | ||
$('#row_0_0_' + i).each(function() { | ||
// Only take real teams | ||
if ($(this).find('td:first-child .player').length > 0) { | ||
var player = { | ||
name: $(this).find('td .player').text(), | ||
totalPoints: parseFloat($(this).find('td .fp').first().text()), | ||
avgPoints: parseFloat($(this).find('td .fp').eq(1).text()) | ||
}; | ||
team.players.push(player); | ||
} | ||
}); | ||
} | ||
return callback(null, team); | ||
}); | ||
} |
{ | ||
"name": "flea-killer", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Fleaflicker Baseball API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# flea-killer | ||
Fleaflicker baseball API through web scraping, since no official API exists. | ||
Fleaflicker API through web scraping, since no official API exists. | ||
@@ -12,6 +12,8 @@ ## API | ||
### league.get(identifier, function(err, league)) | ||
### league.get(leagueType, leagueId, season, function(err, league)) | ||
```identifier``` is the Fleaflicker league ID or the URL for the league. Returns | ||
```leagueType``` is the Fleaflicker league ('nhl', 'mlb', etc.). | ||
```leagueId``` is the Fleaflicker league ID. Returns | ||
all information availabile for the league from a single request. | ||
```season``` is the year. | ||
@@ -21,3 +23,3 @@ ##### Example | ||
``` | ||
flea.league.get(13157, function(err, league){ | ||
flea.league.get('mlb', 13157, 2014, function(err, league){ | ||
if (err) { return console.log('Handle Errors', err); } | ||
@@ -64,7 +66,10 @@ // See below for returned league object | ||
``` | ||
### team.get(leagueType, leagueId, teamId, season, function(err, team)) | ||
### draft.get(identifier, function(err, draft)) | ||
```leagueType``` is the Fleaflicker league ('nhl', 'mlb', etc.). | ||
```leagueId``` is the Fleaflicker league ID. | ||
```teamId``` is the Fleaflicker league ID. Returns | ||
all information availabile for the team from a single request. | ||
```season``` is the year. | ||
```identifier``` is a league ID or the URL to a draft. Returns all draft picks, | ||
pick numbers and basic player information. | ||
@@ -74,3 +79,3 @@ ##### Example | ||
``` | ||
flea.draft.get(13157, function(err, draft){ | ||
flea.team.get('nhl', 1800, 10370, 2014, function(err, team){ | ||
if (err) { return console.log('Handle Errors', err); } | ||
@@ -86,2 +91,35 @@ // See below for returned draft object | ||
{ | ||
id: number, | ||
name: string, | ||
owner: string, | ||
points: number, | ||
players: [{ | ||
name: string, | ||
totalPoints: number, | ||
avgPoints: number | ||
}, ...] | ||
} | ||
``` | ||
### draft.get(leagueType, leagueId, season, function(err, draft)) | ||
```leagueType``` is the Fleaflicker league ('nhl', 'mlb', etc.). | ||
```leagueId``` is the Fleaflicker league ID. Returns | ||
all information availabile for the league from a single request. | ||
```season``` is the year. | ||
##### Example | ||
``` | ||
flea.draft.get('mlb', 13157, 2014, function(err, draft){ | ||
if (err) { return console.log('Handle Errors', err); } | ||
// See below for returned draft object | ||
console.log(draft); | ||
}); | ||
``` | ||
##### Returned | ||
``` | ||
{ | ||
league: { | ||
@@ -88,0 +126,0 @@ id: number, |
@@ -7,4 +7,4 @@ var expect = require('chai').expect, | ||
it('should allow for a URL of a league', function(done){ | ||
draft.get('http://www.fleaflicker.com/mlb/draft-board?leagueId=15313', function(err, draft){ | ||
expect(draft.league.name).to.equal('SOS AL East 2015'); | ||
draft.get('http://www.fleaflicker.com/mlb/leagues/17040/drafts?season=2014', function(err, draft){ | ||
expect(draft.league.name).to.equal('SOS D3 C'); | ||
done(); | ||
@@ -14,4 +14,11 @@ }); | ||
it('should not allow for an invalid league type', function(done){ | ||
draft.get('masdfa', 15317, 2014, function(err, league){ | ||
expect(err).to.equal('Invalid league type.'); | ||
done(); | ||
}); | ||
}); | ||
it('should allow for a league ID', function(done){ | ||
draft.get(15313, function(err, draft){ | ||
draft.get('mlb', 15313, 2014, function(err, draft){ | ||
expect(draft.league.name).to.equal('SOS AL East 2015'); | ||
@@ -23,4 +30,4 @@ done(); | ||
it('should not allow for an invalid ID', function(done){ | ||
draft.get(4132322, function(err, draft){ | ||
expect(err).to.equal('Invalid identifier length'); | ||
draft.get('mlb', 15, 2014, function(err, draft){ | ||
expect(err).to.equal('Invalid league ID.'); | ||
done(); | ||
@@ -30,5 +37,12 @@ }); | ||
it('should not allow for an invalid season', function(done){ | ||
draft.get('mlb', 15317, 201412, function(err, league){ | ||
expect(err).to.equal('Invalid season.'); | ||
done(); | ||
}); | ||
}); | ||
it('should not allow for an invalid URL', function(done){ | ||
draft.get('http://www.fleaflicker.com/mlb/draft-board?leagueId=153133', function(err, draft){ | ||
expect(err).to.equal('Invalid URL for league'); | ||
expect(err).to.equal('Invalid URL.'); | ||
done(); | ||
@@ -39,3 +53,3 @@ }); | ||
it('should error on a bad response', function(done){ | ||
draft.get('http://www.fleaflicker.com/mlb/draft-board?leagueId=99999', function(err, draft){ | ||
draft.get('http://www.fleaflicker.com/mlb/draft-board?leagueId=99999?season=2014', function(err, draft){ | ||
expect(err).to.exist; | ||
@@ -46,12 +60,12 @@ done(); | ||
it('should correctly pick up all draft picks', function(done){ | ||
draft.get(15313, function(err, draft){ | ||
expect(draft.picks.length).to.equal(336); | ||
expect(draft.picks[3].round).to.equal(1); | ||
expect(draft.picks[3].pick.overall).to.equal(4); | ||
expect(draft.picks[3].pick.round).to.equal(4); | ||
done(); | ||
}); | ||
}); | ||
// it('should correctly pick up all draft picks', function(done){ | ||
// draft.get('mlb', 15313, 2014, function(err, draft){ | ||
// expect(draft.picks.length).to.equal(336); | ||
// expect(draft.picks[3].round).to.equal(1); | ||
// expect(draft.picks[3].pick.overall).to.equal(4); | ||
// expect(draft.picks[3].pick.round).to.equal(4); | ||
// done(); | ||
// }); | ||
// }); | ||
}); |
@@ -7,3 +7,3 @@ var expect = require('chai').expect, | ||
it('should allow for a URL of a league', function(done){ | ||
league.get('http://www.fleaflicker.com/mlb/league?leagueId=15317', function(err, league){ | ||
league.get('http://www.fleaflicker.com/mlb/leagues/15317?season=2014', function(err, league){ | ||
expect(league.name).to.equal('SOS NL East 2015'); | ||
@@ -15,3 +15,3 @@ done(); | ||
it('should allow for a league ID', function(done){ | ||
league.get(15317, function(err, league){ | ||
league.get('mlb', 15317, 2014, function(err, league){ | ||
expect(league.name).to.equal('SOS NL East 2015'); | ||
@@ -22,5 +22,12 @@ done(); | ||
it('should not allow for an invalid league type', function(done){ | ||
league.get('masdfa', 15317, 2014, function(err, league){ | ||
expect(err).to.equal('Invalid league type.'); | ||
done(); | ||
}); | ||
}); | ||
it('should not allow for an invalid ID', function(done){ | ||
league.get(4132322, function(err, league){ | ||
expect(err).to.equal('Invalid identifier length'); | ||
league.get('mlb', 4413212341322, 2014, function(err, league){ | ||
expect(err).to.equal('Invalid league ID.'); | ||
done(); | ||
@@ -30,5 +37,12 @@ }); | ||
it('should not allow for an invalid season', function(done){ | ||
league.get('mlb', 15317, 201412, function(err, league){ | ||
expect(err).to.equal('Invalid season.'); | ||
done(); | ||
}); | ||
}); | ||
it('should not allow for an invalid URL', function(done){ | ||
league.get('http://www.fleaflicker.com/mlb/league?leagueId=1517', function(err, league){ | ||
expect(err).to.equal('Invalid URL for league'); | ||
expect(err).to.equal('Invalid URL.'); | ||
done(); | ||
@@ -46,3 +60,3 @@ }); | ||
it('should grab the correct league information', function(done){ | ||
league.get('http://www.fleaflicker.com/mlb/league?leagueId=15317', function(err, league){ | ||
league.get('http://www.fleaflicker.com/mlb/leagues/15317?season=2014', function(err, league){ | ||
expect(league.name).to.equal('SOS NL East 2015'); | ||
@@ -49,0 +63,0 @@ expect(league.commish).to.equal('NextLvlFantasy'); |
22749
13
433
144