Update main.py
Browse files
main.py
CHANGED
|
@@ -673,8 +673,10 @@ def _scrape_player_index_brscraper():
|
|
| 673 |
|
| 674 |
def get_player_career_stats_brscraper(player_name, seasons_to_check=10, playoffs=False):
|
| 675 |
if not BRSCRAPER_AVAILABLE:
|
|
|
|
| 676 |
return pd.DataFrame()
|
| 677 |
all_rows = []
|
|
|
|
| 678 |
# This function should only try to fetch data for the specific seasons requested,
|
| 679 |
# not all available seasons, to avoid unnecessary calls and "invalid season" warnings.
|
| 680 |
# However, the current design of `getPlayerStats` endpoint passes `selected_seasons`
|
|
@@ -688,32 +690,34 @@ def get_player_career_stats_brscraper(player_name, seasons_to_check=10, playoffs
|
|
| 688 |
for season_str in seasons_to_try:
|
| 689 |
end_year = int(season_str.split('–')[1])
|
| 690 |
try:
|
| 691 |
-
logging.info(f"
|
|
|
|
| 692 |
df_season = nba.get_stats(end_year, info='per_game', playoffs=playoffs, rename=False)
|
| 693 |
|
| 694 |
if df_season.empty:
|
| 695 |
-
logging.warning(f"nba.get_stats returned empty DataFrame for {player_name} in {season_str}.")
|
| 696 |
-
continue
|
| 697 |
|
| 698 |
if 'Player' not in df_season.columns:
|
| 699 |
-
logging.warning(f"DataFrame for {player_name} in {season_str} has no 'Player' column. Columns: {df_season.columns.tolist()}")
|
| 700 |
-
continue
|
| 701 |
|
| 702 |
row = df_season[df_season['Player'] == player_name]
|
| 703 |
if not row.empty:
|
| 704 |
row = row.copy()
|
| 705 |
row['Season'] = season_str
|
| 706 |
all_rows.append(row)
|
| 707 |
-
logging.info(f"Found stats for {player_name} in {season_str}.")
|
| 708 |
else:
|
| 709 |
-
logging.info(f"Player {player_name} not found in {season_str} stats.")
|
| 710 |
|
| 711 |
except Exception as e:
|
| 712 |
-
logging.warning(f"Could not fetch {season_str} {'playoff' if playoffs else 'regular season'} stats for {player_name}: {e}")
|
| 713 |
# This is where the "not a valid season" error comes from. It's a warning, not a fatal error for the loop.
|
|
|
|
|
|
|
| 714 |
|
| 715 |
if not all_rows:
|
| 716 |
-
logging.warning(f"No stats found for {player_name} across all attempted seasons.")
|
| 717 |
return pd.DataFrame()
|
| 718 |
|
| 719 |
df = pd.concat(all_rows, ignore_index=True)
|
|
|
|
| 673 |
|
| 674 |
def get_player_career_stats_brscraper(player_name, seasons_to_check=10, playoffs=False):
|
| 675 |
if not BRSCRAPER_AVAILABLE:
|
| 676 |
+
logging.error("BRScraper is not available. Cannot fetch player career stats.")
|
| 677 |
return pd.DataFrame()
|
| 678 |
all_rows = []
|
| 679 |
+
|
| 680 |
# This function should only try to fetch data for the specific seasons requested,
|
| 681 |
# not all available seasons, to avoid unnecessary calls and "invalid season" warnings.
|
| 682 |
# However, the current design of `getPlayerStats` endpoint passes `selected_seasons`
|
|
|
|
| 690 |
for season_str in seasons_to_try:
|
| 691 |
end_year = int(season_str.split('–')[1])
|
| 692 |
try:
|
| 693 |
+
logging.info(f"DEBUG: Calling nba.get_stats for player '{player_name}' in season {season_str} (year: {end_year}, playoffs: {playoffs})...")
|
| 694 |
+
|
| 695 |
df_season = nba.get_stats(end_year, info='per_game', playoffs=playoffs, rename=False)
|
| 696 |
|
| 697 |
if df_season.empty:
|
| 698 |
+
logging.warning(f"DEBUG: nba.get_stats returned empty DataFrame for {player_name} in {season_str}. Skipping this season.")
|
| 699 |
+
continue
|
| 700 |
|
| 701 |
if 'Player' not in df_season.columns:
|
| 702 |
+
logging.warning(f"DEBUG: DataFrame for {player_name} in {season_str} has no 'Player' column. Columns: {df_season.columns.tolist()}. Skipping this season.")
|
| 703 |
+
continue
|
| 704 |
|
| 705 |
row = df_season[df_season['Player'] == player_name]
|
| 706 |
if not row.empty:
|
| 707 |
row = row.copy()
|
| 708 |
row['Season'] = season_str
|
| 709 |
all_rows.append(row)
|
| 710 |
+
logging.info(f"DEBUG: Found stats for {player_name} in {season_str}. Appending row.")
|
| 711 |
else:
|
| 712 |
+
logging.info(f"DEBUG: Player {player_name} not found in {season_str} stats (after getting season data).")
|
| 713 |
|
| 714 |
except Exception as e:
|
|
|
|
| 715 |
# This is where the "not a valid season" error comes from. It's a warning, not a fatal error for the loop.
|
| 716 |
+
logging.warning(f"DEBUG: Exception when fetching {season_str} {'playoff' if playoffs else 'regular season'} stats for {player_name}: {e}")
|
| 717 |
+
# traceback.print_exc() # Uncomment for full traceback if needed
|
| 718 |
|
| 719 |
if not all_rows:
|
| 720 |
+
logging.warning(f"DEBUG: No stats found for {player_name} across all attempted seasons. Returning empty DataFrame.")
|
| 721 |
return pd.DataFrame()
|
| 722 |
|
| 723 |
df = pd.concat(all_rows, ignore_index=True)
|