diff --git a/statsapi/__init__.py b/statsapi/__init__.py index db5d08b..c784e03 100644 --- a/statsapi/__init__.py +++ b/statsapi/__init__.py @@ -1167,14 +1167,37 @@ def player_stat_data( return player -def lookup_player(lookup_value, gameType="R", season=datetime.now().year, sportId=1): +def latest_season(sportId=1): + """Get the latest season for a given sportId. Returns a dict containing seasonId and various dates.""" + params = { + "sportId": sportId, + "seasonId": "all", + } + all_seasons = get("season", params) + + return all_seasons.get("seasons")[-1] + + +def lookup_player(lookup_value, gameType=None, season=None, sportId=1): """Get data about players based on first, last, or full name.""" 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", } + if gameType: + params.update( + { + "gameType": gameType, + } + ) + if not season: + season_data = latest_season(sportId=sportId) + season = season_data.get("seasonId", datetime.now().year) + params.update( + { + "season": season, + } + ) r = get("sports_players", params) players = [] @@ -1187,14 +1210,21 @@ def lookup_player(lookup_value, gameType="R", season=datetime.now().year, sportI return players -def lookup_team(lookup_value, activeStatus="Y", season=datetime.now().year, sportIds=1): +def lookup_team(lookup_value, activeStatus="Y", season=None, sportIds=1): """Get a info about a team or teams based on the team name, city, abbreviation, or file code.""" params = { "activeStatus": activeStatus, - "season": season, "sportIds": sportIds, "fields": "teams,id,name,teamCode,fileCode,teamName,locationName,shortName", } + if not season: + season_data = latest_season(sportId=sportIds.split(",")[0]) + season = season_data.get("seasonId", datetime.now().year) + params.update( + { + "season": season, + } + ) r = get("teams", params) teams = []