Git Product home page Git Product logo

Comments (2)

toddrob99 avatar toddrob99 commented on July 26, 2024 1

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.

Yurockkk avatar Yurockkk commented on July 26, 2024

Thanks for sharing how to print out the logs for debugging, very helpful!

from mlb-statsapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.