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
Python Business Transactions Library - ContractsPY

Python Business Transactions Library - ContractsPY Declare and define business transactions in Python. Use the contracts library to validate business

Arzu Huseynov 7 Jun 21, 2022
Create a roles overview page for all Ansible roles/playbooks in Gitlab

ansible-create-roles-overview Overview The script ./create_roles_overview.py queries a Gitlab API for Ansible roles and playbooks. It will iterate ove

2 Oct 11, 2021
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
Python Bot that attends classes, answers polls, and then again waits for classes to start.

LPU_myclass_Bot LPU_myclass_Bot is a Python bot that waits for class to start, attends class, answers polls, and then again waits for another class to

Saurabh Kumar 6 Apr 07, 2022
Python bot for send videos of a Youtube channel to a telegram group , channel or chat

py_youtube_to_telegram Usage: If you want to install ytt and use it, run this command: sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/nima

Nima Fanniasl 8 Nov 22, 2022
Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

A python wrapper for interacting with the LabArchives API.

LabArchives API wrapper for Python A python wrapper for interacting with the LabArchives API. This very simple package makes it easier to make arbitra

Marek Cmero 3 Aug 01, 2022
Python client for Invidious' JSON API

Python project template A template for new Python projects. Features Automatically builds PDoc documentation & uploads package to PyPI on new GitHub r

Kevo 2 Jun 05, 2022
The Fastest multi spambot of Telegram ๐Ÿคž ๐Ÿคž

Revil Spam Bot The Fastest multi spambot of Telegram ๐Ÿคž ๐Ÿคž ๐š‚๐š„๐™ฟ๐™ฟ๐™พ๐š๐šƒ ๐Ÿ–ค แด„ส€แด‡แด€แด›แดส€ ๐Ÿ–ค โšก ๐“ก๐“ฎ๐“ฟ๐“ฒ๐“ต ๐“—๐“พ๐“ท๐“ฝ๐“ฎ๐“ป ๐”๐”ฒ๐”ฉ๐”ฑ๐”ฆ แบžรธโœž๏ธŽ โšก ๐“ ๐•พะผฮฟฮฟฯ„ะฝ ๐“ะธโˆ‚ ๐•ฑ

REVIL HUNTER 4 Dec 08, 2021
A telegram bot that sends a meme a day, from reddit's top meme of the day

MemeBot A telegram bot that sends a meme a day, from reddit's top meme of the day You can use the bot either with an external scheduler (ex: pythonany

Michele Vitulli 1 Dec 13, 2021
Wrapper for vk_api lib for faster bot buliding

Welcome to VKBotPod repository! Wrapper for vk_api lib for faster bot buliding Features Simple syntax Rich functionality Special thanks to movpushmov

NullPointerException 3 Jan 14, 2022
Pythonic event-processing library based on decorators

Process Events In Style This library aims to simplify the common pattern of event processing. It simplifies the process of filtering, dispatching and

Nicolas Marier 3 Sep 01, 2022
8300-account-nuker - A simple accoutn nuker or can use it full closing dm and leaving server

8300 ACCOUNT NUKER VERISON: its just simple accoutn nuker or can use it full clo

โ€ โ€  5 Jan 26, 2022
A small python script which runs a speedtest using speedtest.net and inserts it into a Google Docs Spreadsheet.

speedtest-google-sheets This is a small python script which runs a speedtest using speedtest.net and inserts it into a Google Docs Spreadsheet. Setup

marie 2 Feb 10, 2022
My Discord Bot that I used to learn Python. Please disregard the unstructured code!

Botsche My personal Discord Bot. To run this bot, change TOKEN in config.ini to your Discord Bot Token, which can be retrieved from your Discord Dev

Mats Voss 1 Nov 29, 2021
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 01, 2023
Example app to be deployed to AWS as an API Gateway / Lambda Stack

Disclaimer I won't answer issues or emails regarding the project anymore. The project is old and not maintained anymore. I'm not sure if it still work

Ben 123 Jan 01, 2023
A Discord bot made by QwertyIsCoding

QwertyBot QwertyBot A Discord bot made by QwertyIsCoding Explore the docs ยป View Demo . Report Bug . Request Feature About The Project This Discord bo

4 Oct 08, 2022
A fast, easy to set up telegram userbot running Python 3 which uses fork of the Telethon Library.

forked from friendly-telegram/friendly-telegram Friendly Telegram Userbot A fast, easy to set up telegram userbot running Python 3 which uses fork of

GeekTG 75 Jan 04, 2023
Uploader-Bot - A Modified Telegram Url Uploader Bot With Mongodb, Zee5, Sonyliv Support and Many Other Yt-dlp Sites

๐š๐šŽ๐šš๐šž๐š’๐š›๐šŽ๐š ๐š…๐šŠ๐š›๐š’๐šŠ๐š‹๐š•๐šŽ๐šœ ๐Ÿ”Š APP_ID API_HASH TG_BOT_TOKEN DATABASE_URL

11 Sep 10, 2022