Skip to content

Commit

Permalink
enh: Add latest_season()
Browse files Browse the repository at this point in the history
enh: make lookup_player and lookup_team default to latest season #33
  • Loading branch information
toddrob99 committed Aug 10, 2022
1 parent 4c1683d commit cae21aa
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions statsapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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 = []
Expand Down

0 comments on commit cae21aa

Please sign in to comment.