Created
June 18, 2013 20:24
-
-
Save gfranxman/5809010 to your computer and use it in GitHub Desktop.
mlb boxscores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
import urllib2 | |
from datetime import datetime, timedelta | |
import simplejson as JSON | |
from pprint import pprint | |
import re | |
when = datetime.now() #- timedelta(days=1) | |
GRID_URL_TEMPLATE = r'http://gd2.mlb.com/components/game/mlb/year_%(year)d/month_%(month)02d/day_%(day)02d/grid.json' | |
BOX_SCORE_URL_TEMPLATE = r'http://gd2.mlb.com/components/game/mlb/year_%(year)s/month_%(month)s/day_%(day)s/gid_%(year)s_%(month)s_%(day)s_%(teama)s_%(teamb)s_%(seq)s/boxscore.json' | |
#lazy/poor mans xpath for json ( no support for indicies or anything cool like that ) | |
def pull( data, key_list, default ): | |
if len( key_list ) == 0: | |
return default | |
if len( key_list ) == 1: | |
return data.get( key_list[0], default ) | |
else: | |
return pull( data[key_list[0]], key_list[1:], default ) | |
def pull_by_path( data, path, default ): | |
return pull( data, path.split('/'), default ) | |
grid = urllib2.urlopen( GRID_URL_TEMPLATE % { 'day': when.day, 'month':when.month, 'year': when.year } ).read() | |
gamesdata = JSON.loads( grid ) | |
for game in pull_by_path( gamesdata, 'data/games/game', [] ): | |
print "%s at %s -- %s" % ( game['away_team_name'], game['home_team_name'], game['event_time'] ) | |
m = re.match( r"(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<teama>\w+)-(?P<teamb>\w+)-(?P<seq>\d+)", game['id'] ) | |
try: | |
boxscore = urllib2.urlopen( BOX_SCORE_URL_TEMPLATE % m.groupdict() ).read() | |
boxscoredata = JSON.loads( boxscore ) | |
#print "box score:", boxscore | |
#pprint( boxscoredata ) | |
print "home", | |
print pull_by_path( boxscoredata, 'data/boxscore/home_sname', "NA"), | |
print pull_by_path( boxscoredata, 'data/boxscore/home_wins', "NA"), | |
print pull_by_path( boxscoredata, 'data/boxscore/home_loss', "NA") | |
print "away", | |
print pull_by_path( boxscoredata, 'data/boxscore/away_sname', "NA"), | |
print pull_by_path( boxscoredata, 'data/boxscore/away_wins', "NA"), | |
print pull_by_path( boxscoredata, 'data/boxscore/away_loss', "NA") | |
print "boxscores" | |
for inning in pull_by_path( boxscoredata, 'data/boxscore/linescore/inning_line_score', ['bogus',3,3,] ): | |
print "%2s" % inning['inning'], | |
print "%17s" % "team", | |
print "%7s" % "runs", | |
print "%7s" % "hits", | |
print "%7s" % "errors" | |
for inning in pull_by_path( boxscoredata, 'data/boxscore/linescore/inning_line_score', ['bogus',3,3,] ): | |
print "%2s" % inning['away'], | |
print "%17s" % pull_by_path( boxscoredata,'data/boxscore/away_sname', "_"), | |
print "%7s" % pull_by_path( boxscoredata,'data/boxscore/linescore/away_team_runs', "_"), | |
print "%7s" % pull_by_path( boxscoredata,'data/boxscore/linescore/away_team_hits', "_"), | |
print "%7s" % pull_by_path( boxscoredata,'data/boxscore/linescore/away_team_errors', "_") | |
for inning in pull_by_path( boxscoredata, 'data/boxscore/linescore/inning_line_score', ['bogus',3,3,] ): | |
print "%2s" % inning['home'], | |
print "%17s" % pull_by_path( boxscoredata,'data/boxscore/home_sname','_') , | |
print "%7s" % pull_by_path( boxscoredata, 'data/boxscore/linescore/home_team_runs','_') , | |
print "%7s" % pull_by_path( boxscoredata, 'data/boxscore/linescore/home_team_hits','_') , | |
print "%7s" % pull_by_path( boxscoredata, 'data/boxscore/linescore/home_team_errors','_' ) | |
except Exception, e: #urllib2.HttpException, e: | |
#print e | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment