Spaces:
Sleeping
Sleeping
Update data/statcast.py
Browse files- data/statcast.py +70 -66
data/statcast.py
CHANGED
|
@@ -13,75 +13,79 @@ HEADERS = {
|
|
| 13 |
}
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def fetch_statcast_range(start_date: str, end_date: str) -> pd.DataFrame:
|
| 17 |
"""
|
| 18 |
-
WBC-first
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"hfBBT": "",
|
| 32 |
-
"hfPR": "",
|
| 33 |
-
"hfZ": "",
|
| 34 |
-
"stadium": "",
|
| 35 |
-
"hfBBL": "",
|
| 36 |
-
"hfNewZones": "",
|
| 37 |
-
"hfGT": "F|D|L|W|",
|
| 38 |
-
"hfC": "",
|
| 39 |
-
"hfSea": f"{season}|",
|
| 40 |
-
"hfSit": "",
|
| 41 |
-
"player_type": "batter",
|
| 42 |
-
"hfOuts": "",
|
| 43 |
-
"opponent": "",
|
| 44 |
-
"pitcher_throws": "",
|
| 45 |
-
"batter_stands": "",
|
| 46 |
-
"hfSA": "",
|
| 47 |
-
"game_date_gt": start_date,
|
| 48 |
-
"game_date_lt": end_date,
|
| 49 |
-
"team": "",
|
| 50 |
-
"position": "",
|
| 51 |
-
"hfRO": "",
|
| 52 |
-
"home_road": "",
|
| 53 |
-
"hfFlag": "",
|
| 54 |
-
"metric_1": "",
|
| 55 |
-
"hfInn": "",
|
| 56 |
-
"min_pitches": "0",
|
| 57 |
-
"min_results": "0",
|
| 58 |
-
"group_by": "name",
|
| 59 |
-
"sort_col": "pitches",
|
| 60 |
-
"player_event_sort": "api_h_launch_speed",
|
| 61 |
-
"sort_order": "desc",
|
| 62 |
-
"min_abs": "0",
|
| 63 |
-
"type": "details",
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
response = requests.get(
|
| 67 |
-
STATCAST_SEARCH_URL,
|
| 68 |
-
params=params,
|
| 69 |
-
headers=HEADERS,
|
| 70 |
-
timeout=60,
|
| 71 |
-
)
|
| 72 |
-
response.raise_for_status()
|
| 73 |
-
|
| 74 |
-
text = response.text.strip()
|
| 75 |
-
if not text or text.startswith("<!DOCTYPE html"):
|
| 76 |
-
continue
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
df = pd.read_csv(StringIO(text))
|
| 80 |
-
except Exception:
|
| 81 |
-
continue
|
| 82 |
-
|
| 83 |
-
if not df.empty:
|
| 84 |
-
return df
|
| 85 |
|
| 86 |
return pd.DataFrame()
|
| 87 |
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
|
| 16 |
+
def _query_statcast(start_date: str, end_date: str, season: str) -> pd.DataFrame:
|
| 17 |
+
params = {
|
| 18 |
+
"all": "true",
|
| 19 |
+
"hfPT": "",
|
| 20 |
+
"hfAB": "",
|
| 21 |
+
"hfBBT": "",
|
| 22 |
+
"hfPR": "",
|
| 23 |
+
"hfZ": "",
|
| 24 |
+
"stadium": "",
|
| 25 |
+
"hfBBL": "",
|
| 26 |
+
"hfNewZones": "",
|
| 27 |
+
"hfGT": "F|D|L|W|",
|
| 28 |
+
"hfC": "",
|
| 29 |
+
"hfSea": f"{season}|",
|
| 30 |
+
"hfSit": "",
|
| 31 |
+
"player_type": "batter",
|
| 32 |
+
"hfOuts": "",
|
| 33 |
+
"opponent": "",
|
| 34 |
+
"pitcher_throws": "",
|
| 35 |
+
"batter_stands": "",
|
| 36 |
+
"hfSA": "",
|
| 37 |
+
"game_date_gt": start_date,
|
| 38 |
+
"game_date_lt": end_date,
|
| 39 |
+
"team": "",
|
| 40 |
+
"position": "",
|
| 41 |
+
"hfRO": "",
|
| 42 |
+
"home_road": "",
|
| 43 |
+
"hfFlag": "",
|
| 44 |
+
"metric_1": "",
|
| 45 |
+
"hfInn": "",
|
| 46 |
+
"min_pitches": "0",
|
| 47 |
+
"min_results": "0",
|
| 48 |
+
"group_by": "name",
|
| 49 |
+
"sort_col": "pitches",
|
| 50 |
+
"player_event_sort": "api_h_launch_speed",
|
| 51 |
+
"sort_order": "desc",
|
| 52 |
+
"min_abs": "0",
|
| 53 |
+
"type": "details",
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
response = requests.get(
|
| 57 |
+
STATCAST_SEARCH_URL,
|
| 58 |
+
params=params,
|
| 59 |
+
headers=HEADERS,
|
| 60 |
+
timeout=60,
|
| 61 |
+
)
|
| 62 |
+
response.raise_for_status()
|
| 63 |
+
|
| 64 |
+
text = response.text.strip()
|
| 65 |
+
if not text or text.startswith("<!DOCTYPE html"):
|
| 66 |
+
return pd.DataFrame()
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
return pd.read_csv(StringIO(text))
|
| 70 |
+
except Exception:
|
| 71 |
+
return pd.DataFrame()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
def fetch_statcast_range(start_date: str, end_date: str) -> pd.DataFrame:
|
| 75 |
"""
|
| 76 |
+
WBC-first pull.
|
| 77 |
+
Try current 2026 range first.
|
| 78 |
+
If that returns nothing, fall back to 2023 WBC historical window so the
|
| 79 |
+
analytics tabs still show real WBC Statcast data instead of blank charts.
|
| 80 |
"""
|
| 81 |
+
current_df = _query_statcast(start_date, end_date, season="2026")
|
| 82 |
+
if not current_df.empty:
|
| 83 |
+
return current_df
|
| 84 |
+
|
| 85 |
+
# Historical WBC fallback: real WBC data, not mock data
|
| 86 |
+
fallback_df = _query_statcast("2023-03-07", "2023-03-23", season="2023")
|
| 87 |
+
if not fallback_df.empty:
|
| 88 |
+
return fallback_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
return pd.DataFrame()
|
| 91 |
|