Skip to content

Commit f563fcd

Browse files
committed
added lookup_player() and lookup_team()
1 parent 282ec04 commit f563fcd

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,41 @@ If you install manually, be sure to also install requests.
1515

1616
## Available Functions
1717

18-
* `statsapi.get()` - make calls directly to MLB StatsAPI endpoints;
19-
supports the most flexibility in request parameters, and returns raw json data
18+
* `statsapi.get()` - make calls directly to MLB StatsAPI endpoints; supports the most flexibility in request parameters, and returns raw json data
2019

21-
* `statsapi.meta()` - retrieve available values from StatsAPI for use in other queries,
22-
or look up descriptions for values found in API results
20+
* `statsapi.meta()` - retrieve available values from StatsAPI for use in other queries, or look up descriptions for values found in API results
2321

24-
* `statsapi.notes()` - retrieve notes for a given endpoint,
25-
including a list of required parameters, as well as hints for some endpoints
22+
* `statsapi.notes()` - retrieve notes for a given endpoint, including a list of required parameters, as well as hints for some endpoints
23+
24+
* `statsapi.lookup_player()` - get a list of player data based on first, last, or full name, jersey number, current team Id, position, etc.
25+
26+
* `statsapi.lookup_team()` - get a lsit of teams info based on the team name, city, abbreviation, or file code
2627

2728
* `statsapi.schedule()` - retrieve a list of games on a given date/range and/or team/opponent
2829

2930
* `statsapi.boxscore()` - generate a formatted boxscore for a given game
3031

3132
* `statsapi.linescore()` - generate a formatted linescore for a given game
3233

33-
* `statsapi.roster()` - generate a list of players on a team's roster
34+
* `statsapi.roster()` - generate a formatted list of players on a team's roster
3435

3536
* `statsapi.standings()` - generate a formatted list of standings for a given league/date
3637

37-
* `statsapi.team_leaders()` - generate a list of a team's leaders for a given stat
38+
* `statsapi.team_leaders()` - generate a formatted list of a team's leaders for a given stat
3839

39-
* `statsapi.league_leaders()` - generate a list of stat leaders for all-time (single season) or a given season
40+
* `statsapi.league_leaders()` - generate a formatted list of stat leaders for all-time (single season) or a given season
4041

41-
* `statsapi.player_stats()` - get a list of a player's career or season stats
42+
* `statsapi.player_stats()` - generate a formatted list of a player's career or season stats
4243

4344
* `statsapi.last_game()` - get the game id for the given team's most recent game
4445

4546
* `statsapi.next_game()` - get the game id for the given team's next game
4647

47-
* `statsapi.game_highlights()` - generate a list of highlights with video links for a given game
48+
* `statsapi.game_highlights()` - generate a formatted list of highlights with video links for a given game
4849

49-
* `statsapi.game_pace()` - get information about pace of game for a given season (back to 1999)
50+
* `statsapi.game_pace()` - generate a formatted list of pace of game information for a given season (back to 1999)
5051

51-
* `statsapi.game_scoring_plays()` - get a list of scoring plays for a given game
52+
* `statsapi.game_scoring_plays()` - generate a formatted list of scoring plays for a given game
5253

5354
## Example Use
5455

statsapi/__init__.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,87 @@ def player_stats(personId,group='[hitting,pitching,fielding]',type='season'):
883883

884884
return stats
885885

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+
886967
def team_leaders(teamId,leaderCategories,season=datetime.now().year,leaderGameTypes='R',limit=10):
887968
"""Get stat leaders for a given team.
888969

statsapi/endpoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@
859859
'required': True
860860
}
861861
},
862-
'query_params': ['season','gameType'],
862+
'query_params': ['season','gameType','fields'],
863863
'required_params': [['season']]
864864
},
865865
'standings': {

0 commit comments

Comments
 (0)