toddrob99 / MLB-StatsAPI

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

Season/Career WAR #69

Closed WRB1021 closed 2 years ago

WRB1021 commented 2 years ago

Hi there. Not an issue, just a question. I'm fairly new to python and to this API but I'm having a bit of trouble pulling in the career WAR / full season WAR for a given player. I know WAR is a field in the meta, but for some reason when I pull career stats for a given player with the 'player_stats' command I'm not seeing WAR in the output. Any help would be appreciated!

toddrob99 commented 2 years ago

I don't think WAR is a stat that MLB publishes in the public API. Perhaps it's available in the StatCast endpoints that require authentication (which is only provided to affiliates). Please refer to this reddit post where basically the same question was asked and I provided a more detailed response.

WRB1021 commented 2 years ago

Got it, thanks a ton!

toddrob99 commented 2 years ago

I took another look, and figured it out!

params = {
    "personId": 547180,  # Bryce Harper
    "season": 2021,
    "hydrate": "stats(group=[batting],type=[sabermetrics],season=2021)",
}
statsapi.get("person", params)

https://statsapi.mlb.com/api/v1/people/547180?hydrate=stats(group=[batting],type=[sabermetrics],season=2021

You should be able to use the same hydration on the people endpoint with multiple personIds.

WRB1021 commented 2 years ago

Oh excellent thanks so much Todd!

toddrob99 commented 2 years ago

That was a single player, single season, but this appears to give all hitters for a given season: https://statsapi.mlb.com/api/v1/stats?stats=sabermetrics&group=hitting&sportId=1&season=2021

statsapi.get("stats", {"stats": "sabermetrics", "group": "hitting", "season": 2021, "sportId": 1})