Skip to content

Instantly share code, notes, and snippets.

@adtaylor
Created August 30, 2015 11:07
Show Gist options
  • Save adtaylor/981f8851bca273959cae to your computer and use it in GitHub Desktop.
Save adtaylor/981f8851bca273959cae to your computer and use it in GitHub Desktop.
Access the Fantasy Football Premier League API using Node.js
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);
});
}
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