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
🤖 The bot that runs the official Fairfield Programming Association Discord server.

🤖 The bot that runs the official Fairfield Programming Association Discord server.

Fairfield Programming Association 1 Jan 07, 2022
A python API for BSCScan (Binance Smart Chain Explorer), available on PyPI.

bscscan-python A complete Python API for BscScan.com, available on PyPI. Powered by BscScan.com APIs. This is a gently modified fork of the etherscan-

Panagiotis Kotsias 246 Dec 31, 2022
SEMID - OSINT module with lots of discord functions

SEMID Framework About Semid is a framework with different Discord functions and

Hima 20 Sep 23, 2022
Microservice to extract structured information on EVM smart contracts.

Contract Serializer Microservice to extract structured information on EVM smart contract. Why? Modern NFT contracts may have different names for getPr

WeBill.io 8 Dec 19, 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

20 Jun 18, 2022
Bot to notify when vaccine appointments are available

Vaccine Watch Bot to notify when vaccine appointments are available. Supports checking Hy-Vee, Walgreens, CVS, Walmart, Cosentino's stores (KC), and B

Peter Carnesciali 37 Aug 13, 2022
A custom Discord Rich Presence to display when you're studying so you're stupid friends won't disturb you when you're studying.

Studying RPC Description A custom Discord Rich Presence to display when you're studying so you're stupid friends won't disturb you when you're studyin

John Edmerson Pizarra 4 Nov 19, 2022
Portal Backend for Yuta management

Portal Backend for Yuta management Prerequisites Python 3.10 or above. pip, pdm installed. Quickstart Install the required packages: pdm install Runn

Loc Mai 1 Dec 20, 2021
✨ Music&Video Userbot

🎶 Fizi - UserBot 🎶 🤖 Telegram UserBot Untuk Memutar Lagu Dan Video Di Obrolan Suara Telegram. ✨ Didukung Oleh PyTgCalls Pyrogram 📝 Persyaratan Pyt

F I Z I • Ɱeƙípres 4 Mar 29, 2022
A simple python script for rclone. Use multiple Google Service Accounts and cycle through them.

About GSAclone GSAclone is a simple python script for rclone, written with the purpose of using multiple Google service accounts on Google Drive and "

Shiro39 6 Feb 25, 2022
Whatsapp-APi Wrapper From rzawapi.my.id

Whatsapp-APi Wrapper From rzawapi.my.id

Rezza Priatna 2 Apr 19, 2022
A simple message content sniping Discord bot which you can run yourself! Sniping API pulled from isobot and Arch bot

Discord Snipe Bot This is a bot made with the same message content sniping API from isobot and Arch bot. It's default prefix is -, however you can als

notsniped 5 Aug 11, 2022
:snake: A simple library to fetch data from the iTunes Store API made for Python >= 3.5

itunespy itunespy is a simple library to fetch data from the iTunes Store API made for Python 3.5 and beyond. Important: Since version 1.6 itunespy no

Fran González 56 Dec 22, 2022
Yes, it's true :revolving_hearts: This repository has 301 stars.

Yes, it's true! Inspired by a similar repository from @RealPeha, but implemented using a webhook on AWS Lambda and API Gateway, so it's serv

511 Dec 30, 2022
It's a simple python script to take backup of directories (compressing) then the same to move your mentioned S3 bucket with the help of AWS IAM User.

Directory Backup Moved to S3 (Pyscript) Description Here it's a python script that needs to use this script simply create a directory backup and moved

Yousaf K Hamza 3 Mar 04, 2022
An Python SDK for QQ based on mirai-api-http v2.

Argon 一个基于 graia-broadcast 和 mirai-api-http v2 的 Python SDK。 本项目适用于 mirai-api-http 2.0 以上版本。 目前仍处于开发阶段,内部接口可能会有较大的变化。 The Stasis / 停滞 为维持 GraiaProject

BlueGlassBlock 1 Oct 29, 2021
Repositorio dedicado a contener los archivos fuentes del bot de discord "Lector de Ejercicios".

Lector de Ejercicios Este bot de discord está pensado para usarse únicamente en el discord de la materia Algoritmos y Programación I, de la Facultad d

Franco Lighterman Reismann 3 Sep 17, 2022
A.I and game for gomoku, working only on windows

Gomoku (A.I of gomoku) The goal of the project is to create an artificial intelligence of gomoku. Goals Beat the opponent. Requirements Python 3.7+ Wo

Luis Rosario 13 Jun 20, 2021
Takes upcoming items from a Google Calendar and posts them to Slack.

Google Calendar to Slack by Jason Snell - [email protected] This Python s

6 Aug 21, 2022
Solcast rooftop api for HA

Solcast Solar Home Assistant(https://www.home-assistant.io/) Component This custom component integrates the Solcast API into Home Assistant. Modified

Greg 1 Oct 11, 2021