toddrob99 / MLB-StatsAPI

Python wrapper for MLB Stats API
GNU General Public License v3.0
533 stars 100 forks source link

2011 schedule function not working to get list of game_ids #67

Closed bennettcolecohen closed 2 years ago

bennettcolecohen commented 2 years ago

I'm getting all game_ids to pull boxscores from older seasons, and am using the statsapi.schedule function per the minimum example below. This throws an error shown below where 'score' isn't recognized. This code works for 2010 season and 2012-current seasons...just 2011. Anyone know the fix?

import statsapi games = statsapi.schedule(start_date = '03/30/2011', end_date = '10/28/2011') Screen Shot 2022-02-01 at 2 05 52 PM

toddrob99 commented 2 years ago

Apparently the Cubs played an Exhibition game against themselves on 3/30/2011, and that game doesn't have any score data. The statsapi.schedule() method is pulling some data from each game record, and the score field not being present despite game status being final causes the error. I could handle the missing score and default it to 0, but it seems kind of wrong to assign an arbitrary score to a final game.

If you are looking for regular season and postseason games, you probably want to use 3/31/2011 as the start date instead of 3/30. The start of the 2011 regular season was 3/31/2011.

If you are just looking for gamePks, and don't need the other data that the statsapi.schedule() method returns, you can get them a more efficient way. Something like this: sched = statsapi.get("schedule", {"sportId": 1, "startDate": "03/30/2011", "endDate": "10/28/2011", "fields": "dates,date,totalGames,games,gamePk,link,gameType"}), and if you want to include only specific game types, include something like "gameType": "R,F,D,L,W" in the parameters.

bennettcolecohen commented 2 years ago

Got it, thanks! The schedule function works just fine, but thanks for the tip about using the general GET for that. Cheers!