mattdodge / yahoofantasy

A Python SDK for the Yahoo! Fantasy Sports API
59 stars 16 forks source link

Performances dump `player_stats` KeyError #69

Open j-krl opened 2 months ago

j-krl commented 2 months ago

I am running the command yahoofantasy dump -g nhl -s 2023 -o ./data/nhl-2023-performances.csv performances for my recently completed NHL league. All other dump commands are working fine, but this one raises

WARN: Failed to fetch week 1 stats for Aleksander Barkov (427.p.5981), trying again in 2 minutes

I thought it might some kind of ratelimit, but when I throw a breakpoint in, the traceback is as follows:

Traceback (most recent call last):
  File ".../venv/lib/python3.11/site-packages/yahoofantasy/cli/dump.py", line 54, in _player_out
    for stat in player.get_stats(week_num):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/yahoofantasy/resources/player.py", line 23, in get_stats
    stats_data = self._fetch_stats(week_num)["player_stats"]
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'player_stats'

I believe the error is happening inside the player.get_stats(week_num) call inside _player_out. I didn't dig any further but can give a hand if needed.

mattdodge commented 1 month ago

I'm not a part of an NHL fantasy league so it's a bit hard for me to test. But it looks like your suspicion is correct, this is likely not due to rate limiting but is indeed some kind of bug. I wonder if the stat data comes back differently for NHL leagues? A bit surprised this wouldn't have been reported before though.

Try this snippet and see if you can see what the stats data object looks like for a player, this might lead to some hints

league = ctx.get_leagues('nhl', 2023)[0]
players = league.players()
player_1 = players[0]
stats = player_1._fetch_stats(1)