Skip to content

Commit

Permalink
Refactor injuries to include all injuries
Browse files Browse the repository at this point in the history
  • Loading branch information
panzarino committed Sep 8, 2017
1 parent bda8eb2 commit 4a99955
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 72 deletions.
7 changes: 4 additions & 3 deletions mlbgame/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def standings(date=datetime.now()):
return mlbgame.info.Standings(data)


def injury(team_id):
"""Return Injuries object that contains injury info for a team"""
return mlbgame.info.Injuries(team_id)
def injury():
"""Return Injuries object that contains injury info"""
data = mlbgame.info.injury()
return mlbgame.info.Injuries(data)
11 changes: 11 additions & 0 deletions mlbgame/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
GAME_URL = BASE_URL + '/gid_{3}/{4}'
PROPERTY_URL = 'http://mlb.mlb.com/properties/mlb_properties.xml'
ROSTER_URL = 'http://mlb.mlb.com/lookup/json/named.roster_40.bam?team_id={0}'
INJURY_URL = 'http://mlb.mlb.com/fantasylookup/json/named.wsfb_news_injury.bam'
STANDINGS_URL = ('http://mlb.mlb.com/lookup/json/named.standings_schedule_date.bam?season={0}&'
'schedule_game_date.game_date=%27{1}%27&sit_code=%27h0%27&league_id=103&'
'league_id=104&all_star_sw=%27N%27&version=2')
Expand Down Expand Up @@ -121,6 +122,16 @@ def get_historical_standings(date):
ValueError('Could not find standings for that date.')


def get_injuries():
"""Return the injuries file for specified date."""
try:
return urlopen(INJURY_URL)
except HTTPError:
ValueError('Could not find the injuries 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 int(year), int(month), int(day)
81 changes: 16 additions & 65 deletions mlbgame/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,6 @@ def __init__(self, data):
self.players.append(Player(player))


class RosterException(Exception):
"""Roster Exceptions"""


class NoTeamID(RosterException):
"""A `team_id` was not supplied"""


class Player(mlbgame.object.Object):
"""Represents an MLB Player
Expand Down Expand Up @@ -257,6 +249,10 @@ class Standings(object):
"""

def __init__(self, data):
"""Creates a standings object for info specified in `data`.
`data` should be a dictionary of values
"""
self.standings_schedule_date = data['standings_schedule_date']
self.last_update = data['last_update']
self.divisions = [Division(x['division'], x['teams']) for x in data['divisions']]
Expand Down Expand Up @@ -329,70 +325,25 @@ class Team(mlbgame.object.Object):
"""
pass

def injury():
data = mlbgame.data.get_injuries()
parsed = json.loads(data.read().decode('utf-8'))
return parsed['wsfb_news_injury']['queryResults']['row']


class Injuries(object):
"""Represents the MLB Disabled List
Properties:
injury_url
injuries
team_id
injury_json
last_update
"""
injury_url = 'http://mlb.mlb.com/fantasylookup/json/named.wsfb_news_injury.bam'

def __init__(self, team_id=None):
if team_id:
self.injuries = []
if isinstance(team_id, int):
self.team_id = str(team_id)
else:
try:
raise TeamIDException('A `team_id` must be an integer.')
except TeamIDException as e:
print(e)
raise
self.parse_injury()
self.last_update = self.set_last_update()
else:
try:
raise TeamIDException('A `team_id` was not supplied.')
except TeamIDException as e:
print(e)
raise

@property
def injury_json(self):
"""Return injury output as json"""
try:
return requests.get(Injuries.injury_url).json()
except requests.exceptions.RequestException as e:
print(e)
sys.exit(-1)

def set_last_update(self):
"""Return a dateutil object from string [last update]
originally in ISO 8601 format: YYYY-mm-ddTHH:MM:SS"""
last_update = self.injury_json['wsfb_news_injury']['queryResults']['created']
return dateutil.parser.parse(last_update)

def parse_injury(self):
"""Parse the json injury"""
injuries = self.injury_json['wsfb_news_injury']['queryResults']['row']
injuries = [
injury for injury in injuries if injury['team_id'] == self.team_id]
for injury in injuries:
mlbinjury = Injury(injury)
self.injuries.append(mlbinjury)


class injuryException(Exception):
"""injury Exceptions"""


class TeamIDException(injuryException):
"""A `team_id` was not supplied or the `team_id` was not an integer."""

def __init__(self, injuries):
"""Creates an Injuries object for given data.
`injuries` should be a list of injuries.
"""
self.injuries = [Injury(x) for x in injuries]


class Injury(mlbgame.object.Object):
Expand Down
5 changes: 1 addition & 4 deletions tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,7 @@ def test_standings_historical(self):
self.assertEqual(team.x_wl_seas, '88-74')

def test_injury(self):
injury = mlbgame.injury(121)
self.assertIsInstance(injury.last_update, datetime)
injury = mlbgame.injury()
self.assertIsInstance(injury.injuries, list)
for player in injury.injuries:
self.assertIsInstance(player.display_ts, str)
Expand All @@ -436,5 +435,3 @@ def test_injury(self):
self.assertIsInstance(player.position, str)
self.assertIsInstance(player.team_id, int)
self.assertIsInstance(player.team_name, str)
self.assertEqual(player.team_id, 121)
self.assertEqual(player.team_name, 'Mets')

0 comments on commit 4a99955

Please sign in to comment.