Skip to content

Commit 71a567b

Browse files
committed
game_scoring_plays()
1 parent 66b1ca2 commit 71a567b

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ If you install manually, be sure to also install requests.
4848

4949
* `statsapi.game_pace()` - get information about pace of game for a given season (back to 1999)
5050

51+
* `statsapi.game_scoring_plays()` - get a list of scoring plays for a given game
52+
5153
## Example Use
5254

5355
### Print the number of games won by the Oakland Athletics in 2018
@@ -132,3 +134,9 @@ lookup Chase Utley's person id from the results, and pass it into `statsapi.play
132134
using `type='hitting'` and `group='career'`
133135

134136
print( statsapi.player_stats(next(x['id'] for x in statsapi.get('sports_players',{'season':2008,'gameType':'W'})['people'] if x['fullName']=='Chase Utley'), 'hitting', 'career') )
137+
138+
### Print a list of scoring plays from the 4/28/2019 Marlins @ Phillies game
139+
140+
```
141+
print( statsapi.game_scoring_plays(567074) )
142+
```

statsapi/__init__.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ def linescore(gamePk,timecode=None):
617617
linescore += ('{:^2}' * (len(k[1])-3)).format(*k[1])
618618
linescore += ('{:^4}' * 3).format(*k[1][-3:])
619619
linescore += '\n'
620-
linescore = linescore[:-1] #strip the extra line break
620+
if len(linescore)>1: linescore = linescore[:-1] #strip the extra line break
621621

622622
return linescore
623623

@@ -633,12 +633,42 @@ def next_game(teamId):
633633
"""
634634
return get('team',{'teamId':teamId,'hydrate':'nextSchedule','fields':'teams,id,teamName,nextGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name'})['teams'][0]['nextGameSchedule']['dates'][0]['games'][0]['gamePk']
635635

636-
def game(gamePk):
637-
"""Get the lineups and player stats for a given game
638-
"""
639-
636+
def game_scoring_plays(gamePk):
637+
"""Get a list of scoring plays for a given game
638+
639+
Example use:
640+
641+
Print a list of scoring plays from the 4/28/2019 Marlins @ Phillies game
640642
641-
return
643+
print( statsapi.game_scoring_plays(567074) )
644+
645+
Output (truncated to show only the first and last records):
646+
647+
Rhys Hoskins doubles (6) on a sharp line drive to left fielder Isaac Galloway. Bryce Harper scores.
648+
Bottom 1 - Miami Marlins: 0, Philadelphia Phillies: 1
649+
650+
Rhys Hoskins walks. Andrew McCutchen scores. Jean Segura to 3rd. Wild pitch by pitcher Tayron Guerrero.
651+
Bottom 8 - Miami Marlins: 1, Philadelphia Phillies: 5
652+
"""
653+
r = get('schedule',{'sportId':1,'gamePk':gamePk,'hydrate':'scoringplays','fields':'dates,date,games,teams,away,team,name,scoringPlays,result,description,awayScore,homeScore,about,halfInning,inning,endTime'})
654+
if not len(r['dates'][0]['games'][0]['scoringPlays']): return ''
655+
656+
items = r['dates'][0]['games'][0]['scoringPlays']
657+
home_team = r['dates'][0]['games'][0]['teams']['home']['team']['name']
658+
away_team = r['dates'][0]['games'][0]['teams']['away']['team']['name']
659+
660+
scoring_plays = ''
661+
unorderedPlays = {}
662+
for v in items:
663+
unorderedPlays.update({v['about']['endTime'] : v})
664+
sortedPlays = []
665+
for x in sorted(unorderedPlays):
666+
sortedPlays.append(unorderedPlays[x])
667+
for a in sortedPlays:
668+
scoring_plays += '{}\n{} {} - {}: {}, {}: {}\n\n'.format(a['result']['description'], a['about']['halfInning'][0:1].upper() + a['about']['halfInning'][1:], a['about']['inning'], away_team, a['result']['awayScore'], home_team, a['result']['homeScore'])
669+
if len(scoring_plays)>1: scoring_plays = scoring_plays[:-2] #strip the extra line break
670+
671+
return scoring_plays
642672

643673
def game_highlights(gamePk):
644674
"""Get the highlight video links for a given game

statsapi/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python
2+

0 commit comments

Comments
 (0)