Skip to content

Commit

Permalink
Hotfix to handle Winter League stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
armstjc committed Jan 4, 2025
1 parent bc7aa5b commit c6e4a09
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
47 changes: 37 additions & 10 deletions get_milb_player_game_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import platform
import random
import time
from datetime import datetime
from urllib.request import urlopen
Expand Down Expand Up @@ -79,7 +80,7 @@ def get_milb_player_game_stats(game_id: int, cache_data=False, cache_dir=""):

json_data = json.loads(json_string)
except Exception:
response = urlopen(game_url)
response = urlopen(game_url, timeout=30)
time.sleep(1)

if response.code == 200:
Expand Down Expand Up @@ -687,14 +688,15 @@ def get_month_milb_player_game_stats(
# for this level is downloaded.
end_month = 13

parser = argparse.ArgumentParser()
parser.add_argument("--season", type=int, required=False)
parser.add_argument("--level", type=str, required=True)
args = parser.parse_args()
lg_level = args.level

season = args.season
# parser = argparse.ArgumentParser()
# parser.add_argument("--season", type=int, required=False)
# parser.add_argument("--level", type=str, required=True)
# args = parser.parse_args()

# lg_level = args.level
# season = args.season
lg_level = "winter"
season = 2024
if season is None:
season == now.year

Expand Down Expand Up @@ -739,17 +741,42 @@ def get_month_milb_player_game_stats(
f"Getting {i}/{season} player game stats data " +
f"in the {lg_level} level of MiLB."
)
get_month_milb_player_game_stats(
df = get_month_milb_player_game_stats(
season, i, level=lg_level, cache_data=True, cache_dir=c_dir
)
else:
print(
f"Getting {i}/{season} player game stats data " +
f"in the {lg_level} level of MiLB."
)
get_month_milb_player_game_stats(season, i, level=lg_level)
df = get_month_milb_player_game_stats(season, i, level=lg_level)
# get_month_milb_player_game_stats(season, i, level=lg_level)

if len(df) == 0:
season -= random.randint(1, 10)
schedule_df = load_milb_schedule(
season=season,
level=lg_level
)
random_int = random.randint(0, len(schedule_df))
game_date = schedule_df["official_date"].iloc[random_int]
game_month = int(game_date.split("-")[1])

if platform.system() == "Windows":
print(
f"Getting {i}/{season} player game stats data " +
f"in the {lg_level} level of MiLB."
)
df = get_month_milb_player_game_stats(
season, i, level=lg_level, cache_data=True, cache_dir=c_dir
)
else:
print(
f"Getting {i}/{season} player game stats data " +
f"in the {lg_level} level of MiLB."
)
df = get_month_milb_player_game_stats(season, i, level=lg_level)

# get_month_milb_player_game_stats(
# 2023, 10, level="win", cache_data=True, cache_dir="D:/"
# )
2 changes: 1 addition & 1 deletion get_milb_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ def load_milb_schedule(
f"download/schedule/{season}_rookie_schedule.csv"
df = pd.read_csv(url)
return df
elif level.lower() == "win":
elif (level.lower() == "win" or level.lower() == "winter"):
url = "https://github.com/armstjc/milb-data-repository/releases/" +\
f"download/schedule/{season}_winter_schedule.csv"
df = pd.read_csv(url)
Expand Down

0 comments on commit c6e4a09

Please sign in to comment.