Skip to content

Commit 426433a

Browse files
committed
fix: last_game() should actually return last game
1 parent 9418370 commit 426433a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

statsapi/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,17 +866,24 @@ def linescore(gamePk, timecode=None):
866866

867867

868868
def last_game(teamId):
869-
"""Get the gamePk for the given team's most recent game.
870-
Note: Sometimes Stats API will actually return the next game in the previousSchedule hydration
869+
"""Get the gamePk for the given team's most recent completed game.
871870
"""
872-
return get(
871+
previousSchedule = get(
873872
"team",
874873
{
875874
"teamId": teamId,
876875
"hydrate": "previousSchedule",
877-
"fields": "teams,id,teamName,previousGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name",
876+
"fields": "teams,team,id,previousGameSchedule,dates,date,games,gamePk,gameDate,status,abstractGameCode",
878877
},
879-
)["teams"][0]["previousGameSchedule"]["dates"][0]["games"][0]["gamePk"]
878+
)
879+
games = []
880+
for d in previousSchedule["teams"][0]["previousGameSchedule"]["dates"]:
881+
games.extend([x for x in d["games"] if x["status"]["abstractGameCode"] == "F"])
882+
883+
if not len(games):
884+
return None
885+
886+
return games[-1]["gamePk"]
880887

881888

882889
def next_game(teamId):

0 commit comments

Comments
 (0)