@@ -621,6 +621,137 @@ def linescore(gamePk,timecode=None):
621
621
622
622
return linescore
623
623
624
+ def last_game (teamId ):
625
+ """Get the gamePk for the given team's most recent game.
626
+ Note: Sometimes Stats API will actually return the next game in the previousSchedule hydration
627
+ """
628
+ return get ('team' ,{'teamId' :teamId ,'hydrate' :'previousSchedule' ,'fields' :'teams,id,teamName,previousGameSchedule,dates,date,games,gamePk,season,gameDate,teams,away,home,team,name' })['teams' ][0 ]['previousGameSchedule' ]['dates' ][0 ]['games' ][0 ]['gamePk' ]
629
+
630
+ def next_game (teamId ):
631
+ """Get the gamePk for the given team's next game.
632
+ Note: Sometimes Stats API will actually return the next game in the previousSchedule hydration
633
+ """
634
+ 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' ]
635
+
636
+ def game (gamePk ):
637
+ """Get the lineups and player stats for a given game
638
+ """
639
+
640
+
641
+ return
642
+
643
+ def game_highlights (gamePk ):
644
+ """Get the highlight video links for a given game
645
+
646
+ Example use:
647
+
648
+ Print the highlight links for the most recent Phillies game
649
+
650
+ print( statsapi.game_highlights( statsapi.last_game(143) ) )
651
+
652
+ Output (truncated to only include the first two highlights):
653
+
654
+ Hoskins' RBI double (00:00:16)
655
+ Rhys Hoskins belts a double off the left-center-field wall to score Bryce Harper and give the Phillies a 1-0 lead in the bottom of the 1st
656
+ https://cuts.diamond.mlb.com/FORGE/2019/2019-04/28/b1117503-3df11d8d-6df0dd65-csvm-diamondx64-asset_1280x720_59_4000K.mp4
657
+
658
+ Phanatic has birthday party (00:01:15)
659
+ Kids and fellow mascots were at Citizens Bank Park to celebrate the Phillie Phanatic's birthday before the game against the Marlins
660
+ https://cuts.diamond.mlb.com/FORGE/2019/2019-04/28/7d978385-db13f22d-f68c304f-csvm-diamondx64-asset_1280x720_59_4000K.mp4
661
+ """
662
+ r = get ('schedule' ,{'sportId' :1 ,'gamePk' :gamePk ,'hydrate' :'game(content(highlights(highlights)))' ,'fields' :'dates,date,games,gamePk,content,highlights,items,headline,type,value,title,description,duration,playbacks,name,url' })
663
+ if not len (r ['dates' ][0 ]['games' ][0 ]['content' ]['highlights' ]['highlights' ]['items' ]): return ''
664
+ items = r ['dates' ][0 ]['games' ][0 ]['content' ]['highlights' ]['highlights' ]['items' ]
665
+
666
+ highlights = ''
667
+ unorderedHighlights = {}
668
+ for v in (x for x in items if isinstance (x ,dict ) and x ['type' ]== 'video' ):
669
+ unorderedHighlights .update ({v ['date' ] : v })
670
+ sortedHighlights = []
671
+ for x in sorted (unorderedHighlights ):
672
+ sortedHighlights .append (unorderedHighlights [x ])
673
+ for a in sortedHighlights :
674
+ #if sum(1 for t in a['keywordsAll'] if t['type']=='team_id') == 1:
675
+ # highlights += next(t['displayName'] for t in a['keywordsAll'] if t['type']=='team_id') + '\n'
676
+ highlights += '{} ({})\n {}\n {}\n \n ' .format (a .get ('title' ,a .get ('headline' ,'' )), a ['duration' ], a .get ('description' ,'' ), next (s ['url' ] for s in a ['playbacks' ] if s ['name' ]== 'mp4Avc' ))
677
+
678
+ return highlights
679
+
680
+ def game_pace (season = datetime .now ().year ,sportId = 1 ):
681
+ """Get information about pace of game for a given season (back to 1999).
682
+
683
+ Example use:
684
+
685
+ Print the pace of game stats for 2008, in order to determine the number and average time of extra inning games
686
+
687
+ print(statsapi.game_pace(2008))
688
+
689
+ Output:
690
+
691
+ 2008 Game Pace Stats
692
+ hitsPer9Inn: 18.26
693
+ runsPer9Inn: 9.38
694
+ pitchesPer9Inn: 297.72
695
+ plateAppearancesPer9Inn: 77.89
696
+ hitsPerGame: 18.11
697
+ runsPerGame: 9.3
698
+ inningsPlayedPerGame: 8.96
699
+ pitchesPerGame: 295.36
700
+ pitchersPerGame: 7.83
701
+ plateAppearancesPerGame: 77.28
702
+ totalGameTime: 7086:06:00
703
+ totalInningsPlayed: 21748.0
704
+ totalHits: 43972
705
+ totalRuns: 22585
706
+ totalPlateAppearances: 187630
707
+ totalPitchers: 19012
708
+ totalPitches: 717131
709
+ totalGames: 2428
710
+ total9InnGames: 2428
711
+ totalExtraInnGames: 208
712
+ timePerGame: 02:55:07
713
+ timePerPitch: 00:00:36
714
+ timePerHit: 00:09:40
715
+ timePerRun: 00:18:50
716
+ timePerPlateAppearance: 00:02:16
717
+ timePer9Inn: 02:56:30
718
+ timePer77PlateAppearances: 02:54:29
719
+ totalExtraInnTime: 775:10:00
720
+ timePer7InnGameWithoutExtraInn: 00:00:00
721
+ total9InnGamesCompletedEarly: 3
722
+ total9InnGamesWithoutExtraInn: 2217
723
+ total9InnGamesScheduled: 2428
724
+ hitsPerRun: 1.947
725
+ pitchesPerPitcher: 37.72
726
+ total7InnGames: 3
727
+ total9InnGames: 2217
728
+ totalExtraInnGames: 208
729
+ timePer7InnGame: 01:54:40
730
+ timePer9InnGame: 02:50:38
731
+ timePerExtraInnGame: 03:43:36
732
+ """
733
+ params = {}
734
+ if season : params .update ({'season' :season })
735
+ if sportId : params .update ({'sportId' :sportId })
736
+
737
+ r = get ('gamePace' ,params )
738
+
739
+ if not len (r ['sports' ]):
740
+ raise ValueError ('No game pace info found for the {} season. Game pace data appears to begin in 1999.' .format (season ))
741
+
742
+ pace = ''
743
+
744
+ pace += '{} Game Pace Stats\n ' .format (season )
745
+ for s in r ['sports' ]:
746
+ for k in s .keys ():
747
+ if k in ['season' ,'sport' ]: continue
748
+ if k == 'prPortalCalculatedFields' :
749
+ for x in s [k ].keys ():
750
+ pace += '{}: {}\n ' .format (x ,s [k ][x ])
751
+ else : pace += '{}: {}\n ' .format (k ,s [k ])
752
+
753
+ return pace
754
+
624
755
def player_stats (personId ,group = '[hitting,pitching,fielding]' ,type = 'season' ):
625
756
"""Get current season or career stats for a given player.
626
757
0 commit comments