toddrob99 / MLB-StatsAPI

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

Question about getting gamePk for past games / how to get game by game stats for each player #79

Closed corey-Robinson1337 closed 2 years ago

corey-Robinson1337 commented 2 years ago

Hey I was wondering what a good approach would be to get the past game PK's for each game of this season. I'm trying to add each players stats for each games so that that I can calculate the variance of some stats so aggregated stats won't be sufficient.

I think this endpoint will get me what I need --

https://statsapi.mlb.com/api/{ver}/people/{personId}/stats/game/{gamePk}

Unfortunately, I'm not sure how I can provide the gamePk for each game of this mlb season.

What would be a good approach for me to get these gamePk's so that I can accomplish this?

toddrob99 commented 2 years ago

Use the schedule endpoint. You can get all games for the 2022 season like this: statsapi.get("schedule", {"sportId":1, "startDate":"01/01/2022", "endDate": "12/31/2022", "fields": "dates,date,games,gamePk,gameType,teams,away,home,team,id"}). Leave out the fields param to see all available fields. Corresponding URL: https://statsapi.mlb.com/api/v1/schedule?sportId=1&startDate=01/01/2022&endDate=12/31/2022&fields=dates,date,games,gamePk,gameType,teams,away,home,team,id.

corey-Robinson1337 commented 2 years ago

Alright I will do just that! Thank you for the prompt response!!

corey-Robinson1337 commented 2 years ago

Sorry to be a bother, but where I can I find the valid values for the field param?

I would like to get the start times of the games too if that's possible!

corey-Robinson1337 commented 2 years ago

never mind I just dug into the code to figure it out!

toddrob99 commented 2 years ago

You can just leave out the fields parameter altogether to get all fields returned (maybe do that with a smaller date range), and then add whatever fields you want from the full response. Keep in mind you will need to include any fields that are above the one you want; for example, if you want the detailedState field, you have to also include it's parent, status.