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
Minimal telegram voice chat music bot, in pyrogram.

VCBOT Fully working VC (user)Bot, based on py-tgcalls and py-tgcalls-wrapper with minimal features. Deploying To heroku: Local machine/VPS: git clone

Aditya 33 Nov 12, 2022
Add Me To Your Group Enjoy With Me. Pyrogram bot. https://t.me/TamilSupport

SongPlayRoBot 3X Fast Telethon Based Bot ⚜ Open Source Bot 👨🏻‍💻 Demo : SongPlayRoBot 💃🏻 Easy To Deploy 🤗 Click Below Image to Deploy DEPLOY Grou

IMVETRI 850 Dec 30, 2022
Senexia - A powerful telegram bot to manage your groups as effectively as possible

⚡ Kenechi bot ⚡ A Powerful, Smart And Simple Group Manager ... Written with AioG

Akhi 2 Jan 11, 2022
Python SDK for IEX Cloud

iexfinance Python SDK for IEX Cloud. Architecture mirrors that of the IEX Cloud API (and its documentation). An easy-to-use toolkit to obtain data for

Addison Lynch 640 Jan 07, 2023
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
SimpleDCABot is a simple bot that buys crypto with a dollar-cost averaging strategy.

Simple Open Dollar Cost Averaging (DCA) Bot SimpleDCABot is a simple bot that buys crypto on a selected exchange at regular intervals for a prescribed

4 Mar 28, 2022
A discord bot that moderates your server!

Staff Bot para Discord O que é? É um bot que modera o seu servidor no Discord, apagando mensagens indesejadas que os usuários mandem! Como usar Primei

Isac Gonçalves Cunha 3 Oct 07, 2021
Stock Market Insights is a Dashboard that gives the 360 degree view of the particular company stock

fedora-easyfix A collection of self-contained and well-documented issues for newcomers to start contributing with How to setup the local development e

Ganesh N 3 Sep 10, 2021
A GitHub Action that automatically reports your Advent of Code progress in a table in your README

Advent README Stars This action adds and maintains a stars report in your README based on your Advent of Code progress. Example Table 2021 Results Day

Kevin Duff 36 Dec 30, 2022
And now, for the first time, you can send alerts via action from ArcSight ESM Console to the TheHive when Correlation Rules are triggered.

ArcSight Integration with TheHive And now, for the first time, you can send alerts via action from ArcSight ESM Console to the TheHive when Correlatio

Amir Hossein Zargaran 3 Jan 19, 2022
派蒙Bot / PaimonBot

派蒙Bot / PaimonBot 基于Mrs4s / go-cqhttp 和 nonebot / nonebot2 的原神QQ群聊机器人 特别鸣谢 MingxuanGame 的人物武器名字自动纠正功能 小灰灰 的人物武器信息api 环境 请务必使用Python3.7以上版本!! 尽量在linux下

晓轩 96 Dec 16, 2022
Tools used by Ada Health's internal IT team to deploy and manage a serverless Munki setup.

Serverless Munki This repository contains cross platform code to deploy a production ready Munki service, complete with AutoPkg, that runs entirely fr

Ada Health 17 Dec 05, 2022
A simple chat api that can also work with ipb4 and chatbox+

SimpleChatApi API for chatting that can work on its own or work with Invision Community and Chatbox+. You are also welcome to create frontend for this

Anubhav K. 1 Feb 01, 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
HelpDESK Dynamics

Helpdesk Application The project is a Helpdesk application (Helpdesk dynamics) where staff of an organization can raise and assign job/trouble tickets

Okeoma Ihunwo 0 Nov 14, 2021
Python: Asynchronous client for the Tailscale API

Python: Asynchronous client for the Tailscale API Asynchronous client for the Tailscale API. About This package allows you to control and monitor Tail

Franck Nijhof 9 Nov 22, 2022
Dumps to CSV all the resources in an organization's member accounts

AWS Org Inventory Dumps to CSV all the resources in an organization's member accounts. Set your environment's AWS_PROFILE and AWS_DEFAULT_REGION varia

Iain Samuel McLean Elder 2 Dec 24, 2021
Ini Hanya Shortcut Untuk Menambahkan Kunci Tambahan Pada Termux & Membantu Para Nub Yang Decode Script Orang:v

Ini Hanya Shortcut Untuk Menambahkan Kunci Tambahan Pada Termux & Membantu Para Nub Yang Decode Script Orang:v

Lord_Ammar 1 Jan 23, 2022
Smilecreator4 - This site is for people who want to hack or want to learn it!

smilecreator4 This site is for people who want to hack or want to learn it! Furthermore, this program does not work without turning off Antivirus or W

1 Jan 04, 2022
A Discord bot that automatically saves SHSH blobs for all of your iOS devices.

AutoTSS AutoTSS is a Discord bot that automatically saves SHSH blobs for all of your iOS devices. Want a CLI automatic blob saver? Check out AutoTSS-c

adam 79 Dec 13, 2022