@@ -883,6 +883,87 @@ def player_stats(personId,group='[hitting,pitching,fielding]',type='season'):
883
883
884
884
return stats
885
885
886
+ def lookup_player (lookup_value ,gameType = 'R' ,season = datetime .now ().year ,sportId = 1 ):
887
+ """Get data about players based on first, last, or full name.
888
+
889
+ Example use:
890
+
891
+ Look up player id for Aaron Nola
892
+ Note: if using a full last name as the lookup_value and that last name could be part of another player's lastname,
893
+ e.g. 'Nola' is part of 'Nolan', include a comma on the end of the last name in order to match on the 'initLastName'
894
+ field which looks like 'Nola, A'
895
+
896
+ player = statsapi.lookup_player('nola,')
897
+ print(player[0]['id']) #assume only 1 record returned for demo purposes
898
+
899
+ Output:
900
+
901
+ 605400
902
+
903
+
904
+ Print full name and position for all players named Harper
905
+
906
+ for player in statsapi.lookup_player('Harper'):
907
+ print('Full name: {}, Position: {}'.format(player['fullName'], player['primaryPosition']['abbreviation']))
908
+
909
+ Output:
910
+
911
+ Full name: Bryce Harper, Position: RF
912
+ Full name: Ryne Harper, Position: P
913
+ """
914
+ params = {'gameType' :gameType , 'season' :season , 'sportId' :sportId , 'fields' :'people,id,fullName,firstName,lastName,primaryNumber,currentTeam,id,primaryPosition,code,abbreviation,useName,boxscoreName,nickName,mlbDebutDate,nameFirstLast,firstLastName,lastFirstName,lastInitName,initLastName,fullFMLName,fullLFMName' }
915
+ r = get ('sports_players' ,params )
916
+
917
+ players = []
918
+ for player in r ['people' ]:
919
+ for v in player .values ():
920
+ if str (lookup_value ).lower () in str (v ).lower ():
921
+ players .append (player )
922
+ break
923
+
924
+ return players
925
+
926
+ def lookup_team (lookup_value ,activeStatus = 'Y' ,season = datetime .now ().year ,sportIds = 1 ):
927
+ """Get a info about a team based on the team name, city, abbreviation, or file code.
928
+
929
+ Values for activeStatus: Y, N, B (Both)
930
+
931
+ Return value will be a list of teams matching the lookup_value.
932
+ If no matches are found, an empty list will be returned.
933
+
934
+ Example use:
935
+
936
+ Get teamId for team with code cwa
937
+
938
+ team = statsapi.lookup_team('chn')
939
+ print(team[0]['id']) #assume only 1 record returned for demo purposes
940
+
941
+ Output:
942
+
943
+ 112
944
+
945
+
946
+ Get info about all teams from NY
947
+
948
+ for team in statsapi.lookup_team('ny'):
949
+ print(team)
950
+
951
+ Output:
952
+
953
+ {'id': 147, 'name': 'New York Yankees', 'teamCode': 'nya', 'fileCode': 'nyy', 'teamName': 'Yankees', 'locationName': 'Bronx', 'shortName': 'NY Yankees'}
954
+ {'id': 121, 'name': 'New York Mets', 'teamCode': 'nyn', 'fileCode': 'nym', 'teamName': 'Mets', 'locationName': 'New York', 'shortName': 'NY Mets'}
955
+ """
956
+ params = {'activeStatus' :activeStatus , 'season' :season , 'sportIds' :sportIds , 'fields' :'teams,id,name,teamCode,fileCode,teamName,locationName,shortName' }
957
+ r = get ('teams' ,params )
958
+
959
+ teams = []
960
+ for team in r ['teams' ]:
961
+ for v in team .values ():
962
+ if str (lookup_value ).lower () in str (v ).lower ():
963
+ teams .append (team )
964
+ break
965
+ return teams
966
+
886
967
def team_leaders (teamId ,leaderCategories ,season = datetime .now ().year ,leaderGameTypes = 'R' ,limit = 10 ):
887
968
"""Get stat leaders for a given team.
888
969
0 commit comments