yahoo_fantasy_api icon indicating copy to clipboard operation
yahoo_fantasy_api copied to clipboard

Get NBA player rank and projected rank?

Open salbro opened this issue 5 years ago • 4 comments

Is there a way to get an NBA player's rank and projected rank? I can't seem to figure it out.

salbro avatar Jan 31 '21 18:01 salbro

Bump. I would love to figure this out as well. From what I can tell, the Players Collection of the Yahoo Fantasy Sports API can return player rank. I can't seem to find out how to do this with this library.

Thank you for this resource. It is incredibly useful!

JaumeClave avatar Feb 05 '21 21:02 JaumeClave

I think I have figured it out @salbro. The Yhandler class inside yhandler.py has a get() function (here). You can append your URI to it and return player original rank or actual rank.

from yahoo_oauth import OAuth2 from yahoo_fantasy_api import yhandler sc = OAuth2(None, None, from_file={your_file_path})
yh = yhandler.YHandler(sc)
yh.get(f"/league/{your_league_id}/players;status=FA;sort=AR")

FA, as documented in this library, returns league free agents and AR will return the actual rank based on your league settings. This repo has better documentation than the Yahoo Fantasy Sports API documentation. I believe Original Rank OR is projected rank but I am not 100% on that.

JaumeClave avatar Feb 05 '21 23:02 JaumeClave

Thanks, JaumeClave! That's really helpful. For posterity, here's some code to fetch all players ranks (and name and keys) from a league object:

def get_player_ranks(league):
    all_ranks = {}
    rank_start = 0
    while True:
        ranked_players = league.yhandler.get(f"/league/{league.league_id}/players;sort=AR;start={rank_start}")
        player_dict = ranked_players['fantasy_content']['league'][1]['players']
        if player_dict == []:
            break
        ranks = {}
        for maybe_rank, player in player_dict.items():
            if maybe_rank == 'count':
                continue
            player_key = player['player'][0][0]['player_key'] 
            player_id = int(player['player'][0][1]['player_id'])
            player_full_name = player['player'][0][2]['name']['full']
            ranks[player_id] = {'rank': rank_start + int(maybe_rank) + 1,
                                'key': player_key,
                                'full name': player_full_name}
        all_ranks.update(ranks)
        rank_start += 25
    return all_ranks

salbro avatar Feb 06 '21 16:02 salbro

@salbro - Yeah, that is great! Thanks for sharing that. I'll be using it in my project!

JaumeClave avatar Feb 07 '21 09:02 JaumeClave