Update main.py
Browse files
main.py
CHANGED
|
@@ -15,7 +15,7 @@ from firebase_admin import credentials, db, storage, auth
|
|
| 15 |
import firebase_admin
|
| 16 |
import logging
|
| 17 |
import traceback
|
| 18 |
-
from bs4 import BeautifulSoup, Comment # BeautifulSoup is still
|
| 19 |
|
| 20 |
# Import BRScraper
|
| 21 |
try:
|
|
@@ -559,7 +559,27 @@ def admin_update_credits(uid):
|
|
| 559 |
# NBA Analytics Hub Data Fetching Utilities
|
| 560 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 561 |
|
| 562 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
|
| 564 |
def clean_team_name(team_name):
|
| 565 |
"""
|
|
@@ -786,6 +806,7 @@ def _scrape_dashboard_info_brscraper():
|
|
| 786 |
# MVP Votings for 2025
|
| 787 |
mvp_2025_df = nba.get_award_votings('mvp', 2025)
|
| 788 |
if not mvp_2025_df.empty:
|
|
|
|
| 789 |
dashboard_data['mvp_2025_votings'] = mvp_2025_df.replace({np.nan: None}).to_dict(orient='records')
|
| 790 |
else:
|
| 791 |
dashboard_data['mvp_2025_votings'] = []
|
|
@@ -794,6 +815,7 @@ def _scrape_dashboard_info_brscraper():
|
|
| 794 |
# Current Season Playoff Probabilities (East)
|
| 795 |
east_probs_df = nba.get_playoffs_probs('east')
|
| 796 |
if not east_probs_df.empty:
|
|
|
|
| 797 |
dashboard_data['playoff_probs_east'] = east_probs_df.replace({np.nan: None}).to_dict(orient='records')
|
| 798 |
else:
|
| 799 |
dashboard_data['playoff_probs_east'] = []
|
|
@@ -802,6 +824,7 @@ def _scrape_dashboard_info_brscraper():
|
|
| 802 |
# Current Season Playoff Probabilities (West)
|
| 803 |
west_probs_df = nba.get_playoffs_probs('west')
|
| 804 |
if not west_probs_df.empty:
|
|
|
|
| 805 |
dashboard_data['playoff_probs_west'] = west_probs_df.replace({np.nan: None}).to_dict(orient='records')
|
| 806 |
else:
|
| 807 |
dashboard_data['playoff_probs_west'] = []
|
|
|
|
| 15 |
import firebase_admin
|
| 16 |
import logging
|
| 17 |
import traceback
|
| 18 |
+
from bs4 import BeautifulSoup, Comment # BeautifulSoup is still imported but not used for team stats now.
|
| 19 |
|
| 20 |
# Import BRScraper
|
| 21 |
try:
|
|
|
|
| 559 |
# NBA Analytics Hub Data Fetching Utilities
|
| 560 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 561 |
|
| 562 |
+
# Helper to clean DataFrame column names for Firebase keys
|
| 563 |
+
def clean_firebase_keys(key_name):
|
| 564 |
+
"""Cleans a string to be a valid Firebase Realtime Database key."""
|
| 565 |
+
if not isinstance(key_name, str):
|
| 566 |
+
key_name = str(key_name)
|
| 567 |
+
# Firebase invalid characters: . $ # [ ] /
|
| 568 |
+
# Replace them with underscores or remove them
|
| 569 |
+
cleaned_key = key_name.replace('.', '_').replace('$', '').replace('#', '').replace('[', '').replace(']', '').replace('/', '_')
|
| 570 |
+
# Also handle '%' if it's problematic, though not explicitly listed by Firebase for keys
|
| 571 |
+
cleaned_key = cleaned_key.replace('%', 'Pct') # Example: W/L% -> W_LPct
|
| 572 |
+
# Ensure no empty keys
|
| 573 |
+
if not cleaned_key:
|
| 574 |
+
return "empty_key_" + str(uuid.uuid4())[:8] # Fallback for completely empty keys
|
| 575 |
+
return cleaned_key
|
| 576 |
+
|
| 577 |
+
def clean_df_for_firebase(df):
|
| 578 |
+
"""Applies Firebase key cleaning to all column names of a DataFrame."""
|
| 579 |
+
if df.empty:
|
| 580 |
+
return df
|
| 581 |
+
df.columns = [clean_firebase_keys(col) for col in df.columns]
|
| 582 |
+
return df
|
| 583 |
|
| 584 |
def clean_team_name(team_name):
|
| 585 |
"""
|
|
|
|
| 806 |
# MVP Votings for 2025
|
| 807 |
mvp_2025_df = nba.get_award_votings('mvp', 2025)
|
| 808 |
if not mvp_2025_df.empty:
|
| 809 |
+
mvp_2025_df = clean_df_for_firebase(mvp_2025_df) # Clean column names
|
| 810 |
dashboard_data['mvp_2025_votings'] = mvp_2025_df.replace({np.nan: None}).to_dict(orient='records')
|
| 811 |
else:
|
| 812 |
dashboard_data['mvp_2025_votings'] = []
|
|
|
|
| 815 |
# Current Season Playoff Probabilities (East)
|
| 816 |
east_probs_df = nba.get_playoffs_probs('east')
|
| 817 |
if not east_probs_df.empty:
|
| 818 |
+
east_probs_df = clean_df_for_firebase(east_probs_df) # Clean column names
|
| 819 |
dashboard_data['playoff_probs_east'] = east_probs_df.replace({np.nan: None}).to_dict(orient='records')
|
| 820 |
else:
|
| 821 |
dashboard_data['playoff_probs_east'] = []
|
|
|
|
| 824 |
# Current Season Playoff Probabilities (West)
|
| 825 |
west_probs_df = nba.get_playoffs_probs('west')
|
| 826 |
if not west_probs_df.empty:
|
| 827 |
+
west_probs_df = clean_df_for_firebase(west_probs_df) # Clean column names
|
| 828 |
dashboard_data['playoff_probs_west'] = west_probs_df.replace({np.nan: None}).to_dict(orient='records')
|
| 829 |
else:
|
| 830 |
dashboard_data['playoff_probs_west'] = []
|