Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 39 additions & 21 deletions statsapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def schedule(
sportId=1,
game_id=None,
leagueId=None,
season=None,
include_series_status=True,
):
"""Get list of games for a given date/range and/or team/opponent."""
if end_date and not start_date:
Expand Down Expand Up @@ -78,16 +80,20 @@ def schedule(
if leagueId:
params.update({"leagueId": leagueId})

if season:
params.update({"season": season})

hydrate = (
"decisions,probablePitcher(note),linescore,broadcasts,game(content(media(epg)))"
)
if date == "2014-03-11" or (str(start_date) <= "2014-03-11" <= str(end_date)):
# For some reason the seriesStatus hydration throws a server error on 2014-03-11 only (checked back to 2000)
logger.warning(
"Excluding seriesStatus hydration because the MLB API throws an error for 2014-03-11 which is included in the requested date range."
)
else:
hydrate += ",seriesStatus"
if include_series_status:
if date == "2014-03-11" or (str(start_date) <= "2014-03-11" <= str(end_date)):
# For some reason the seriesStatus hydration throws a server error on 2014-03-11 only (checked back to 2000)
logger.warning(
"Excluding seriesStatus hydration because the MLB API throws an error for 2014-03-11 which is included in the requested date range."
)
else:
hydrate += ",seriesStatus"
params.update(
{
"sportId": str(sportId),
Expand Down Expand Up @@ -1116,9 +1122,11 @@ def game_pace_data(season=datetime.now().year, sportId=1):
return r


def player_stats(personId, group="[hitting,pitching,fielding]", type="season"):
def player_stats(
personId, group="[hitting,pitching,fielding]", type="season", season=None
):
"""Get current season or career stats for a given player."""
player = player_stat_data(personId, group, type)
player = player_stat_data(personId, group, type, season)

stats = ""
stats += player["first_name"]
Expand Down Expand Up @@ -1154,15 +1162,22 @@ def player_stats(personId, group="[hitting,pitching,fielding]", type="season"):


def player_stat_data(
personId, group="[hitting,pitching,fielding]", type="season", sportId=1
personId, group="[hitting,pitching,fielding]", type="season", sportId=1, season=None
):
"""Returns a list of current season or career stat data for a given player."""

if season is not None and "season" not in type:
raise ValueError(
"The 'season' parameter is only valid when using the 'season' type."
)

params = {
"personId": personId,
"hydrate": "stats(group="
+ group
+ ",type="
+ type
+ (",season=" + str(season) if season else "")
+ ",sportId="
+ str(sportId)
+ "),currentTeam",
Expand Down Expand Up @@ -1209,15 +1224,9 @@ def latest_season(sportId=1):
all_seasons = get("season", params)
return next(
(
x
for x in all_seasons.get("seasons", [])
if x.get("seasonStartDate")
and x.get("seasonEndDate")
and (
x["seasonStartDate"]
< datetime.today().strftime("%Y-%m-%d")
< x["seasonEndDate"]
)
s
for s in all_seasons.get("seasons", [])
if (datetime.today().strftime("%Y-%m-%d") < s.get("seasonEndDate", ""))
),
all_seasons["seasons"][-1],
)
Expand Down Expand Up @@ -1574,6 +1583,7 @@ def meta(type, fields=None):
"awards",
"baseballStats",
"eventTypes",
"freeGameTypes",
"gameStatus",
"gameTypes",
"hitTrajectories",
Expand All @@ -1588,12 +1598,15 @@ def meta(type, fields=None):
"positions",
"reviewReasons",
"rosterTypes",
"runnerDetailTypes",
"scheduleTypes",
"scheduleEventTypes",
"situationCodes",
"sky",
"standingsTypes",
"statGroups",
"statTypes",
"violationTypes",
"windDirection",
]
if type not in types:
Expand Down Expand Up @@ -1640,7 +1653,7 @@ def notes(endpoint):
return msg


def get(endpoint, params={}, force=False):
def get(endpoint, params={}, force=False, *, request_kwargs={}):
"""Call MLB StatsAPI and return JSON data.

This function is for advanced querying of the MLB StatsAPI,
Expand Down Expand Up @@ -1763,8 +1776,13 @@ def get(endpoint, params={}, force=False):
+ note
)

if len(request_kwargs):
logger.debug(
"Including request_kwargs in requests.get call: {}".format(request_kwargs)
)

# Make the request
r = requests.get(url)
r = requests.get(url, **request_kwargs)
if r.status_code not in [200, 201]:
r.raise_for_status()
else:
Expand Down
39 changes: 39 additions & 0 deletions statsapi/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,25 @@
"query_params": ["timecode", "fields"],
"required_params": [[]],
},
"game_uniforms": {
"url": BASE_URL + "{ver}/uniforms/game",
"path_params": {
"ver": {
"type": "str",
"default": "v1",
"leading_slash": False,
"trailing_slash": False,
"required": True,
}
},
"query_params": [
"gamePks",
"fields",
],
"required_params": [
["gamePks"],
],
},
"gamePace": {
"url": BASE_URL + "{ver}/gamePace",
"path_params": {
Expand Down Expand Up @@ -1285,6 +1304,26 @@
"required_params": [["season", "group"]],
"note": "Use meta('statGroups') to look up valid values for group, meta('statTypes') for valid values for stats, and meta('situationCodes') for valid values for sitCodes. Use sitCodes with stats=statSplits.",
},
"team_uniforms": {
"url": BASE_URL + "{ver}/uniforms/team",
"path_params": {
"ver": {
"type": "str",
"default": "v1",
"leading_slash": False,
"trailing_slash": False,
"required": True,
}
},
"query_params": [
"teamIds",
"season",
"fields",
],
"required_params": [
["teamIds"],
],
},
"transactions": {
"url": BASE_URL + "{ver}/transactions",
"path_params": {
Expand Down
2 changes: 1 addition & 1 deletion statsapi/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python

VERSION = "1.8.1"
VERSION = "1.9.0"