Skip to content

Commit 69ce2ae

Browse files
authored
Check if highlights exist gracefully
Currently, the `highlights` object can be None, such as here: ``` >>> r = get( ... "schedule", ... { ... "sportId": 1, ... "gamePk": gamePk, ... "hydrate": "game(content(highlights(highlights)))", ... "fields": "dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url", ... }, ... ) >>> >>> r {'dates': [{'date': '2023-02-25', 'games': [{'gamePk': 719386, 'content': {'highlights': {'highlights': None}}}]}]} ``` The change both checks for it gracefully rather than throwing a `TypeError: 'NoneType' object is not subscriptable` and returns an empty list rather than an empty string, to match the other return values
1 parent ecf58c8 commit 69ce2ae

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

statsapi/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,15 +1038,12 @@ def game_highlight_data(gamePk):
10381038
"fields": "dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url",
10391039
},
10401040
)
1041-
if not len(
1042-
r["dates"][0]["games"][0]["content"]["highlights"]["highlights"]["items"]
1043-
):
1044-
return ""
1045-
1046-
items = r["dates"][0]["games"][0]["content"]["highlights"]["highlights"]["items"]
1041+
gameHighlights = r["dates"][0]["games"][0]["content"]["highlights"]["highlights"]
1042+
if not gameHighlights or len(gameHighlights) == 0:
1043+
return []
10471044

10481045
unorderedHighlights = {}
1049-
for v in (x for x in items if isinstance(x, dict) and x["type"] == "video"):
1046+
for v in (x for x in gameHighlights["items"] if isinstance(x, dict) and x["type"] == "video"):
10501047
unorderedHighlights.update({v["date"]: v})
10511048

10521049
sortedHighlights = []

0 commit comments

Comments
 (0)