From 21167c79876cb8ca37d24d36181a0ce3a1090f6f Mon Sep 17 00:00:00 2001 From: todd Date: Sat, 25 Jul 2020 22:47:29 -0400 Subject: [PATCH] FIX: game_scoring_plays() and game_scoring_play_data() broken due to MLB data change #44 VER: 0.1.9 --- statsapi/__init__.py | 38 +++++++++++++++++++++++++------------- statsapi/version.py | 2 +- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/statsapi/__init__.py b/statsapi/__init__.py index 35fd52a..92d71ac 100644 --- a/statsapi/__init__.py +++ b/statsapi/__init__.py @@ -927,30 +927,42 @@ def game_scoring_play_data(gamePk): * plays - sorted list of scoring play data """ r = get( - "schedule", + "game", { - "sportId": 1, "gamePk": gamePk, - "hydrate": "scoringplays", - "fields": "dates,date,games,teams,away,team,name,scoringPlays,result,description,awayScore,homeScore,about,halfInning,inning,endTime", + "fields": ( + "gamePk,link,gameData,game,pk,teams,away,id,name,teamCode,fileCode," + "abbreviation,teamName,locationName,shortName,home,liveData,plays," + "allPlays,scoringPlays,scoringPlays,atBatIndex,result,description," + "awayScore,homeScore,about,halfInning,inning,endTime" + ), }, ) - if not len(r["dates"][0]["games"][0]["scoringPlays"]): - return "" - - items = r["dates"][0]["games"][0]["scoringPlays"] - home_team = r["dates"][0]["games"][0]["teams"]["home"]["team"] - away_team = r["dates"][0]["games"][0]["teams"]["away"]["team"] + if not len(r["liveData"]["plays"].get("scoringPlays", [])): + return { + "home": r["gameData"]["teams"]["home"], + "away": r["gameData"]["teams"]["away"], + "plays": [], + } unorderedPlays = {} - for v in items: - unorderedPlays.update({v["about"]["endTime"]: v}) + for i in r["liveData"]["plays"].get("scoringPlays", []): + play = next( + (p for p in r["liveData"]["plays"]["allPlays"] if p.get("atBatIndex") == i), + None, + ) + if play: + unorderedPlays.update({play["about"]["endTime"]: play}) sortedPlays = [] for x in sorted(unorderedPlays): sortedPlays.append(unorderedPlays[x]) - return {"home": home_team, "away": away_team, "plays": sortedPlays} + return { + "home": r["gameData"]["teams"]["home"], + "away": r["gameData"]["teams"]["away"], + "plays": sortedPlays, + } def game_highlights(gamePk): diff --git a/statsapi/version.py b/statsapi/version.py index c17f306..446ce18 100644 --- a/statsapi/version.py +++ b/statsapi/version.py @@ -1,3 +1,3 @@ #!/usr/bin/env python -VERSION = "0.1.8" +VERSION = "0.1.9"