cwendt94 / espn-api

ESPN Fantasy API! (Football, Basketball)
MIT License
635 stars 200 forks source link

KeyError: 'away' on box scores #50

Closed johnmikee closed 5 years ago

johnmikee commented 5 years ago

This may very well be pebkac but when I try to pull any box scores I get a KeyError. I can pull other league settings without issue but it seems to break at this point.

Something like the following will throw the following error: box_scores = league.box_scores() print (box_scores)

Traceback (most recent call last): File "test.py", line 9, in <module> box_scores = league.box_scores(week=3) File "/Users/john.peterson/Documents/FF/venv/lib/python3.7/site-packages/ff_espn_api/league.py", line 313, in box_scores box_data = [BoxScore(matchup) for matchup in schedule] File "/Users/john.peterson/Documents/FF/venv/lib/python3.7/site-packages/ff_espn_api/league.py", line 313, in <listcomp> box_data = [BoxScore(matchup) for matchup in schedule] File "/Users/john.peterson/Documents/FF/venv/lib/python3.7/site-packages/ff_espn_api/box_score.py", line 8, in __init__ self.away_team = data['away']['teamId'] KeyError: 'away'

Is there a way to just pull the league data object to parse and see if it is being returned in a different structure? Fwiw its a private league.

Thank you for putting this together, its wonderful.

cwendt94 commented 5 years ago

At the moment there is no way to see the box_score data without calling the ESPN API in your own browser. I think that I should add a feature to have a “logger mode” that would print out all of the json returned from API calls. This would make it really easy to debug.

For the error your running into that’s interesting that there isn’t a away team. Do you have bye weeks in your league? Also could you try passing box_scores(week=1) or week=2, to see if that gives you a error.

johnmikee commented 5 years ago

I forgot to mention that I had tried with week= as well, but it yields the same KeyError. We do have a bye week because we ended up with an odd number of players.

Edit: I printed data when box_score.py is called from league.py and it is almost certainly the bye-week. After printing the set for 6 users there is a line break, <class 'KeyError'>, so I changed box_score.py to

class BoxScore(object):

    def __init__(self, data):

        try:
            self.home_team = data['home']['teamId']
            self.home_score = round(data['home']['rosterForCurrentScoringPeriod']['appliedStatTotal'], 2)
            self.away_team = data['away']['teamId']
            self.away_score =  round(data['away']['rosterForCurrentScoringPeriod']['appliedStatTotal'], 2)

            home_roster = data['home']['rosterForCurrentScoringPeriod']['entries']
            self.home_lineup = [BoxPlayer(player) for player in home_roster]

            away_roster = data['away']['rosterForCurrentScoringPeriod']['entries']
            self.away_lineup = [BoxPlayer(player) for player in away_roster]
        except KeyError as e:
            print (e)
            logging.info(e)
            pass

This continues to generate the data set after the empty user value which is good but with the empty data dictionary, it continues to error out with AttributeError: 'BoxScore' object has no attribute 'away_team'. I'll try to think of a good way to handle an empty/bye and see if I can put it together.

cwendt94 commented 5 years ago

Okay that is good to know about the bye week! It’s hard knowing what ESPN API does with different league settings. I will also try to look at it later and see if I can come up with a solution.

markten commented 5 years ago

I ran into a similar issue yesterday while trying to pull all team scores from the last 7 years of data in the league I participate in. I was iterating over years and weeks pulling scoreboard and box_score data. In my case it was requests for playoff weeks failing and as a result, the matchup object was failing to init the dict.

I independently went with a fail->log->continue solution similar to what @johnmikep did. I think it's a reasonable approach since you can go back and look at the gaps to figure out why the request may have come up with nothing.

cwendt94 commented 5 years ago

Yeah the biggest help for debugging is the json that is returned from the ESPN API. The continue could get a little tricky as some of the calls rely on that data later down for processing.

Where did it fail for playoffs? I know playoffs can be a little tricky as matchup period and scoring period can differ with same matchup span multiple scoring periods.

dhellfeld commented 4 years ago

Box score is fixed, but this issue still remains with scoreboard. Reopen?