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
A Discord BOT that uses Google Sheets for storing the roles and permissions of a discord server.

Discord Role Manager Bot Role Manager is a discord BOT that utilizes Google Sheets for the organization of a server's hierarchy and permissions. Detai

Dion Rigatos 17 Oct 13, 2022
Guilherme Matheus 11 Sep 11, 2022
Collaboration with Microsoft, AWS, Google, and ETHZürich Analytics Club (2022 Datathon Project)

DATATHON_ Collaboration with Microsoft, AWS, Google, and ETHZürich Analytics Club (2022 Datathon Project) Datathon Original Challenge SAV DataDays Rei

esthi 34 Nov 10, 2022
Auxiliator is telegram bot for basic web-application analysis

Auxiliator Auxiliator is telegram bot for basic web-application analysis What for? Sometimes there is no access to your main PC, where you can scan we

Revoltage 13 Dec 26, 2021
Anti Spam/NSFW Telegram Bot Written In Python With Pyrogram.

Anti Spam/NSFW Telegram Bot Written In Python With Pyrogram.

Wahyusaputra 2 Dec 29, 2021
A Telegram Video Merge Bot by @AbirHasan2005

VideoMerge-Bot This is very simple Telegram Videos Merge Bot by @AbirHasan2005. Using FFmpeg for Merging Videos. Features: Merge Multiple Videos. User

Abir Hasan 57 Nov 12, 2022
A Django-style ORM idea for manipulating Google Datastore entities

No SeiQueLa ORM EM DESENVOLVIMENTO Uma ideia de ORM no estilo do Django para manipular entidades do Google Datastore. Montando seu modelo: from noseiq

Geraldo Castro 16 Nov 01, 2022
A powerfull SMS Bomber for Bangladesh . NO limite .Unlimited SMS Spaming

RedBomberBD A powerfull SMS Bomber for Bangladesh . NO limite .Unlimited SMS Spaming Installation Install my-tool on termux by using thoes commands pk

Abdullah Al Redwan 3 Feb 16, 2022
Notification Reminder Application For Python

Notification-Reminder-Application No matter how well you set up your to-do list and calendar, you aren’t going to get things done unless you have a re

1 Nov 26, 2021
Signs the target email up to over 1000 different mailing lists to get spammed each day.

Email Bomber Say goodbye to that email Features Signs up to over 1k different mailing lists Written in python so the program is lightweight Easy to us

Loxdr 1 Nov 30, 2021
Policy and data administration, distribution, and real-time updates on top of Open Policy Agent

⚡ OPAL ⚡ Open Policy Administration Layer OPAL is an administration layer for Open Policy Agent (OPA), detecting changes to both policy and policy dat

8 Dec 07, 2022
pokemon-colorscripts compatible for mac

Pokemon colorscripts some scripts to print out images of pokemons to terminal. Inspired by DT's colorscripts compilation Description Prints out colore

43 Jan 06, 2023
A minimalistic, modern Discord bot for roles and polls using dropdowns

DropBot A minimalistic, modern Discord bot for roles and polls using dropdowns Made by ThatOneCalculator Technologies used Instructions Type /, and na

ModernBots 1 Jun 27, 2022
A QQ(Tencent) robot created by go-cqhttp & nonebot2

绘梨花(胶布)Bot|ErikaBot ✨ 基于NoneBot2的绘梨花多功能 Bot ,自用 ✨ 快速开始 参考go-cqhttp项目文档,配置好机器人的相关设置,以及反向ws客户端 参考nonebot2项目文档,添加必要的.env相关设置 安装本项目相关的依赖库(依赖清单) git clone本

10 Aug 09, 2022
Programa de código abierto para probar el API de Bitso, el exchange más importante de América Latina.

Bitso Semiautomático Programa de código abierto para probar el API de Bitso, el exchange más importante de América Latina. Desarrollador Fernando Mire

Fernando Mireles 17 Dec 07, 2022
Discord bot for polls and votes including STV. Supports hiding results and is written with Discord.py

VoteBot Discord voting bot capable of standard polls, as found in many other bots; anonymous polls, where votes are hidden and totals are only display

6 Nov 15, 2022
OpenVisionAPI client

OpenVisionAPI Client 🚀 Getting Started Prerequisites Installing Install the dependencies $ make setup Usage $ source .venv/bin/activate $ ./ova_clie

Open Vision API 40 Nov 11, 2022
A Discord token stealer app written in Python 3.

Discord Token Stealer A Discord token stealer app written in Python 3. This version of the grabber only supports Windows. Features No local caching Tr

cankat 45 Jan 03, 2023
Python client for QIWI payment system

Pyqiwi Lib for QIWI payment system Installation pip install pyqiwi Usage from decimal import Decimal from datetime import datetime, timedelta from p

Andrey 12 Jun 03, 2022
Lazy airdrop based on private temporary ids

LobsterDAO This uses a modified MerkleDistributor, which allows to issue a lazy airdrop using temporary IDs. In this example it uses Telegram chat_id

41 Sep 10, 2022