toddrob99 / MLB-StatsAPI

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

The roster for team #142

Closed WillMusing closed 1 month ago

WillMusing commented 3 months ago

I am looking for the team roster information and found the statsapi.roster() is very helpful. However, it returns a string which I have to parse and might have some level of errors i.e. if one has middle name, etc. Would it be better to return a list - it seems having the list format in the string.

In addition, it would be really great to also return the player Id as well.

Thank you for creating this statsapi!

toddrob99 commented 3 months ago

You can fetch the unformatted data the same way statsapi.roster() does, using statsapi.get("team_roster", <params>). Here is the function in the source code.

WillMusing commented 3 months ago

Thank you so much @toddrob99. I much appreciated your response!

So the exercise I am working on is to find the team Id / team name based on the pitcher Id (from every pitch) to analyze the pitcher performance. Since I can't find any function to get team Id from pitcher Id, the path I am going is to use the pitcher Id from a specific game -> pitcher first name, last name -> look up the name from team roster of all time (lots of data to aggregate) -> find the team(s) has this pitch name or pitcher Id. Is there any better way to do this?

toddrob99 commented 3 months ago

Where are you getting the pitch data from? If it's from the game endpoint, the team information is in there too.

If you truly have pitch data with a pitcher id but no team info, you can look up the player from the pitcher id using the person endpoint and statsapi.get().

If I were analyzing all pitches from all games in a given season, I would start with the schedule endpoint to get a list of gamePks, then loop through the game endpoint for each gamePk and collect the team, pitcher, and pitch data from there.

WillMusing commented 3 months ago

Thanks again @toddrob99. I tried the team_roster but couldn't get the right data yet. Still working on it. For example,

roster_list = statsapi.get("team_roster", params= { "teamId": 4104, "rosterType": "fullRoster", "season" : 2018}) It returns without the actual roster. I am not sure if I am using it the correct way:

{'copyright': 'Copyright 2024 MLB Advanced Media, L.P.  Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt',
 'link': '/api/v1/teams/4104/roster',
 'teamId': 4104,
 'rosterType': 'fullSeason'}
WillMusing commented 3 months ago

By the way, for your question about pitch data, I got it from https://github.com/jldbc/pybaseball. It contains detail pitching data around every pitch since 2018. I thought analyzing the pitching style/speed by team over time could be valuable information.

This pybaseball data does have gamePk.

toddrob99 commented 3 months ago

It seems like the API doesn't have roster data for minor league teams, because if I change the teamId to 143 (Philadelphia Phillies) it does return the roster data.

https://statsapi.mlb.com/api/v1/teams/4104/roster?rosterType=fullRoster&season=2018 https://statsapi.mlb.com/api/v1/teams/143/roster?rosterType=fullRoster&season=2018