Created
August 30, 2015 01:34
-
-
Save lucusp/52ef9de924b983f4da9c to your computer and use it in GitHub Desktop.
fantasy.nfl.com api function v2
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
| <ul id="data"></ul> |
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
| /* | |
| Theory behind function setup: | |
| This is a recursive function, | |
| that reuses parameter values, therefore | |
| global variables are a no, no. | |
| Parameter descriptions: | |
| offset = the starting value for NFL.com API | |
| total = the total number of records to return | |
| ptype = "OFF" or "DEF" player types | |
| pdata = the API will only return a max of 100 records, pass in an empty string to hold those records | |
| pseqNo = the starting value for the player ranks (usually 0) | |
| addOffset = the starting value for offset, used primarily for returns > 100 (usually 0) | |
| callback = callback function for using the data returned | |
| */ | |
| function getPlayerData(offset, total, ptype, pdata, pseqNo, addOffset, callback) { | |
| var url = 'http://api.fantasy.nfl.com/players/editordraftranks?count=100&offset=' + offset + '&format=json&callback=?'; | |
| var type = ptype; | |
| var seqNo = pseqNo; | |
| var updateOffset = addOffset; | |
| var keys = {}; | |
| var players = pdata; | |
| if (type === "DEF") { | |
| keys = { | |
| "LB": "LB", | |
| "DL": "DL", | |
| "DB": "DB" | |
| }; | |
| } else { | |
| keys = { | |
| "QB": "QB", | |
| "RB": "RB", | |
| "WR": "WR", | |
| "TE": "TE", | |
| "DEF": "DEF", | |
| "K": "K" | |
| }; | |
| } | |
| $.getJSON(url, function(data) {}).success(function(data) { | |
| for (var i = 0; i < data.players.length; i++) { | |
| if (keys.hasOwnProperty(data.players[i].position)) { | |
| var team = data.players[i].teamAbbr || 'FA'; | |
| //temp | |
| var byeWeeks = {}; | |
| if (team !== 'FA') { | |
| seqNo++; | |
| players += '<li class="players ALL SHOW ' + data.players[i].position + '"><span style="line-height: 60px;" class="playerData"><span class="circle abbr ' + team + '">' + seqNo + '</span>' + '<ul class="dataHolder" style="list-style-type: none;"><li>' + data.players[i].firstName + " " + data.players[i].lastName + ", " + team + ", " + data.players[i].position + '</li><li>Bye Week ' + byeWeeks[team] + '</li></ul><span class="handle" style="float:right; line-height: 60px;"><i class="fa fa-2x fa-bars"></i></span></li>'; | |
| } | |
| } | |
| //need to add a break in here that hits once seqNo === total | |
| //otherwise continue to grab players | |
| if(seqNo === total){ | |
| callback(players) | |
| } | |
| } | |
| //need this outside of the loop | |
| if(seqNo < total) { | |
| updateOffset += 100; | |
| getPlayerData(updateOffset, total, type, players, seqNo, updateOffset, callback); | |
| } | |
| }); | |
| } | |
| //empty strings to pass in | |
| var offPlayers = ''; | |
| var defPlayers = ''; | |
| //set up the global default list object | |
| var defaultPlayerData = {}; | |
| getPlayerData(0, 300, "OFF", offPlayers, 0, 0, function(data){ | |
| defaultPlayerData["OFF"] = data; | |
| $('#data').append(defaultPlayerData.OFF); | |
| }); | |
| getPlayerData(0, 300, "DEF", defPlayers, 0, 0, function(data){ | |
| defaultPlayerData["DEF"] = data; | |
| console.log(defaultPlayerData.DEF); | |
| }); | |
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
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment