Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get minor league stats? #87

Closed
ForceConstant opened this issue Aug 9, 2022 · 3 comments
Closed

How to get minor league stats? #87

ForceConstant opened this issue Aug 9, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request question Question about usage/support

Comments

@ForceConstant
Copy link

Seems that player_stat_data() only returns major league stats, how can I get current season stats for a minor leaguer?

@toddrob99
Copy link
Owner

Using player_stat_data() you can currently only get MLB stats. You can get MiLB stats using the MLB-StatsAPI module, but you will have to recreate what player_stat_data() is doing with a small modification to add sportId to the hydrate parameter. You will need to provide a sportId (see: https://statsapi.mlb.com/api/v1/sports), e.g. 11 for AAA, 12 for AA, 13 for A+.

def player_stat_data_modified(personId, group="[hitting,pitching,fielding]", type="season", sportId=1):
    """Returns a list of current season or career stat data for a given player."""
    params = {
        "personId": personId,
        "hydrate": "stats(group=" + group + ",type=" + type + ",sportId=" + str(sportId) + "),currentTeam",
    }
    r = statsapi.get("person", params)

    stat_groups = []

    player = {
        "id": r["people"][0]["id"],
        "first_name": r["people"][0]["useName"],
        "last_name": r["people"][0]["lastName"],
        "active": r["people"][0]["active"],
        "current_team": r["people"][0]["currentTeam"]["name"],
        "position": r["people"][0]["primaryPosition"]["abbreviation"],
        "nickname": r["people"][0].get("nickName"),
        "last_played": r["people"][0].get("lastPlayedDate"),
        "mlb_debut": r["people"][0].get("mlbDebutDate"),
        "bat_side": r["people"][0]["batSide"]["description"],
        "pitch_hand": r["people"][0]["pitchHand"]["description"],
    }

    for s in r["people"][0].get("stats", []):
        for i in range(0, len(s["splits"])):
            stat_group = {
                "type": s["type"]["displayName"],
                "group": s["group"]["displayName"],
                "season": s["splits"][i].get("season"),
                "stats": s["splits"][i]["stat"],
            }
            stat_groups.append(stat_group)

    player.update({"stats": stat_groups})

    return player

personId = 691725  # Andrew Painter
sportId = 13  # A+
painter = player_stat_data_modified(691725, sportId=13)

I will add the sportId parameter (with default value of 1) to player_stat_data() in the next version, at which point you will be able to do statsapi.player_stat_data(691725, sportId=13).

@toddrob99 toddrob99 self-assigned this Aug 9, 2022
@toddrob99 toddrob99 added enhancement New feature or request question Question about usage/support labels Aug 9, 2022
@toddrob99 toddrob99 mentioned this issue Aug 10, 2022
Merged
@toddrob99
Copy link
Owner

v1.5 is available on pypi now (pip install --upgrade mlb-statsapi)

@ForceConstant
Copy link
Author

Great thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Question about usage/support
Projects
None yet
Development

No branches or pull requests

2 participants