toddrob99/MLB-StatsAPI

game_contextMetrics 404 Not Found for url

jeffabc1997 opened this issue · 6 comments

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!

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...?

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

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?

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.

This is really helpful! Thanks!!

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