Comments (2)
The gamePk for statsapi.last_game(143)
is 630896, and the URL the statsapi.game_highlights(630896)
method is calling is https://statsapi.mlb.com/api/v1/schedule?sportId=1&gamePk=630896&hydrate=game(content(highlights(highlights)))&fields=dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url. The error is being thrown because the response from the API does not include a highlights section. I could add protection for missing highlight data, but with the MLB API it's always best to expect exceptions due to missing data because it does happen fairly often.
>>> import logging
>>> import statsapi
>>> logger = logging.getLogger('statsapi')
>>> logger.setLevel(logging.DEBUG)
>>> rootLogger = logging.getLogger()
>>> rootLogger.setLevel(logging.DEBUG)
>>> ch = logging.StreamHandler()
>>> formatter = logging.Formatter("%(asctime)s - %(levelname)8s - %(name)s(%(thread)s) - %(message)s")
>>> ch.setFormatter(formatter)
>>> rootLogger.addHandler(ch)
>>> statsapi.game_highlights(statsapi.last_game(143))
2020-12-22 14:27:00,473 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/{ver}/teams/{teamId}
2020-12-22 14:27:00,475 - DEBUG - statsapi(19844) - Found path param: teamId
2020-12-22 14:27:00,476 - DEBUG - statsapi(19844) - Found query param: hydrate
2020-12-22 14:27:00,476 - DEBUG - statsapi(19844) - Found query param: fields
2020-12-22 14:27:00,477 - DEBUG - statsapi(19844) - path_params: {'teamId': '143'}
2020-12-22 14:27:00,478 - DEBUG - statsapi(19844) - query_params: {'hydrate': 'previousSchedule', 'fields': 'teams,id,teamName,previousGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name'}
2020-12-22 14:27:00,479 - DEBUG - statsapi(19844) - Replacing {teamId}
2020-12-22 14:27:00,480 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/{ver}/teams/143
2020-12-22 14:27:00,481 - DEBUG - statsapi(19844) - Replacing {ver} with default: v1.
2020-12-22 14:27:00,483 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/teams/143
2020-12-22 14:27:00,485 - DEBUG - statsapi(19844) - Adding query parameter hydrate=previousSchedule
2020-12-22 14:27:00,486 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/teams/143?hydrate=previousSchedule
2020-12-22 14:27:00,488 - DEBUG - statsapi(19844) - Adding query parameter fields=teams,id,teamName,previousGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name
2020-12-22 14:27:00,492 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/teams/143?hydrate=previousSchedule&fields=teams,id,teamName,previousGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name
2020-12-22 14:27:00,510 - DEBUG - urllib3.connectionpool(19844) - Starting new HTTPS connection (1): statsapi.mlb.com:443
2020-12-22 14:27:00,837 - DEBUG - urllib3.connectionpool(19844) - https://statsapi.mlb.com:443 "GET /api/v1/teams/143?hydrate=previousSchedule&fields=teams,id,teamName,previousGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name HTTP/1.1" 200 None
2020-12-22 14:27:00,843 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/{ver}/schedule
2020-12-22 14:27:00,843 - DEBUG - statsapi(19844) - Found query param: sportId
2020-12-22 14:27:00,845 - DEBUG - statsapi(19844) - Found query param: gamePk
2020-12-22 14:27:00,846 - DEBUG - statsapi(19844) - Found query param: hydrate
2020-12-22 14:27:00,847 - DEBUG - statsapi(19844) - Found query param: fields
2020-12-22 14:27:00,848 - DEBUG - statsapi(19844) - path_params: {}
2020-12-22 14:27:00,849 - DEBUG - statsapi(19844) - query_params: {'sportId': '1', 'gamePk': '630896', 'hydrate': 'game(content(highlights(highlights)))', 'fields': 'dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url'}
2020-12-22 14:27:00,850 - DEBUG - statsapi(19844) - Replacing {ver} with default: v1.
2020-12-22 14:27:00,850 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/schedule
2020-12-22 14:27:00,851 - DEBUG - statsapi(19844) - Adding query parameter sportId=1
2020-12-22 14:27:00,852 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/schedule?sportId=1
2020-12-22 14:27:00,853 - DEBUG - statsapi(19844) - Adding query parameter gamePk=630896
2020-12-22 14:27:00,854 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/schedule?sportId=1&gamePk=630896
2020-12-22 14:27:00,857 - DEBUG - statsapi(19844) - Adding query parameter hydrate=game(content(highlights(highlights)))
2020-12-22 14:27:00,858 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/schedule?sportId=1&gamePk=630896&hydrate=game(content(highlights(highlights)))
2020-12-22 14:27:00,859 - DEBUG - statsapi(19844) - Adding query parameter fields=dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url
2020-12-22 14:27:00,860 - DEBUG - statsapi(19844) - URL: https://statsapi.mlb.com/api/v1/schedule?sportId=1&gamePk=630896&hydrate=game(content(highlights(highlights)))&fields=dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url
2020-12-22 14:27:00,864 - DEBUG - urllib3.connectionpool(19844) - Starting new HTTPS connection (1): statsapi.mlb.com:443
2020-12-22 14:27:01,076 - DEBUG - urllib3.connectionpool(19844) - https://statsapi.mlb.com:443 "GET /api/v1/schedule?sportId=1&gamePk=630896&hydrate=game(content(highlights(highlights)))&fields=dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url HTTP/1.1" 200 None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\statsapi\__init__.py", line 970, in game_highlights
sortedHighlights = game_highlight_data(gamePk)
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\statsapi\__init__.py", line 1008, in game_highlight_data
r["dates"][0]["games"][0]["content"]["highlights"]["highlights"]["items"]
KeyError: 'highlights'
from mlb-statsapi.
Thanks for sharing how to print out the logs for debugging, very helpful!
from mlb-statsapi.
Related Issues (20)
- team stats HOT 1
- Missing documentation? HOT 2
- Standings_data division parameter should accept divisionId instead of only abbreviation HOT 5
- Past 7 Day stats for player id HOT 1
- Get request for 'team_stats' returning 404 HOT 1
- Return line score when using schedule end point HOT 2
- dataclass() got an unexpected keyword argument 'kw_only' HOT 2
- TypeError: Teamrecords.__init__() got an unexpected keyword argument 'eliminationnumbersport' HOT 2
- Determining a list of teams that a player played on HOT 1
- SSL: SSLV3_ALERT_HANDSHAKE_FAILURE HOT 1
- game_contextMetrics 404 Not Found for url HOT 6
- Unexpected behavior in lookup_player() when season is not specified HOT 1
- Incorrect seasons endpoint documentation HOT 2
- boxscore_data function returns incorrect and incomplete batting notes. HOT 2
- Player Stats Error HOT 10
- Typos HOT 5
- Get Team Mark HOT 2
- Extra Games in Schedule HOT 2
- Normalize diacritics in player lookup HOT 6
- The divisions endpoint allows 'season' as an optional endpoint.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mlb-statsapi.