Update main.py
Browse files
main.py
CHANGED
|
@@ -544,8 +544,8 @@ def clean_team_name(team_name):
|
|
| 544 |
if pd.isna(team_name):
|
| 545 |
return team_name
|
| 546 |
team_name = str(team_name).strip()
|
| 547 |
-
team_name = re.sub(r'\s*\(\d+\)$', '', team_name)
|
| 548 |
-
team_name = team_name.replace('*', '')
|
| 549 |
return team_name
|
| 550 |
|
| 551 |
def is_data_stale(timestamp_str, max_age_hours=24):
|
|
@@ -573,9 +573,11 @@ def get_team_standings_brscraper(year):
|
|
| 573 |
logging.info(f"Scraping team standings for {year} (cache stale or not found).")
|
| 574 |
df = _scrape_team_standings_brscraper(year)
|
| 575 |
if not df.empty:
|
|
|
|
|
|
|
| 576 |
db_ref.set({
|
| 577 |
'last_updated': datetime.utcnow().isoformat(),
|
| 578 |
-
'data':
|
| 579 |
})
|
| 580 |
logging.info(f"Team standings for {year} saved to Firebase cache.")
|
| 581 |
return df
|
|
@@ -585,7 +587,7 @@ def _scrape_team_standings_brscraper(year):
|
|
| 585 |
logging.error("BRScraper not available for team standings.")
|
| 586 |
return pd.DataFrame()
|
| 587 |
try:
|
| 588 |
-
df = nba.get_standings(year, info='total')
|
| 589 |
if df.empty:
|
| 590 |
logging.warning(f"Could not find team standings for {year} using BRScraper.")
|
| 591 |
return pd.DataFrame()
|
|
@@ -597,7 +599,7 @@ def _scrape_team_standings_brscraper(year):
|
|
| 597 |
df = df.rename(columns={old_col: new_col for old_col, new_col in column_mapping.items() if old_col in df.columns})
|
| 598 |
|
| 599 |
if 'Team' in df.columns:
|
| 600 |
-
df['Team'] = df['Team'].astype(str)
|
| 601 |
df['Team'] = df['Team'].apply(clean_team_name)
|
| 602 |
|
| 603 |
numeric_cols = [col for col in df.columns if col not in ['Team']]
|
|
@@ -969,7 +971,6 @@ def get_team_stats():
|
|
| 969 |
if tm_df.empty:
|
| 970 |
return jsonify({'error': f'No team data available for {selected_season_str}'}), 404
|
| 971 |
|
| 972 |
-
# Map input abbreviations to full names for lookup
|
| 973 |
full_team_names_map = {
|
| 974 |
"ATL": "Atlanta Hawks", "BOS": "Boston Celtics", "BRK": "Brooklyn Nets",
|
| 975 |
"CHO": "Charlotte Hornets", "CHI": "Chicago Bulls", "CLE": "Cleveland Cavaliers",
|
|
@@ -994,7 +995,6 @@ def get_team_stats():
|
|
| 994 |
df_dict['Season'] = selected_season_str
|
| 995 |
stats.append(df_dict)
|
| 996 |
else:
|
| 997 |
-
# Add original abbreviation to not_found list
|
| 998 |
original_abbr = next((abbr for abbr, name in full_team_names_map.items() if name == team_full_name_lookup), team_full_name_lookup)
|
| 999 |
teams_with_no_data.append(original_abbr)
|
| 1000 |
|
|
|
|
| 544 |
if pd.isna(team_name):
|
| 545 |
return team_name
|
| 546 |
team_name = str(team_name).strip()
|
| 547 |
+
team_name = re.sub(r'\s*\(\d+\)$', '', team_name)
|
| 548 |
+
team_name = team_name.replace('*', '')
|
| 549 |
return team_name
|
| 550 |
|
| 551 |
def is_data_stale(timestamp_str, max_age_hours=24):
|
|
|
|
| 573 |
logging.info(f"Scraping team standings for {year} (cache stale or not found).")
|
| 574 |
df = _scrape_team_standings_brscraper(year)
|
| 575 |
if not df.empty:
|
| 576 |
+
# Apply clean_df_for_firebase before saving to cache
|
| 577 |
+
df_cleaned_for_firebase = clean_df_for_firebase(df.copy()) # Use a copy to avoid modifying original df if it's used later
|
| 578 |
db_ref.set({
|
| 579 |
'last_updated': datetime.utcnow().isoformat(),
|
| 580 |
+
'data': df_cleaned_for_firebase.to_dict(orient='records')
|
| 581 |
})
|
| 582 |
logging.info(f"Team standings for {year} saved to Firebase cache.")
|
| 583 |
return df
|
|
|
|
| 587 |
logging.error("BRScraper not available for team standings.")
|
| 588 |
return pd.DataFrame()
|
| 589 |
try:
|
| 590 |
+
df = nba.get_standings(year, info='total')
|
| 591 |
if df.empty:
|
| 592 |
logging.warning(f"Could not find team standings for {year} using BRScraper.")
|
| 593 |
return pd.DataFrame()
|
|
|
|
| 599 |
df = df.rename(columns={old_col: new_col for old_col, new_col in column_mapping.items() if old_col in df.columns})
|
| 600 |
|
| 601 |
if 'Team' in df.columns:
|
| 602 |
+
df['Team'] = df['Team'].astype(str)
|
| 603 |
df['Team'] = df['Team'].apply(clean_team_name)
|
| 604 |
|
| 605 |
numeric_cols = [col for col in df.columns if col not in ['Team']]
|
|
|
|
| 971 |
if tm_df.empty:
|
| 972 |
return jsonify({'error': f'No team data available for {selected_season_str}'}), 404
|
| 973 |
|
|
|
|
| 974 |
full_team_names_map = {
|
| 975 |
"ATL": "Atlanta Hawks", "BOS": "Boston Celtics", "BRK": "Brooklyn Nets",
|
| 976 |
"CHO": "Charlotte Hornets", "CHI": "Chicago Bulls", "CLE": "Cleveland Cavaliers",
|
|
|
|
| 995 |
df_dict['Season'] = selected_season_str
|
| 996 |
stats.append(df_dict)
|
| 997 |
else:
|
|
|
|
| 998 |
original_abbr = next((abbr for abbr, name in full_team_names_map.items() if name == team_full_name_lookup), team_full_name_lookup)
|
| 999 |
teams_with_no_data.append(original_abbr)
|
| 1000 |
|