Skip to content

Commit 4cf5b45

Browse files
authored
Merge pull request #36 from digitalSaint/feat/dl
Adds a disabled list feature
2 parents 2ede652 + cfd0913 commit 4cf5b45

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

examples/example_injury.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
from tabulate import tabulate
4+
from mlbgame import injury
5+
import dateutil.parser
6+
from datetime import datetime
7+
8+
team_id = 117 # Houston Astros
9+
i = injury.Injury(team_id)
10+
injuries = []
11+
12+
for inj in i.injuries:
13+
team = inj.team_name
14+
injury = ['{0}, {1} ({2})'.format(inj.name_last, inj.name_first,
15+
inj.position), inj.insert_ts, inj.injury_status, inj.due_back,
16+
inj.injury_desc, inj.injury_update]
17+
injuries.append(injury)
18+
print tabulate(injuries, headers=[team, 'Updated', 'Status', 'Due Back',
19+
'Injury', 'Notes'])
20+
print
21+
print 'Last Updated: %s' % i.last_update
22+
23+
"""
24+
Astros Updated Status Due Back Injury Notes
25+
------------------- --------- --------- ------------------------- ------------------------------------ --------------------------------------------------------------------------------------------
26+
Musgrove, Joe (P) 06/09 10-day DL Likely June 12 Right shoulder discomfort Threw bullpen session June 8; played catch June 9.
27+
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.
28+
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.
29+
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.
30+
Gustave, Jandel (P) 05/31 10-day DL TBD Right forearm tightness Participating in throwing program in Florida as of May 31 update.
31+
32+
Last Updated: 2017-06-11 06:56:50
33+
"""

mlbgame/injury.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Module that is used for getting the MLB injuries.
5+
"""
6+
from __future__ import print_function
7+
8+
import sys
9+
import dateutil.parser
10+
import requests
11+
12+
class Injury(object):
13+
"""Represents the MLB Disabled List
14+
15+
Properties:
16+
injury_url
17+
injuries
18+
team_id
19+
injury_json
20+
last_update
21+
"""
22+
injury_url = 'http://mlb.mlb.com/fantasylookup/json/named.wsfb_news_injury.bam'
23+
24+
def __init__(self, team_id=None):
25+
if team_id:
26+
self.injury_url = Injury.injury_url
27+
self.injuries = []
28+
if isinstance(team_id, int):
29+
self.team_id = str(team_id)
30+
else:
31+
try:
32+
raise TeamIDException('A `team_id` must be an integer.')
33+
except TeamIDException as e:
34+
print(e)
35+
raise
36+
self.parse_injury()
37+
else:
38+
try:
39+
raise TeamIDException('A `team_id` was not supplied.')
40+
except TeamIDException as e:
41+
print(e)
42+
raise
43+
44+
@property
45+
def injury_json(self):
46+
"""Return injury output as json"""
47+
try:
48+
return requests.get(self.injury_url).json()
49+
except requests.exceptions.RequestException as e:
50+
print(e)
51+
sys.exit(-1)
52+
53+
@property
54+
def last_update(self):
55+
"""Return a dateutil object from string [last update]
56+
originally in ISO 8601 format: YYYY-mm-ddTHH:MM:SS"""
57+
last_update = self.injury_json['wsfb_news_injury']['queryResults']['created']
58+
return dateutil.parser.parse(last_update)
59+
60+
def parse_injury(self):
61+
"""Parse the json injury"""
62+
injuries = self.injury_json['wsfb_news_injury']['queryResults']['row']
63+
injuries = [ injury for injury in injuries if injury['team_id'] == self.team_id ]
64+
for injury in injuries:
65+
mlbinjury = type('Player', (object,), injury)
66+
self.injuries.append(mlbinjury)
67+
68+
69+
class injuryException(Exception):
70+
"""injury Exceptions"""
71+
72+
73+
class TeamIDException(injuryException):
74+
"""A `team_id` was not supplied or the `team_id` was not an integer."""
75+
76+
77+
#
78+
# @meta_classes
79+
#
80+
81+
#class Player(object):
82+
# """Represents an MLB injury
83+
#
84+
# Properties:
85+
# display_ts
86+
# due_back"
87+
# injury_desc
88+
# injury_status
89+
# injury_update
90+
# insert_ts
91+
# league_id
92+
# name_first
93+
# name_last
94+
# player_id
95+
# position
96+
# team_id
97+
# team_name

tests/files/injury.json

+1
Large diffs are not rendered by default.

tests/test_injury.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
import unittest
3+
import requests_mock
4+
import requests
5+
import json
6+
from datetime import datetime
7+
import dateutil.parser
8+
from mlbgame import injury
9+
10+
class TestInjury(unittest.TestCase):
11+
def setUp(self):
12+
base_url = 'http://mlb.mlb.com'
13+
self.injury_url = '%s/fantasylookup/json/named.wsfb_news_injury.bam' % base_url
14+
self.injury_file = 'tests/files/injury.json'
15+
with open(self.injury_file) as json_data:
16+
self.injury_json = json.load(json_data)
17+
json_data.close()
18+
19+
def tearDown(self):
20+
del self.injury_url
21+
del self.injury_file
22+
del self.injury_json
23+
24+
def test_team_id_is_str(self):
25+
team_id = 117
26+
i = injury.Injury(team_id)
27+
self.assertIsInstance(i.team_id, str)
28+
29+
def test_injury_url(self):
30+
team_id = 117
31+
i = injury.Injury(team_id)
32+
self.assertEqual(i.injury_url, self.injury_url)
33+
34+
def test_injury_is_list(self):
35+
team_id = 117
36+
i = injury.Injury(team_id)
37+
self.assertIsInstance(i.injuries, list)
38+
39+
@requests_mock.Mocker()
40+
def test_injury_json(self, requests_mock):
41+
requests_mock.get(self.injury_url, json=self.injury_json)
42+
team_id = 117
43+
i = injury.Injury(team_id)
44+
self.assertEqual(i.injury_json, self.injury_json)
45+
46+
@requests_mock.Mocker()
47+
def test_last_update(self, requests_mock):
48+
requests_mock.get(self.injury_url, json=self.injury_json)
49+
team_id = 117
50+
i = injury.Injury(team_id)
51+
last_update = self.injury_json['wsfb_news_injury']['queryResults']['created']
52+
self.assertEqual(dateutil.parser.parse(last_update), i.last_update)

0 commit comments

Comments
 (0)