A Python API to retrieve and read MLB GameDay data

Overview

mlbgame

Build Status Code Climate Coverage Status

Join Slack

mlbgame is a Python API to retrieve and read MLB GameDay data. mlbgame works with real time data, getting information as games are being played.

mlbgame uses the same data that MLB GameDay uses, and therefore is updated as soon as something happens in a game.

mlbgame documentation

mlbgame on Github (Source Code)

If you have a question or need help, the quickest way to get a response is to file an issue on the Github issue tracker

mlbgame's submodules should not really be used other than as used by the main functions of the package (in __init__.py).

Use of mlbgame must follow the terms stated in the license and on mlb.com.

Installation

mlbgame is in the Python Package Index (PyPI). Installing with pip is recommended for all systems.

mlbgame can be installed by running:

pip install mlbgame

Alternatively, the latest release of mlbgame can be downloaded as a zip or tarball. If you do not install with pip, you must also install lxml as specified in setup.py.

If you want to help develop mlbgame, you must also install the dev dependencies, which can be done by running pip install -e .[dev] from within the directory.

Examples

Here is a quick teaser to find the scores of all home Mets games for the month of June, 2015:

from __future__ import print_function
import mlbgame

month = mlbgame.games(2015, 6, home='Mets')
games = mlbgame.combine_games(month)
for game in games:
    print(game)

And the output is:

Giants (5) at Mets (0)
Giants (8) at Mets (5)
Giants (4) at Mets (5)
Braves (3) at Mets (5)
Braves (5) at Mets (3)
Braves (8) at Mets (10)
Blue Jays (3) at Mets (4)
Blue Jays (2) at Mets (3)
Reds (1) at Mets (2)
Reds (1) at Mets (2)
Reds (1) at Mets (2)
Reds (2) at Mets (7)
Cubs (1) at Mets (0)

Maybe you want to know the pitchers for the Royals game on April 30th, 2015:

from __future__ import print_function
import mlbgame

day = mlbgame.day(2015, 4, 12, home='Royals', away='Royals')
game = day[0]
output = 'Winning pitcher: %s (%s) - Losing Pitcher: %s (%s)'
print(output % (game.w_pitcher, game.w_team, game.l_pitcher, game.l_team))

And the output is:

Winning pitcher: Y. Ventura (Royals) - Losing Pitcher: C. Wilson (Angels)

You can easily print a list of the Mets batters in the final game of the 2015 World Series:

from __future__ import print_function
import mlbgame

game = mlbgame.day(2015, 11, 1, home='Mets')[0]
stats = mlbgame.player_stats(game.game_id)
for player in stats.home_batting:
    print(player)

And the output is:

Curtis Granderson (RF)
David Wright (3B)
Daniel Murphy (2B)
Yoenis Cespedes (CF)
Juan Lagares (CF)
Lucas Duda (1B)
Travis d'Arnaud (C)
Michael Conforto (LF)
Wilmer Flores (SS)
Matt Harvey (P)
Jeurys Familia (P)
Kelly Johnson (PH)
Jonathon Niese (P)
Addison Reed (P)
Bartolo Colon (P)
Comments
  • AttributeError: 'Overview' object has no attribute 'home_probable_pitcher_s_era'

    AttributeError: 'Overview' object has no attribute 'home_probable_pitcher_s_era'

    for game in games:
            stats = mlbgame.team_stats(game.game_id)
            overview = mlbgame.overview(game.game_id)
            player_stats = mlbgame.player_stats(game.game_id)
            winning_team_SP_era = overview.home_probable_pitcher_s_era
       
    
    question 
    opened by Hisairnessag3 11
  • wrote code to unzip .gz files on windows systems

    wrote code to unzip .gz files on windows systems

    I added a few lines of code for this to work on Windows systems. There is probably a better way to do this like using gzip to read the xml and using elementree to parse the string, but this was more straightforward.

    opened by russodanielp 9
  • XML Error pulling player stats

    XML Error pulling player stats

    The xml bug described in #121 appears to be fixed for the game overview but is still present for player stats. I've built from master and get the following result when trying to pull player stats from a completed game:

    >>> games = mlb.combine_games( mlb.games(2019, 3, 29))
    >>> print(games[0])
    Tigers (0) at Blue Jays (6)
    >>> mlb.player_stats(games[0].game_id)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/ec2-user/mlbgame/mlbgame/__init__.py", line 214, in player_stats
        data = mlbgame.stats.player_stats(game_id)
      File "/home/ec2-user/mlbgame/mlbgame/stats.py", line 75, in player_stats
        raw_box_score_tree = etree.parse(raw_box_score).getroot()
      File "src/lxml/etree.pyx", line 3435, in lxml.etree.parse
      File "src/lxml/parser.pxi", line 1861, in lxml.etree._parseDocument
      File "src/lxml/parser.pxi", line 1881, in lxml.etree._parseFilelikeDocument
      File "src/lxml/parser.pxi", line 1776, in lxml.etree._parseDocFromFilelike
      File "src/lxml/parser.pxi", line 1187, in lxml.etree._BaseParser._parseDocFromFilelike
      File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
      File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult
      File "src/lxml/parser.pxi", line 640, in lxml.etree._raiseParseError
      File "http://gd2.mlb.com/components/game/mlb/year_2019/month_03/day_29/gid_2019_03_29_detmlb_tormlb_1/rawboxscore.xml", line 1
    lxml.etree.XMLSyntaxError: Space required after the Public Identifier, line 1, column 55
    >>> 
    
    bug help wanted 
    opened by jacobmh1177 8
  • Error when viewing overview for spring training games

    Error when viewing overview for spring training games

    I'm getting an error when trying to view the scoreboard for spring training games. Here is the error:

    >>> mlbgame.overview('2019_03_10_chnmlb_milmlb_1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.7/site-packages/mlbgame/__init__.py", line 198, in overview
        return mlbgame.game.Overview(mlbgame.game.overview(game_id))
      File "/usr/local/lib/python3.7/site-packages/mlbgame/game.py", line 328, in overview
        raw_box_score_root = etree.parse(raw_box_score).getroot()
      File "src/lxml/etree.pyx", line 3435, in lxml.etree.parse
      File "src/lxml/parser.pxi", line 1861, in lxml.etree._parseDocument
      File "src/lxml/parser.pxi", line 1881, in lxml.etree._parseFilelikeDocument
      File "src/lxml/parser.pxi", line 1776, in lxml.etree._parseDocFromFilelike
      File "src/lxml/parser.pxi", line 1187, in lxml.etree._BaseParser._parseDocFromFilelike
      File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
      File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult
      File "src/lxml/parser.pxi", line 640, in lxml.etree._raiseParseError
      File "http://gd2.mlb.com/components/game/mlb/year_2019/month_03/day_10/gid_2019_03_10_chnmlb_milmlb_1/rawboxscore.xml", line 1
    lxml.etree.XMLSyntaxError: Space required after the Public Identifier, line 1, column 55
    

    Is this due to MLB not posting scoreboard details for spring training games or is this an issue with the library?

    bug duplicate 
    opened by KYDronePilot 8
  • Breaking change to gameday API?

    Breaking change to gameday API?

    Have a script that grabs info daily and today got an exception:

    import mlbgame
    games = mlbgame.games(2018,5,29)
    print games
    games = [g for item in games for g in item]
    for game in games:
        print game.game_id
        print mlbgame.overview(game.game_id).time
    
    

    gameid 2018_05_29_chamlb_clemlb_1 ValueError: Could not find a game with that id.

    API: http://gd2.mlb.com/components/game/mlb/year_2018/month_05/day_29/gid_2018_05_29_chamlb_clemlb_1/rawboxscore.xml

    question 
    opened by Hisairnessag3 8
  • ValueError: Could not find a game with that id.

    ValueError: Could not find a game with that id.

    Whats causing this? Just started happening.

    code:

    import mlbgame stats = mlbgame.player_stats('2014_06_07_miamlb_chnmlb_1')

    for player in stats.home_batting: print(player.h)

    bug 
    opened by Hisairnessag3 8
  • player_stats not handling some spring training games

    player_stats not handling some spring training games

    Occasionally MLB teams play non-MLB teams in exhibition. Two of these games took place on April 4, 2016. combine_games handles this okay, simply ignoring the non-MLB match-ups. See the April 4 scoreboard:

    Nationals (4) at Braves (3) Giants (12) at Brewers (3) Mariners (2) at Rangers (3) White Sox (4) at Athletics (3) Rockies (10) at D-backs (5) Cubs (9) at Angels (0) Blue Jays (5) at Rays (3) Red Sox (0) at Indians (0) Phillies (2) at Reds (6) Twins (2) at Orioles (3) Dodgers (15) at Padres (0) Astros (0) at Yankees (0)

    However, player_stats does not handle these well. Below is the error message I receive when I try to run mlbgame.player_stats(game.game_id) on one of those games, for instance the Miami Marlins vs. the Diablos Rojos:

    Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mlbgame/data.py", line 47, in get_box_score data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/gid_%s/boxscore.xml" % (year, month, day, game_id)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 162, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 471, in open response = meth(req, response) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 581, in http_response 'http', request, response, code, msg, hdrs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 509, in error return self._call_chain(_args) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 443, in _call_chain result = func(_args) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 589, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mlbgame/init.py", line 238, in player_stats data = mlbgame.stats.player_stats(game_id) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mlbgame/stats.py", line 11, in player_stats data = mlbgame.data.get_box_score(game_id) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mlbgame/data.py", line 49, in get_box_score raise ValueError("Could not find a game with that id.") ValueError: Could not find a game with that id.

    bug 
    opened by rationalpastime 8
  • Home/Away Probable Pitchers Reversed

    Home/Away Probable Pitchers Reversed

    I'm grabbing the probable pitchers for 3/28/2019 and I noticed on the Reds/Pirates game it has Jamison Taillon as the home pitcher & Luis Castillo as the away pitcher when the game is being played in Cincinnati.

    bug 
    opened by mjsimpson24 7
  • Added Innings All File to access more detail on pitches, including PI…

    Added Innings All File to access more detail on pitches, including PI…

    …TCHf/x data - 04/05/18 9:53 PM

    Example Source URL: http://gd2.mlb.com/components/game/mlb/year_2017/month_04/day_03/gid_2017_04_03_seamlb_houmlb_1/inning/inning_all.xml

    opened by frankmowen 7
  • Season stats not found

    Season stats not found

    So I have a simple loop looking for stats like so:

    def test_stats(games):
        for game in games:
            stats = mlbgame.team_stats(game.game_id)
            hr_home = stats.home_batting.hr
            hr_away = stats.away_batting.hr
            box_score = mlbgame.box_score(game.game_id)
            error_home = box_score.print_scoreboard()
            pitch_home = stats.home_pitching.so
            pi_home = stats.home_pitching.s_h
            print hr_away,hr_home,pitch_home,pi_home
    

    when I run this, it is giving me: AttributeError: 'TeamStats' object has no attribute 's_h'

    Is this another break?

    bug 
    opened by Hisairnessag3 7
  • Away batting?

    Away batting?

    Hi. I just came across this fantastic piece of work today. Thanks so much. I'm fairly new to Python, but I can't figure out what I'm doing wrong. This piece of code:

    stats = mlbgame.player_stats(game.game_id)
       for player in stats.home_batting:
        ... stuff here on player
    

    works fine But this code

    stats = mlbgame.player_stats(game.game_id)
        for player in stats.away_batting:
         ... stuff here on player
    

    Never executes because away_batting is always empty. I presume I'm doing something wrong, but if I'm not....

    bug 
    opened by JonathanSFalk 7
  • Initial setup to access MLB GD2 Database: HTTP Forbidden

    Initial setup to access MLB GD2 Database: HTTP Forbidden

    Hopefully this isn't a stupid question, but: Are there additional setup steps needed to gain access to the MLB database beyond the module install? Accounts, authentication, etc. When the module wouldn't return any data, I tracked this back to an HTTP Forbidden error when accessing MLB's GD2 link. Same thing when I tested that link in browser - Access Denied. Am I missing something to set up this API? If there are additional steps to authenticate for database access, can this be clarified in the documentation? Thanks!

    opened by Ctru14 0
  • ValueError: Could not find a game with that id still occurring -- worked yesterday

    ValueError: Could not find a game with that id still occurring -- worked yesterday

    Just getting started with this API, and this morning am seeing the same game id error still occuring. This happens with running the base example presented in the documentation. Anyone have a fix?

    `#!python from future import print_function import mlbgame

    game = mlbgame.day(2015, 11, 1, home='Mets')[0] stats = mlbgame.player_stats(game.game_id) for player in stats.home_batting: print(player)`

    opened by bennettcohen 1
  • Getting game events for game with unplayed bottom of final inning causes IndexError

    Getting game events for game with unplayed bottom of final inning causes IndexError

    Version: 2.5.0

    Steps to reproduce:

    1. Choose a game in which the bottom of the 9th inning was not played. For example, game_id 2021_04_03_tormlb_nyamlb_1, the Yankees v Blue Jays game from April 3rd, 2021.
    2. Run mlbgame.events.game_events(id) with the selected game_id value.

    Result:

    line 48, in game_events
      'bottom': __inning_info(x, 'bottom')
    line 17, in __inning_info
      half = inning.findall(part)[0]
    IndexError: list index out of range
    

    Edit: this appears to be a duplicate of #137, still open from 2 years ago.

    opened by caseyjohnsonwv 0
Releases(v2.5.0)
  • v2.5.0(Apr 16, 2018)

    New Features:

    • Add probable starter attributes (#64, fixes #32)
    • Add more attributes to overview (#71, #70, fixes #68)
    • Add important dates feature (#85, fixes #84)

    Bug Fixes:

    • Updated README and documentation to show correct output from examples (#75)
    • Fix incorrect date for scoreboard function (#78, fixes #77)
    Source code(tar.gz)
    Source code(zip)
  • v2.4.2(Feb 6, 2018)

    • Remove unneeded import requests statement, thereby removing requests as an accidental and unlisted dependency
    • Better stats retrieval through rawboxscore.xml (#58, fixes #30)
    • Fix bug which removed away team stats (#58, fixes #57)
    • Fix failing tests
    Source code(tar.gz)
    Source code(zip)
  • v2.4.1(Jan 17, 2018)

  • v2.4.0(Sep 8, 2017)

    New Features

    • Add ability to get player, coach, and umpire data for game (#43)
    • Add ability to get current roster data (#34, #27)
    • Add ability to get current injury data (#36)
    • Add ability to get current and historical standings data (#33, #31)

    Deprecations:

    • Removed ability to locally cache XML data

    Developers:

    • Added test cases with 97% coverage
      • Added Travis CI and Coveralls
    • Added code climate
    • Added new dev install requirements
    • Refactored existing code to reduce duplication
    • Added Slack for developers to join
    Source code(tar.gz)
    Source code(zip)
  • v2.3.3(May 13, 2017)

  • v2.3.2(Apr 18, 2017)

    • Fixes a bug where .gz files would not work automatically on Windows machines (#24 fixed in #28, thanks @russodanielp)
    • Fixes a bug where game type would not accurately be reported (name changed from game_type to game_tag)
    • Internal refactoring and miscellaneous documentation changes
    Source code(tar.gz)
    Source code(zip)
  • v2.3.1(Nov 23, 2016)

  • v2.3.0(Nov 9, 2016)

    New Features

    • mlbgame.day and mlbgame.games now work for games in the future (#16)
    • Overall league information
    • Individual team information (#17)
    • --clear parameter of the update script will remove all cached data
    • Data for the 2016 season is now cached by default (previously was through 2015 season)

    Bug Fixes

    • Update now works correctly and more efficiently (#18)
    • Game code has been refactored to work better
    Source code(tar.gz)
    Source code(zip)
  • v2.2.1(Jul 30, 2016)

  • v2.2.0(Jul 19, 2016)

    New Features

    • Get game overview information (such as venue, thanks to @rationalpastime for the suggestion)
    • Ability to cache this overview information
    • Update now has the option of what extra data to update (-m/--more is no longer an option, please check new documentation)

    Bug Fixes

    • Fix bug where s_hr was reported incorrectly in statmap
    • Fix bug where updating events would corrupt both local event data and local stats data
    Source code(tar.gz)
    Source code(zip)
  • v2.1.0(Jul 5, 2016)

    Adds support to get event data for certain games. This event data contains information about every at bat and pitch.

    Bug fixes/changes:

    • Remove batter_stats and pitcher_stats from statmap.py
    • Import statements now try python 3 as default
    • Statmap data has been updated
    Source code(tar.gz)
    Source code(zip)
  • v2.0.2(Apr 7, 2016)

  • v2.0.1(Apr 1, 2016)

  • v2.0.0(Mar 25, 2016)

  • v1.0.3(Feb 5, 2016)

  • v1.0.2(Jan 30, 2016)

  • v1.0.1(Jan 27, 2016)

  • v1.0.0(Jan 27, 2016)

  • v0.4.0(Dec 18, 2015)

  • v0.3.1(Dec 7, 2015)

  • v0.2.2(Dec 1, 2015)

  • v0.2.1(Nov 27, 2015)

  • v0.2.0(Nov 26, 2015)

  • v0.1.2(Nov 21, 2015)

  • v0.1.1(Nov 19, 2015)

Owner
Zach Panzarino
Software Engineer and Entrepreneur
Zach Panzarino
IMDbPY is a Python package useful to retrieve and manage the data of the IMDb movie database about movies, people, characters and companies

IMDbPY is a Python package for retrieving and managing the data of the IMDb movie database about movies, people and companies. Revamp notice Starting

Davide Alberani 1.1k Jan 2, 2023
API to retrieve the number of grades on the OGE website (Website listing the grades of students) to know if a new grade is available. If a new grade has been entered, the program sends a notification e-mail with the subject.

OGE-ESIREM-API Introduction API to retrieve the number of grades on the OGE website (Website listing the grades of students) to know if a new grade is

Benjamin Milhet 5 Apr 27, 2022
A wrapper for The Movie Database API v3 and v4 that only uses the read access token (not api key).

fulltmdb A wrapper for The Movie Database API v3 and v4 that only uses the read access token (not api key). Installation Use the package manager pip t

Jacob Hale 2 Sep 26, 2021
This is a simple program that uses Python and pyTwitchAPI to retrieve the list of users in a streamer's chat and then checks each one of these users to see if they follow the broadcaster or not

This is a simple program that uses Python and pyTwitchAPI to retrieve the list of users in a streamer's chat and then checks each one of these users to see if they follow the broadcaster or not

RwinShow 57 Dec 18, 2022
Retrieve information from DBLP and update BibTex files automatically

Rebib TLDR: This script retrieves information from DBLP to update your BibTex files. python rebib.py --bibfile xxx.bib It first parses the bib entries

Shangtong Zhang 49 Jan 1, 2023
VALORANT rank yoinker lets you retrieve the ranks and basic informations of everyone in the lobby, regardless of gamemode.

vRY VALORANT rank yoinker Retrieve the rank and basic information of everyone in the lobby, regardless of gamemode. Table of Contents Terms of Use Abo

Isaac Kenyon 270 Dec 30, 2022
Python3 library that can retrieve Chrome-based browser's saved login info.

Passax EDUCATIONAL PURPOSES ONLY Python3 library that can retrieve Chrome-based browser's saved login info. Requirements secretstorage~=3.3.1 pywin32=

Auax 1 Jan 25, 2022
Aula-API - a school system widely used in Denmark, as you can see and read about in the python file

Information : Hello, thank you for reading this first of all. This is a Aula-API

Binary.club 2 May 28, 2022
Read API docs offline, CLI, supports DevDocs.io compatible JSON files

Read API docs offline, CLI, supports DevDocs.io compatible JSON files

Tero Karvinen 3 Oct 18, 2022
Handles SDVX EXCEED GEAR result screen photos and attempts to read it.

Handles SDVX EXCEED GEAR result screen photos and attempts to read it.

silverhawke 1 Jan 8, 2022
A telegram bot to read RSS feeds

Telegram bot to fetch RSS feeds This is a telegram bot that fetches RSS feeds in regular intervals and send it to you. The feed sources can be added o

Santhosh Thottingal 14 Dec 15, 2022
Check AWS S3 instances for read/write/delete access

s3sec Test AWS S3 buckets for read/write/delete access This tool was developed to quickly test a list of s3 buckets for public read, write and delete

0xmoot 114 Dec 6, 2022
Read manga from your favourites websites on telegram.

tg-manga-bot Read manga from your favourites websites on telegram. Current Development Bot @idkpythonbot Telegram Channel tg_manga_bot Commands start

Daniel Rivero 41 Dec 22, 2022
A python to scratch API connector. Can fetch data from the API and send it back in cloud variables.

Scratch2py Scratch2py or S2py is a easy to use, versatile tool to communicate with the Scratch API Based of scratchclient by Raihan142857 Installation

null 20 Jun 18, 2022
Automation that uses Github Actions, Google Drive API, YouTube Data API and youtube-dl together to feed BackJam app with new music

Automation that uses Github Actions, Google Drive API, YouTube Data API and youtube-dl together to feed BackJam app with new music

Antônio Oliveira 1 Nov 21, 2021
BioThings API framework - Making high-performance API for biological annotation data

BioThings SDK Quick Summary BioThings SDK provides a Python-based toolkit to build high-performance data APIs (or web services) from a single data sou

BioThings 39 Jan 4, 2023
PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

Python Reddit API Wrapper Development 3k Dec 29, 2022
PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

Python Reddit API Wrapper Development 3k Dec 29, 2022
alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API.

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API. It allows rapid trading algo development easily, with support for both REST and streaming data interfaces

Alpaca 1.5k Jan 9, 2023