toddrob99 / MLB-StatsAPI

Python wrapper for MLB Stats API
GNU General Public License v3.0
539 stars 100 forks source link

game_contextMetrics 404 Not Found for url #125

Closed jeffabc1997 closed 1 year ago

jeffabc1997 commented 1 year ago

Hi, I was trying to use endpoint game_contextMetrics to get awayWinProbability and homeWinProbability and also probable starting pitchers. This is my code.

date = "09/05/2023"
params = {
    "sportId": 1,
    "date": date,
    "hydrate": "probablePitcher(note)"
}
schedule = statsapi.get("schedule", params)
game = schedule["dates"][0]["games"][1]
contextMetrics = statsapi.get("game_contextMetrics", {"gamePk": game["gamePk"]})
print(contextMetrics)

However, it seems that we can't find the URL. HTTPError: 404 Client Error: Not Found for url: https://statsapi.mlb.com/api/v1/game/716714/contextMetrics When I tried date = "09/04/2023" (yesterday), the result is what I expected. Did I miss something? I think i can still get pitchers by using endpoint schedule.

date = "09/05/2023"
params = {
    "sportId": 1,
    "date": date,
    "hydrate": "probablePitcher(note)" # works fine
}
schedule = statsapi.get("schedule", params)

I'm novice to statsapi. thanks!

jeffabc1997 commented 1 year ago

I want to submit the above comment 8 hours ago (but Github didn't let me ) and at that time game_contextMetrics couldn't work. It works now. I would like to know why...?

toddrob99 commented 1 year ago

I think the contextMetrics endpoint only works while the game is live. Does that line up with what you're experiencing?

jeffabc1997 commented 1 year ago

Yes, I think so. the contextMetrics will work some hours before the game starts, and it's initial value is 50 vs 50. I think I should add some try and except block to prevent my program raising error?

toddrob99 commented 1 year ago

Yes, and you can check the game status and only query the second endpoint when the game is in warmup or in progress statuses. After the game ends the probability will be 100/0 so there's really no reason to look it up.

jeffabc1997 commented 1 year ago

This is really helpful! Thanks!!

toddrob99 commented 1 year ago

I'm going to close, but feel free to comment if you need anything else.