toddrob99 / MLB-StatsAPI

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

Cleaner endpoint for stats ranked by team #78

Closed rs7q5 closed 2 years ago

rs7q5 commented 2 years ago

I know you can get the rank of the mlb teams based on certain stats but is there any way to get an easier endpoint so it acts more like the league leaders but for teams?

For example I want the ranking of the teams with the most home runs and would want the number and the team along with rank.

Or if there is any tips on getting some of these stats cleanly or what endpoint to use, would appreciate some tips!

toddrob99 commented 2 years ago

The teams_stats endpoint does that...

params = {
    "season": 2022,
    "stats": "season",
    "group": "hitting",
    "sortStat": "homeRuns",
    "order": "desc",
    "gameType": "R",
    "sportIds": 1,
}
team_homerun_leaders = statsapi.get("teams_stats", params)

Gets data from: https://statsapi.mlb.com/api/v1/teams/stats?season=2022&stats=season&group=hitting&sortStat=homeRuns&order=desc&gameType=R&sportIds=1. This is regular season homerun rankings for the 2022 season.

rs7q5 commented 2 years ago

Yeah I was hoping though I could simply specify the sortStat and not the group or is there a meta endpoint that tells you which stats show up in each statType? A lot of those endpoints do seem to be empty.

If I knew what statType to use for a parameter in baseballStats, that would be fine, but the baseballStats seems to only have the statGroups.

If there isn't I can just do it the old school way and iterate through the statTypes and see what's what, just wasn't sure if there was a more convenient way.

toddrob99 commented 2 years ago

You can leave out group, but then you'll get leaders for both hitting and pitching.

I am not following what you are saying about baseballStats only having the statGroups... You will get all of the stats but only the one specified in sortStat is used to rank the players. If you only want homeruns to be listed in the stats, you can use the fields parameters to pare down what gets returned.

rs7q5 commented 2 years ago

The baseballStats endpoint lists the stat as hitting, pitching etc. I was hoping there may of been one for which statType each metric was in.

So would it just be fields=homeRuns? I would still have to specify stat though and I guess then know that the stat I want is in fact in the stats returned.

I guess my thing was I was trying to avoid manually having to go through each statType option (season, seasonAdvanced, etc.) to figure out which parameters are returned for each.

I am sorting teams by some user selected metric and not each player of a team or the team league wide. So I believe I have to specify stat and the group (which I can specify multiple groups).