I am trying to get all the available players for a position with JSON returned by the Yahoo! Fantasy API, using this resource:
http://fantasysports.yahooapis.com/fantasy/v2/game/nfl/players;status=A;position=RB
It seems like it always returns a maximum of 25 players with this API. I've tried using the ;count=n
filter as well, but if n is anything higher that 25 I still only get 25 players returned. Does anyone know why this is? And how I can get more?
Here is my code:
from yahoo_oauth import OAuth1
oauth = OAuth1(None, None, from_file='oauth.json', base_url='http://fantasysports.yahooapis.com/fantasy/v2/')
uri = 'league/nfl.l.91364/players;position=RB;status=A;count=100'
if not oauth.token_is_valid():
oauth.refresh_access_token
response = oauth.session.get(uri, params={'format': 'json'})
I did solve this. What I found was that the maximum "count" is 25, but the "start" parameter is the key to this operation. It seems the the API attaches an index to each of the players (however it is sorted) and the "start" parameter is the index to start out. It might seem odd, but the only way I could find was to get all the players back in batches of 25. So my solution in code was something like the following:
from yahoo_oauth import OAuth1
oauth = OAuth1(None, None, from_file='oauth.json', base_url='http://fantasysports.yahooapis.com/fantasy/v2/')
done = False
start = 1
while(not done) :
uri = 'league/nfl.l.<league>/players;position=RB;status=A;start=%s,count=25' % start
if not oauth.token_is_valid():
oauth.refresh_access_token
response = oauth.session.get(uri, params={'format': 'json'})
# parse response, get num of players, do stuff
start += 25
if numPlayersInResp < 25:
done = True