@@ -630,33 +630,149 @@ def player_stats(personId):
630
630
631
631
return "This function is not yet available."
632
632
633
- def team_leaders (leaderCategories ,teamId = None , season = None ):
633
+ def team_leaders (teamId , leaderCategories ,season = datetime . now (). year , leaderGameTypes = 'R' , limit = 10 ):
634
634
"""Get stat leaders for a given team.
635
+
635
636
Get a list of available leaderCategories by calling the meta endpoint with type=leagueLeaderTypes
637
+
638
+ Example use:
639
+
640
+ Print the top 5 team leaders in walks for the 2008 Phillies
641
+
642
+ print(statsapi.team_leaders(143,'walks',limit=5,season=2008))
643
+
644
+ Output:
645
+
646
+ Rank Name Value
647
+ 1 Pat Burrell 102
648
+ 2 Ryan Howard 81
649
+ 3 Chase Utley 64
650
+ 4 Jimmy Rollins 58
651
+ 5 Jayson Werth 57
636
652
"""
637
- if not season : season = datetime .now ().year
638
- params = {'leaderCategories' :leaderCategories ,'season' : season }
639
- if teamId : params .update ({'teamId' :teamId })
653
+ params = {'leaderCategories' :leaderCategories ,'season' :season ,'teamId' :teamId ,'leaderGameTypes' :leaderGameTypes ,'limit' :limit }
654
+ params .update ({'fields' : 'teamLeaders,leaders,rank,value,person,fullName' })
640
655
641
656
r = get ('team_leaders' ,params )
642
657
643
-
658
+ leaders = ''
659
+ lines = []
660
+ for player in [x for x in r ['teamLeaders' ][0 ]['leaders' ]]:
661
+ lines .append ([player ['rank' ],player ['person' ]['fullName' ],player ['value' ]])
644
662
645
- return "This function is not yet available."
663
+ leaders += '{:<4} {:<20} {:<5}\n ' .format (* ['Rank' ,'Name' ,'Value' ])
664
+ for a in lines :
665
+ leaders += '{:^4} {:<20} {:^5}\n ' .format (* a )
666
+
667
+ return leaders
668
+
669
+ def league_leaders (leaderCategories ,season = None ,limit = 10 ,statGroup = None ,leagueId = None ,gameTypes = None ,playerPool = None ,sportId = 1 ):
670
+ """Get stat leaders overall or for a given league (103=AL, 104=NL).
646
671
647
- def league_leaders (leaderCategories ,leagueId = None ,season = None ):
648
- """Get stat leaders for a given team.
649
672
Get a list of available leaderCategories by calling the meta endpoint with type=leagueLeaderTypes
673
+
674
+ Get a list of available statGroups by calling the meta endpoint with type=statGroups
675
+ Note that excluding statGroup may return unexpected results. For example leaderCategories='earnedRunAverage'
676
+ will return different results with statGroup='pitching' and statGroup='catching'.
677
+
678
+ Get a list of available gameTypes by calling the meta endpoint with type=gameTypes
679
+
680
+ Available playerPool values: ['all','qualified','rookies'] (default is qualified)
681
+
682
+ Example use:
683
+
684
+ Print a list of the top 10 pitchers by earned run average
685
+
686
+ print( statsapi.league_leaders('earnedRunAverage',statGroup='pitching',limit=10) )
687
+
688
+ Output:
689
+
690
+ Rank Name Team Value
691
+ 1 Luis Castillo Cincinnati Reds 1.23
692
+ 2 Marcus Stroman Toronto Blue Jays 1.43
693
+ 3 Matt Shoemaker Toronto Blue Jays 1.57
694
+ 4 Chris Paddack San Diego Padres 1.67
695
+ 5 Tyler Glasnow Tampa Bay Rays 1.75
696
+ 6 Trevor Bauer Cleveland Indians 1.99
697
+ 7 Joe Musgrove Pittsburgh Pirates 2.06
698
+ 8 Caleb Smith Miami Marlins 2.17
699
+ 9 Max Fried Atlanta Braves 2.30
700
+ 10 Aaron Sanchez Toronto Blue Jays 2.32
701
+
702
+
703
+ Print a list of the top 5 batters by OPS
704
+
705
+ print( statsapi.league_leaders('onBasePlusSlugging',statGroup='hitting',limit=5) )
706
+
707
+ Output:
708
+
709
+ Rank Name Team Value
710
+ 1 Cody Bellinger Los Angeles Dodgers 1.390
711
+ 2 Christian Yelich Milwaukee Brewers 1.264
712
+ 3 Anthony Rendon Washington Nationals 1.182
713
+ 4 Hunter Dozier Kansas City Royals 1.143
714
+ 5 Pete Alonso New York Mets 1.082
715
+
716
+
717
+ Print a list of the 10 American League players with the most errors in 2017
718
+
719
+ print( statsapi.league_leaders('errors',statGroup='fielding',limit=10,season=2017,leagueId=103) )
720
+
721
+ Output:
722
+
723
+ Rank Name Team Value
724
+ 1 Tim Anderson Chicago White Sox 28
725
+ 2 Rougned Odor Texas Rangers 19
726
+ 3 Tim Beckham Baltimore Orioles 18
727
+ 3 Nicholas Castellanos Detroit Tigers 18
728
+ 3 Jorge Polanco Minnesota Twins 18
729
+ 6 Elvis Andrus Texas Rangers 17
730
+ 6 Xander Bogaerts Boston Red Sox 17
731
+ 6 Jean Segura Seattle Mariners 17
732
+ 9 Alcides Escobar Kansas City Royals 15
733
+ 9 Jonathan Schoop Baltimore Orioles 15
734
+
735
+
736
+ Print a list of top 10 all time single season leader in triples
737
+
738
+ print( statsapi.league_leaders('triples',statGroup='hitting',limit=10,sportId=1) )
739
+
740
+ Rank Name Team Value
741
+ 1 Chief Wilson Pittsburgh Pirates 36
742
+ 2 Jimmy Williams Pittsburgh Pirates 27
743
+ 3 Sam Crawford Detroit Tigers 26
744
+ 3 Kiki Cuyler Pittsburgh Pirates 26
745
+ 3 Joe Jackson Cleveland Naps 26
746
+ 6 Sam Crawford Detroit Tigers 25
747
+ 6 Larry Doyle New York Giants 25
748
+ 6 Buck Freeman Washington Senators 25
749
+ 6 Tom Long St. Louis Cardinals 25
750
+ 10 Ty Cobb Detroit Tigers 24
751
+ 10 Ty Cobb Detroit Tigers 24
650
752
"""
651
- if not season : season = datetime .now ().year
652
- params = {'leaderCategories' :leaderCategories ,'season' :season }
753
+ params = {'leaderCategories' :leaderCategories ,'sportId' :sportId ,'limit' :limit }
754
+ if season : params .update ({'season' :season })
755
+ else : params .update ({'statType' :'statsSingleSeason' }) #won't get any results for all-time leaders unless type is single season
756
+ if statGroup :
757
+ if statGroup == 'batting' : statGroup = 'hitting'
758
+ params .update ({'statGroup' :statGroup })
759
+ if gameTypes : params .update ({'leaderGameTypes' :gameTypes })
653
760
if leagueId : params .update ({'leagueId' :leagueId })
761
+ if playerPool : params .update ({'playerPool' :playerPool })
762
+ params .update ({'fields' : 'leagueLeaders,leaders,rank,value,team,name,league,name,person,fullName' })
654
763
655
- r = get ('stats_leaders ' ,params )
764
+ r = get ('league_leaders ' ,params )
656
765
657
-
766
+ leaders = ''
767
+ lines = []
768
+ for player in [x for x in r ['leagueLeaders' ][0 ]['leaders' ]]:
769
+ lines .append ([player ['rank' ],player ['person' ]['fullName' ],player ['team' ].get ('name' ,'' ),player ['value' ]])
658
770
659
- return "This function is not yet available."
771
+ leaders += '{:<4} {:<20} {:<23} {:<5}\n ' .format (* ['Rank' ,'Name' ,'Team' ,'Value' ])
772
+ for a in lines :
773
+ leaders += '{:^4} {:<20} {:<23} {:^5}\n ' .format (* a )
774
+
775
+ return leaders
660
776
661
777
def standings (leagueId = None ,season = None ,standingsTypes = None ,date = None ):
662
778
"""Get formatted standings for a given league and season.
@@ -742,7 +858,7 @@ def standings(leagueId=None,season=None,standingsTypes=None,date=None):
742
858
743
859
return standings
744
860
745
- def roster (teamId ,rosterType = None ,season = None ,date = None ):
861
+ def roster (teamId ,rosterType = None ,season = datetime . now (). year ,date = None ):
746
862
"""Get the roster for a given team.
747
863
Get a list of available rosterTypes by calling the meta endpoint with type=rosterTypes.
748
864
Default rosterType=active
@@ -782,7 +898,6 @@ def roster(teamId,rosterType=None,season=None,date=None):
782
898
#21 P Vince Velasquez
783
899
#56 P Zach Eflin
784
900
"""
785
- if not season : season = datetime .now ().year
786
901
if not rosterType : rosterType = 'active'
787
902
params = {'rosterType' :rosterType ,'season' :season ,'teamId' :teamId }
788
903
if date : params .update ({'date' :date })
@@ -938,7 +1053,7 @@ def get(endpoint,params,force=False):
938
1053
if len (missing_params ) == 0 :
939
1054
satisfied = True
940
1055
break
941
- if not satisfied :
1056
+ if not satisfied and not force :
942
1057
if ep .get ('note' ):
943
1058
note = '\n --Endpoint note: ' + ep .get ('note' )
944
1059
else : note = ''
0 commit comments