From 6b6696cc89aa6a209c6e5ce5ac4cfcce6fa09e7a Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Wed, 3 Aug 2022 22:01:10 -0400 Subject: [PATCH 1/6] Add national broadcast data to schedule --- statsapi/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/statsapi/__init__.py b/statsapi/__init__.py index f79e6ec..8cbdcc9 100644 --- a/statsapi/__init__.py +++ b/statsapi/__init__.py @@ -77,7 +77,7 @@ def schedule( params.update( { "sportId": str(sportId), - "hydrate": "decisions,probablePitcher(note),linescore", + "hydrate": "decisions,probablePitcher(note),linescore,broadcasts", } ) @@ -121,6 +121,13 @@ def schedule( "inning_state": game.get("linescore", {}).get("inningState", ""), "venue_id": game.get("venue", {}).get("id"), "venue_name": game.get("venue", {}).get("name"), + "national_broadcasts": list( + set( + broadcast["name"] + for broadcast in game.get("broadcasts", []) + if broadcast.get("isNational", False) + ) + ), } if game_info["status"] in ["Final", "Game Over"]: if game.get("isTie"): From d543008b829711913604711b4ad95442aff1a721 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Wed, 3 Aug 2022 22:58:53 -0400 Subject: [PATCH 2/6] Also pick up MLB.tv free games --- statsapi/__init__.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/statsapi/__init__.py b/statsapi/__init__.py index 8cbdcc9..cd809d9 100644 --- a/statsapi/__init__.py +++ b/statsapi/__init__.py @@ -77,7 +77,7 @@ def schedule( params.update( { "sportId": str(sportId), - "hydrate": "decisions,probablePitcher(note),linescore,broadcasts", + "hydrate": "decisions,probablePitcher(note),linescore,broadcasts,game(content(media(epg)))", } ) @@ -129,6 +129,8 @@ def schedule( ) ), } + if game["content"].get("media", {}).get("freeGame", False): + game_info["national_broadcasts"].append("MLB.tv Free Game") if game_info["status"] in ["Final", "Game Over"]: if game.get("isTie"): game_info.update({"winning_team": "Tie", "losing_Team": "Tie"}) @@ -1346,20 +1348,8 @@ def standings( for div in divisions.values(): standings += div["div_name"] + "\n" if include_wildcard: - standings += ( - "{:^4} {:<21} {:^3} {:^3} {:^4} {:^4} {:^7} {:^5} {:^4}\n".format( - *[ - "Rank", - "Team", - "W", - "L", - "GB", - "(E#)", - "WC Rank", - "WC GB", - "(E#)", - ] - ) + standings += "{:^4} {:<21} {:^3} {:^3} {:^4} {:^4} {:^7} {:^5} {:^4}\n".format( + *["Rank", "Team", "W", "L", "GB", "(E#)", "WC Rank", "WC GB", "(E#)",] ) for t in div["teams"]: standings += "{div_rank:^4} {name:<21} {w:^3} {l:^3} {gb:^4} {elim_num:^4} {wc_rank:^7} {wc_gb:^5} {wc_elim_num:^4}\n".format( From 21ff015e47986321650337d383db24d4b4d85e29 Mon Sep 17 00:00:00 2001 From: todd Date: Tue, 9 Aug 2022 21:10:34 -0400 Subject: [PATCH 3/6] update formatting with black --- statsapi/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/statsapi/__init__.py b/statsapi/__init__.py index cd809d9..02f1954 100644 --- a/statsapi/__init__.py +++ b/statsapi/__init__.py @@ -1348,8 +1348,20 @@ def standings( for div in divisions.values(): standings += div["div_name"] + "\n" if include_wildcard: - standings += "{:^4} {:<21} {:^3} {:^3} {:^4} {:^4} {:^7} {:^5} {:^4}\n".format( - *["Rank", "Team", "W", "L", "GB", "(E#)", "WC Rank", "WC GB", "(E#)",] + standings += ( + "{:^4} {:<21} {:^3} {:^3} {:^4} {:^4} {:^7} {:^5} {:^4}\n".format( + *[ + "Rank", + "Team", + "W", + "L", + "GB", + "(E#)", + "WC Rank", + "WC GB", + "(E#)", + ] + ) ) for t in div["teams"]: standings += "{div_rank:^4} {name:<21} {w:^3} {l:^3} {gb:^4} {elim_num:^4} {wc_rank:^7} {wc_gb:^5} {wc_elim_num:^4}\n".format( From 8991668863711727598b29507be19255a12a8c3d Mon Sep 17 00:00:00 2001 From: todd Date: Tue, 9 Aug 2022 21:23:22 -0400 Subject: [PATCH 4/6] enh: add support for sportId to player_stat_data() #87 --- statsapi/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/statsapi/__init__.py b/statsapi/__init__.py index 02f1954..db5d08b 100644 --- a/statsapi/__init__.py +++ b/statsapi/__init__.py @@ -1120,11 +1120,19 @@ def player_stats(personId, group="[hitting,pitching,fielding]", type="season"): return stats -def player_stat_data(personId, group="[hitting,pitching,fielding]", type="season"): +def player_stat_data( + personId, group="[hitting,pitching,fielding]", type="season", sportId=1 +): """Returns a list of current season or career stat data for a given player.""" params = { "personId": personId, - "hydrate": "stats(group=" + group + ",type=" + type + "),currentTeam", + "hydrate": "stats(group=" + + group + + ",type=" + + type + + ",sportId=" + + str(sportId) + + "),currentTeam", } r = get("person", params) From 4c1683d19fe31e89758309b9797e682521f16501 Mon Sep 17 00:00:00 2001 From: todd Date: Tue, 9 Aug 2022 21:26:12 -0400 Subject: [PATCH 5/6] ver: 1.5 --- statsapi/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/statsapi/version.py b/statsapi/version.py index 72fb65a..12ede69 100644 --- a/statsapi/version.py +++ b/statsapi/version.py @@ -1,3 +1,3 @@ #!/usr/bin/env python -VERSION = "1.4.2" +VERSION = "1.5" From cae21aa70c23537d45f4719bb17c2bdf39199356 Mon Sep 17 00:00:00 2001 From: todd Date: Wed, 10 Aug 2022 13:39:13 -0400 Subject: [PATCH 6/6] enh: Add latest_season() enh: make lookup_player and lookup_team default to latest season #33 --- statsapi/__init__.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) 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 = []