Skip to content

Commit

Permalink
minor code clean up:
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyhurley committed Jun 20, 2017
1 parent 69595f2 commit 7f89050
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ target/

# testing file
test.py

# idea
*.idea*
10 changes: 8 additions & 2 deletions mlbgame/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,14 @@ def day(year, month, day, home=None, away=None):
return []
# get data
data = mlbgame.game.scoreboard(year, month, day, home=home, away=away)
results = [mlbgame.game.GameScoreboard(data[x]) for x in data]
return results
return [mlbgame.game.GameScoreboard(data[x]) for x in data]


def todays_games(home=None, away=None):
from datetime import date
data = mlbgame.game.scoreboard(date.today().year, date.today().month, date.today().day, home=home, away=away)
return [mlbgame.game.GameScoreboard(data[x]) for x in data]


def games(years, months=None, days=None, home=None, away=None):
"""Return a list of lists of games for multiple days.
Expand Down
125 changes: 67 additions & 58 deletions mlbgame/data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

"""This module gets the XML data that other functions use.
It checks if the data is cached first, and if not,
It checks if the data is cached first, and if not,
gets the data from mlb.com.
"""

Expand All @@ -14,93 +14,102 @@
except ImportError:
from urllib2 import urlopen, HTTPError


# Templates For Local Paths and URLS
BASE_URL = "http://gd2.mlb.com/components/game/mlb/year_{0}/month_{1}/day_{2}/"
GAME_URL = BASE_URL + "/gid_{3}/{4}"
BASE_PATH = "gameday-data/year_{0}/month_{1}/day_{2}/"
GAME_PATH = BASE_PATH + "gid_{3}{4}/"

PROPERTY_URL = "http://mlb.mlb.com/properties/mlb_properties.xml"
# Local Directory
PWD = os.path.join(os.path.dirname(__file__))


def get_scoreboard(year, month, day):
"""Return the game file for a certain day matching certain criteria."""
# add zeros if less than 10
monthstr = str(month).zfill(2)
daystr = str(day).zfill(2)
# file
filename = "gameday-data/year_%i/month_%s/day_%s/scoreboard.xml.gz" % (year, monthstr, daystr)
file = os.path.join(os.path.dirname(__file__), filename)
local_filename = BASE_PATH.format(year, monthstr, daystr) + "scoreboard.xml.gz"
local_file = os.path.join(PWD, local_filename)
# check if file exits
if os.path.isfile(file):
if os.path.isfile(local_file):
if sys.platform == 'win32':
file_unzipped = file[:-3]
file_unzipped = local_file[:-3]
if not os.path.exists(file_unzipped):
import gzip
f_gz = gzip.open(file, 'rb')
f = open(file_unzipped, 'wb')
f.write(f_gz.read())
f.close()
f_gz.close()
file = file_unzipped
data = file
else:
# get data if file does not exist
try:
data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%i/month_%s/day_%s/scoreboard.xml" % (year, monthstr, daystr))
except HTTPError:
data = os.path.join(os.path.dirname(__file__), "gameday-data/default.xml")
with(gzip.open(local_file, 'rb')) as f_gz:
with(open(file_unzipped, 'wb')) as f:
f.write(f_gz.read())
return local_file
# get data if file does not exist
try:
data = urlopen(BASE_URL.format(year, monthstr, daystr) + "scoreboard.xml")
except HTTPError:
data = os.path.join(PWD, "gameday-data/default.xml")
return data


def get_box_score(game_id):
"""Return the box score file of a game with matching id."""
# get relevant information from game id
year, month, day, rest = game_id.split('_', 3)
# file
filename = "gameday-data/year_%s/month_%s/day_%s/gid_%s/boxscore.xml" % (year, month, day, game_id)
file = os.path.join(os.path.dirname(__file__), filename)
year, month, day = get_date_from_game_id(game_id)
local_filename = GAME_PATH.format(year, month, day , game_id, "boxscore.xml")
local_file = os.path.join(PWD, local_filename)
# check if file exits
if os.path.isfile(file):
data = file
else:
# get data if file does not exist
try:
data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/gid_%s/boxscore.xml" % (year, month, day, game_id))
except HTTPError:
raise ValueError("Could not find a game with that id.")
return data
if os.path.isfile(local_file):
return local_file
# get data if file does not exist
try:
return urlopen(GAME_URL.format(year, month, day , game_id, "boxscore.xml"))
except HTTPError:
raise ValueError("Could not find a game with that id.")


def get_game_events(game_id):
"""Return the game events file of a game with matching id."""
# get relevant information from game id
year, month, day, rest = game_id.split('_', 3)
# file
filename = "gameday-data/year_%s/month_%s/day_%s/gid_%s/game_events.xml" % (year, month, day, game_id)
file = os.path.join(os.path.dirname(__file__), filename)
year, month, day = get_date_from_game_id(game_id)
local_filename = GAME_PATH.format(year, month, day, game_id, game_id, "game_events.xml")
local_file = os.path.join(PWD, local_filename)
# check if file exits
if os.path.isfile(file):
data = file
else:
# get data if file does not exist
try:
data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/gid_%s/game_events.xml" % (year, month, day, game_id))
except HTTPError:
raise ValueError("Could not find a game with that id.")
return data
if os.path.isfile(local_file):
return local_file
# get data if file does not exist
try:
return urlopen(GAME_URL.format(year, month, day, game_id, "game_events.xml"))
except HTTPError:
raise ValueError("Could not find a game with that id.")


def get_overview(game_id):
"""Return the linescore file of a game with matching id."""
# get relevant information from game id
year, month, day, rest = game_id.split('_', 3)
# file
filename = "gameday-data/year_%s/month_%s/day_%s/gid_%s/linescore.xml" % (year, month, day, game_id)
file = os.path.join(os.path.dirname(__file__), filename)
year, month, day = get_date_from_game_id(game_id)
local_filename = GAME_PATH.format(year, month, day, game_id, "linescore.xml")
local_file = os.path.join(PWD, local_filename)
# check if file exits
if os.path.isfile(file):
data = file
else:
if os.path.isfile(local_file):
return local_file
# get data if file does not exist
try:
data = urlopen("http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/gid_%s/linescore.xml" % (year, month, day, game_id))
except HTTPError:
raise ValueError("Could not find a game with that id.")
return data
try:
return urlopen(GAME_URL.format(year, month, day, game_id, "linescore.xml"))
except HTTPError:
raise ValueError("Could not find a game with that id.")


def get_properties():
"""Return the current mlb properties file"""
try:
return urlopen("http://mlb.mlb.com/properties/mlb_properties.xml")
return urlopen(PROPERTY_URL)
# in case mlb.com depricates this functionality
except HTTPError:
raise ValueError("Could not find the properties file. mlb.com does not provide the file that mlbgame needs to perform this operation.")
raise ValueError("Could not find the properties file. "
"mlb.com does not provide the file that mlbgame needs to perform this operation.")


def get_date_from_game_id(game_id):
year, month, day, _discard = game_id.split('_', 3)
return year, month, day
1 change: 1 addition & 0 deletions mlbgame/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime
import lxml.etree as etree


def scoreboard(year, month, day, home=None, away=None):
"""Return the scoreboard information for games matching the parameters as a dictionary."""
# get data
Expand Down

0 comments on commit 7f89050

Please sign in to comment.