Skip to main content

A Python 3 wrapper for the MLB StatsAPI at statsapi.mlb.com

Project description

mlbapi

PyPI version Python versions License Stars

mlbapi is a Python library that provides Pythonic bindings for MLB Advanced Media's StatsAPI — the same data source that powers MLB.com's live game data, box scores, standings, and more.

Unlike raw API calls, mlbapi returns structured Python objects so you can work with MLB data naturally in your code.


Features

  • Schedule lookups by team and date range
  • Box score data including batting and pitching stats
  • Live linescore data (inning, batter, pitcher, count)
  • Structured Python objects from every endpoint — no manual JSON parsing
  • Lightweight: only requires requests

Installation

pip install mlbapi

Quick Start

Get a team's schedule for a date range

import mlbapi

# Houston Astros schedule for the 2024 season
schedule = mlbapi.schedule(start_date='04/01/2024', end_date='10/01/2024', team_id=117)

Get a team's schedule for a single date

import mlbapi

schedule = mlbapi.schedule(date='04/01/2024', team_id=117)

Get box score data for a game

import mlbapi

schedule = mlbapi.schedule(date='04/01/2024', team_id=117)
game_pk = schedule.dates[0].games[0].game_pk
boxscore = mlbapi.boxscore(game_pk)

# Print all game info (weather, attendance, venue, etc.)
for info in boxscore.info:
    print(info.info)

# Print batting stats for both teams
print(boxscore.teams.away.team_stats.batting.__dict__)
print(boxscore.teams.home.team_stats.batting.__dict__)

Get live linescore data

import mlbapi

schedule = mlbapi.schedule(date='04/01/2024', team_id=117)
game_pk = schedule.dates[0].games[0].game_pk
line = mlbapi.linescore(game_pk)

output = '{} of the {}, {} facing {}. {} ball(s), {} strike(s), {} out(s)'
print(output.format(
    line.inning_half,
    line.current_inning_ordinal,
    line.offense.batter.full_name,
    line.defense.pitcher.full_name,
    line.balls,
    line.strikes,
    line.outs
))
# Top of the 9th, Scott Van Slyke facing Framber Valdez. 0 ball(s), 3 strike(s), 3 out(s)

Team IDs

Common MLB team IDs for reference:

Team ID Team ID
Yankees 147 Dodgers 119
Red Sox 111 Giants 137
Astros 117 Cubs 112
Braves 144 Cardinals 138
Mets 121 Padres 135

A full list is available via mlbapi.teams().


Documentation

Full documentation and endpoint reference coming soon. In the meantime, feel free to open an issue with questions.


Contributing

Contributions are welcome! Please read CONTRIBUTING.md before submitting a pull request. Bug reports and feature requests can be filed as GitHub Issues.


Support the Project

If mlbapi saves you time, consider supporting its development:


License

This project is licensed under the terms found in LICENSE.


Disclaimer

This library is not affiliated with or endorsed by Major League Baseball or MLB Advanced Media. Use of the MLB StatsAPI is subject to MLB's terms of service.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlbapi-0.2.2.tar.gz (37.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlbapi-0.2.2-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file mlbapi-0.2.2.tar.gz.

File metadata

  • Download URL: mlbapi-0.2.2.tar.gz
  • Upload date:
  • Size: 37.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mlbapi-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2e7feceb5682d650ed963ffd2fc79e8dddd1ddf4929e33a6c3f6760fff72da2a
MD5 f224dddf3c3383645cee14a91d788613
BLAKE2b-256 db0783737ea2f792810763f7d45aa052450a50b2734e5a155647696d3aa486f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlbapi-0.2.2.tar.gz:

Publisher: publish-to-pypi.yml on trevor-viljoen/mlbapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlbapi-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: mlbapi-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 39.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mlbapi-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0e7b53d1421c59cd22e7a3d3112fcb734c274d6c50dcd9cb898d439645dbc0c4
MD5 60af23dd0d71fc912ef58af1c347657b
BLAKE2b-256 b33cd6d872aae49e5c8c9787f17438f38b8c2f7be74dcb163e1303aaa106feb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlbapi-0.2.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on trevor-viljoen/mlbapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page