Created
August 30, 2015 11:07
-
-
Save adtaylor/981f8851bca273959cae to your computer and use it in GitHub Desktop.
Access the Fantasy Football Premier League API using Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fplFetch = require('./fplFetch.js'), | |
PLAYER_COUNT = 590; | |
// | |
// Fetch All Players | |
// | |
for(var i = 0; i < PLAYER_COUNT; i++) { | |
fplStat( i, function(data) { | |
console.log('Player data:' ,data); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
// | |
// Constants | |
// | |
var PLAYER_URL = "http://fantasy.premierleague.com/web/api/elements/"; | |
// | |
// Modules | |
// | |
var fplFetch = function( ID, cb ) { | |
http.get( PLAYER_URL + ID + "/" , function(res) { | |
var body = ''; | |
res.on('data', function(chunk) { | |
if( res.statusCode != 404 ) body += chunk; | |
}); | |
res.on('end', function() { | |
if(!body.length) return console.log( PLAYER_URL + ID , body.length ); | |
var data = JSON.parse(body); | |
data._id = data.id; | |
cb( data ); | |
}); | |
}).on('error', function(e) { | |
console.log("Got error: ", e); | |
}); | |
} | |
module.exports = fplFetch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment