Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a disabled list feature #36

Merged
merged 6 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/example_injury.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

from tabulate import tabulate
from mlbgame import injury
import dateutil.parser
from datetime import datetime

team_id = 117 # Houston Astros
i = injury.Injury(team_id)
injuries = []

for inj in i.injuries:
team = inj.team_name
injury = ['{0}, {1} ({2})'.format(inj.name_last, inj.name_first,
inj.position), inj.insert_ts, inj.injury_status, inj.due_back,
inj.injury_desc, inj.injury_update]
injuries.append(injury)
print tabulate(injuries, headers=[team, 'Updated', 'Status', 'Due Back',
'Injury', 'Notes'])
print
print 'Last Updated: %s' % i.last_update

"""
Astros Updated Status Due Back Injury Notes
------------------- --------- --------- ------------------------- ------------------------------------ --------------------------------------------------------------------------------------------
Musgrove, Joe (P) 06/09 10-day DL Likely June 12 Right shoulder discomfort Threw bullpen session June 8; played catch June 9.
Morton, Charlie (P) 06/09 10-day DL TBD Right lat strain Shut down as of May 31 update; threw from 75 feet June 9.
McHugh, Collin (P) 06/08 60-day DL TBD Posterior impingement in right elbow Doing light mound work per May 31 update; may throw breaking pitches soon per June 5 update.
Keuchel, Dallas (P) 06/10 10-day DL Possibly mid-to-late June Neck discomfort MRI revealed inflammation, will not throw for roughly one week as of June 10 update.
Gustave, Jandel (P) 05/31 10-day DL TBD Right forearm tightness Participating in throwing program in Florida as of May 31 update.

Last Updated: 2017-06-11 06:56:50
"""
97 changes: 97 additions & 0 deletions mlbgame/injury.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env python

"""
Module that is used for getting the MLB injuries.
"""
from __future__ import print_function

import sys
import dateutil.parser
import requests

class Injury(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.injury_url = Injury.injury_url
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()
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(self.injury_url).json()
except requests.exceptions.RequestException as e:
print(e)
sys.exit(-1)

@property
def 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 = type('Player', (object,), 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."""


#
# @meta_classes
#

#class Player(object):
# """Represents an MLB injury
#
# Properties:
# display_ts
# due_back"
# injury_desc
# injury_status
# injury_update
# insert_ts
# league_id
# name_first
# name_last
# player_id
# position
# team_id
# team_name
1 change: 1 addition & 0 deletions tests/files/injury.json

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions tests/test_injury.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
import unittest
import requests_mock
import requests
import json
from datetime import datetime
import dateutil.parser
from mlbgame import injury

class TestInjury(unittest.TestCase):
def setUp(self):
base_url = 'http://mlb.mlb.com'
self.injury_url = '%s/fantasylookup/json/named.wsfb_news_injury.bam' % base_url
self.injury_file = 'tests/files/injury.json'
with open(self.injury_file) as json_data:
self.injury_json = json.load(json_data)
json_data.close()

def tearDown(self):
del self.injury_url
del self.injury_file
del self.injury_json

def test_team_id_is_str(self):
team_id = 117
i = injury.Injury(team_id)
self.assertIsInstance(i.team_id, str)

def test_injury_url(self):
team_id = 117
i = injury.Injury(team_id)
self.assertEqual(i.injury_url, self.injury_url)

def test_injury_is_list(self):
team_id = 117
i = injury.Injury(team_id)
self.assertIsInstance(i.injuries, list)

@requests_mock.Mocker()
def test_injury_json(self, requests_mock):
requests_mock.get(self.injury_url, json=self.injury_json)
team_id = 117
i = injury.Injury(team_id)
self.assertEqual(i.injury_json, self.injury_json)

@requests_mock.Mocker()
def test_last_update(self, requests_mock):
requests_mock.get(self.injury_url, json=self.injury_json)
team_id = 117
i = injury.Injury(team_id)
last_update = self.injury_json['wsfb_news_injury']['queryResults']['created']
self.assertEqual(dateutil.parser.parse(last_update), i.last_update)