diff --git a/src/milb_manager.py b/src/milb_manager.py index ae3a08ee..a7bb542b 100644 --- a/src/milb_manager.py +++ b/src/milb_manager.py @@ -1502,9 +1502,28 @@ class MiLBRecentManager(BaseMiLBManager): logger.info(f"[MiLB] All games found ({len(all_games_log)}): {all_games_log}") logger.info(f"[MiLB] Favorite team games found ({len(favorite_games_log)}): {favorite_games_log}") - # Sort by game time (most recent first) and limit to recent_games_to_show + # Sort by game time (most recent first) and apply per-team logic new_recent_games.sort(key=lambda x: x.get('start_time', ''), reverse=True) - new_recent_games = new_recent_games[:self.recent_games_to_show] + + # If showing favorite teams only, select one game per team + if self.milb_config.get("show_favorite_teams_only", False): + # Select one game per favorite team (most recent game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in new_recent_games + if game.get('home_team') == team or game.get('away_team') == team] + + if team_specific_games: + # Take the most recent (first in sorted list) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + team_games.sort(key=lambda x: x.get('start_time', ''), reverse=True) + new_recent_games = team_games + else: + # Limit to configured number if not using favorite teams only + new_recent_games = new_recent_games[:self.recent_games_to_show] if new_recent_games: logger.info(f"[MiLB] Found {len(new_recent_games)} recent final games for favorite teams: {self.favorite_teams}") @@ -1716,9 +1735,28 @@ class MiLBUpcomingManager(BaseMiLBManager): self.logger.info(f"[MiLB] Added upcoming game: {game.get('away_team')} @ {game.get('home_team')} at {game_time}") self.logger.debug(f"[MiLB] Game data for upcoming: {game}") - # Sort by game time (soonest first) and limit to upcoming_games_to_show + # Sort by game time (soonest first) and apply per-team logic new_upcoming_games.sort(key=lambda x: x.get('start_time', '')) - new_upcoming_games = new_upcoming_games[:self.upcoming_games_to_show] + + # If showing favorite teams only, select one game per team + if self.milb_config.get("show_favorite_teams_only", False): + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in new_upcoming_games + if game.get('home_team') == team or game.get('away_team') == team] + + if team_specific_games: + # Take the earliest (first in sorted list) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda x: x.get('start_time', '')) + new_upcoming_games = team_games + else: + # Limit to configured number if not using favorite teams only + new_upcoming_games = new_upcoming_games[:self.upcoming_games_to_show] self.logger.info(f"[MiLB] Found {len(new_upcoming_games)} upcoming games after processing") # Compare new list with old list to see if an update is needed diff --git a/src/mlb_manager.py b/src/mlb_manager.py index 56d1b752..40374511 100644 --- a/src/mlb_manager.py +++ b/src/mlb_manager.py @@ -1223,9 +1223,28 @@ class MLBRecentManager(BaseMLBManager): self.logger.info(f"[MLB] All games found ({len(all_games_log)}): {all_games_log}") self.logger.info(f"[MLB] Favorite team games found ({len(favorite_games_log)}): {favorite_games_log}") - # Sort by game time (most recent first) and limit to recent_games_to_show + # Sort by game time (most recent first) and apply per-team logic new_recent_games.sort(key=lambda x: x['start_time'], reverse=True) - new_recent_games = new_recent_games[:self.recent_games_to_show] + + # If showing favorite teams only, select one game per team + if self.mlb_config.get("show_favorite_teams_only", False): + # Select one game per favorite team (most recent game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in new_recent_games + if game['home_team'] == team or game['away_team'] == team] + + if team_specific_games: + # Take the most recent (first in sorted list) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + team_games.sort(key=lambda x: x['start_time'], reverse=True) + new_recent_games = team_games + else: + # Limit to configured number if not using favorite teams only + new_recent_games = new_recent_games[:self.recent_games_to_show] if new_recent_games: logger.info(f"[MLB] Found {len(new_recent_games)} recent games for favorite teams: {self.favorite_teams}") @@ -1345,9 +1364,28 @@ class MLBUpcomingManager(BaseMLBManager): else: self.logger.info(f"[MLB] Skipping game {game_id} - not upcoming.") - # Sort by game time (soonest first) and limit to upcoming_games_to_show + # Sort by game time (soonest first) and apply per-team logic new_upcoming_games.sort(key=lambda x: x['start_time']) - new_upcoming_games = new_upcoming_games[:self.upcoming_games_to_show] + + # If showing favorite teams only, select one game per team + if self.mlb_config.get("show_favorite_teams_only", False): + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in new_upcoming_games + if game['home_team'] == team or game['away_team'] == team] + + if team_specific_games: + # Take the earliest (first in sorted list) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda x: x['start_time']) + new_upcoming_games = team_games + else: + # Limit to configured number if not using favorite teams only + new_upcoming_games = new_upcoming_games[:self.upcoming_games_to_show] if new_upcoming_games: logger.info(f"[MLB] Found {len(new_upcoming_games)} upcoming games for favorite teams") diff --git a/src/nba_managers.py b/src/nba_managers.py index 4a873f21..48364046 100644 --- a/src/nba_managers.py +++ b/src/nba_managers.py @@ -759,15 +759,30 @@ class NBARecentManager(BaseNBAManager): # Filter for favorite teams only if the config is set if self.nba_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_recent_games - if game['home_abbr'] in self.favorite_teams or - game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_recent_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (most recent game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the most recent + team_specific_games.sort(key=lambda g: g.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + team_games.sort(key=lambda g: g.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) else: team_games = new_recent_games - - # Sort games by start time, most recent first, then limit to recent_games_to_show - team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) - team_games = team_games[:self.recent_games_to_show] + # Sort games by start time, most recent first, then limit to recent_games_to_show + team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) + team_games = team_games[:self.recent_games_to_show] self.recent_games = team_games if self.recent_games: @@ -838,15 +853,30 @@ class NBAUpcomingManager(BaseNBAManager): # Filter for favorite teams only if the config is set if self.nba_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_upcoming_games - if game['home_abbr'] in self.favorite_teams or - game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_upcoming_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the earliest + team_specific_games.sort(key=lambda g: g.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda g: g.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) else: team_games = new_upcoming_games - - # Sort games by start time, soonest first, then limit to configured count - team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) - team_games = team_games[:self.upcoming_games_to_show] + # Sort games by start time, soonest first, then limit to configured count + team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) + team_games = team_games[:self.upcoming_games_to_show] self.upcoming_games = team_games if self.upcoming_games: diff --git a/src/ncaa_baseball_managers.py b/src/ncaa_baseball_managers.py index fe20b7ef..eab46be9 100644 --- a/src/ncaa_baseball_managers.py +++ b/src/ncaa_baseball_managers.py @@ -908,14 +908,32 @@ class NCAABaseballRecentManager(BaseNCAABaseballManager): # Filter for favorite teams only if the config is set if self.ncaa_baseball_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_recent_games if game['home_team'] in self.favorite_teams or game['away_team'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_recent_games + if game['home_team'] in self.favorite_teams or + game['away_team'] in self.favorite_teams] + + # Select one game per favorite team (most recent game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_team'] == team or game['away_team'] == team] + + if team_specific_games: + # Sort by game time and take the most recent + team_specific_games.sort(key=lambda g: g.get('start_time'), reverse=True) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + team_games.sort(key=lambda g: g.get('start_time'), reverse=True) else: team_games = new_recent_games - - if team_games: # Sort by game time (most recent first), then limit to recent_games_to_show team_games = sorted(team_games, key=lambda g: g.get('start_time'), reverse=True) team_games = team_games[:self.recent_games_to_show] + + if team_games: logger.info(f"[NCAABaseball] Found {len(team_games)} recent games for favorite teams (limited to {self.recent_games_to_show}): {self.favorite_teams}") self.recent_games = team_games if not self.current_game or self.current_game.get('id') not in [g.get('id') for g in self.recent_games]: @@ -1010,14 +1028,32 @@ class NCAABaseballUpcomingManager(BaseNCAABaseballManager): # Filter for favorite teams only if the config is set if self.ncaa_baseball_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_upcoming_games if game['home_team'] in self.favorite_teams or game['away_team'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_upcoming_games + if game['home_team'] in self.favorite_teams or + game['away_team'] in self.favorite_teams] + + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_team'] == team or game['away_team'] == team] + + if team_specific_games: + # Sort by game time and take the earliest + team_specific_games.sort(key=lambda g: g.get('start_time')) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda g: g.get('start_time')) else: team_games = new_upcoming_games - - if team_games: # Sort by game time (soonest first), then limit to configured count team_games = sorted(team_games, key=lambda g: g.get('start_time')) team_games = team_games[:self.upcoming_games_to_show] + + if team_games: logger.info(f"[NCAABaseball] Found {len(team_games)} upcoming games for favorite teams (limited to {self.upcoming_games_to_show})") self.upcoming_games = team_games if not self.current_game or self.current_game.get('id') not in [g.get('id') for g in self.upcoming_games]: diff --git a/src/ncaam_basketball_managers.py b/src/ncaam_basketball_managers.py index 4dd3f7c1..d58b4f8a 100644 --- a/src/ncaam_basketball_managers.py +++ b/src/ncaam_basketball_managers.py @@ -828,15 +828,30 @@ class NCAAMBasketballRecentManager(BaseNCAAMBasketballManager): # Filter for favorite teams only if the config is set if self.ncaam_basketball_config.get("show_favorite_teams_only", False): - new_team_games = [game for game in new_recent_games - if game['home_abbr'] in self.favorite_teams or - game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_recent_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (most recent game for each team) + new_team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the most recent + team_specific_games.sort(key=lambda g: g.get('start_time_utc', datetime.min.replace(tzinfo=timezone.utc)), reverse=True) + new_team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + new_team_games.sort(key=lambda g: g.get('start_time_utc', datetime.min.replace(tzinfo=timezone.utc)), reverse=True) else: new_team_games = new_recent_games - - # Sort by game time (most recent first), then limit to recent_games_to_show - new_team_games.sort(key=lambda g: g.get('start_time_utc', datetime.min.replace(tzinfo=timezone.utc)), reverse=True) - new_team_games = new_team_games[:self.recent_games_to_show] + # Sort by game time (most recent first), then limit to recent_games_to_show + new_team_games.sort(key=lambda g: g.get('start_time_utc', datetime.min.replace(tzinfo=timezone.utc)), reverse=True) + new_team_games = new_team_games[:self.recent_games_to_show] # Only log if there's a change in games or enough time has passed should_log = ( @@ -964,15 +979,30 @@ class NCAAMBasketballUpcomingManager(BaseNCAAMBasketballManager): # Filter for favorite teams only if the config is set if self.ncaam_basketball_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_upcoming_games - if game['home_abbr'] in self.favorite_teams or - game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_upcoming_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the earliest + team_specific_games.sort(key=lambda g: g.get('start_time_utc', datetime.max.replace(tzinfo=timezone.utc))) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda g: g.get('start_time_utc', datetime.max.replace(tzinfo=timezone.utc))) else: team_games = new_upcoming_games - - # Sort by game time (soonest first), then limit to configured count - team_games.sort(key=lambda g: g.get('start_time_utc', datetime.max.replace(tzinfo=timezone.utc))) - team_games = team_games[:self.upcoming_games_to_show] + # Sort by game time (soonest first), then limit to configured count + team_games.sort(key=lambda g: g.get('start_time_utc', datetime.max.replace(tzinfo=timezone.utc))) + team_games = team_games[:self.upcoming_games_to_show] if self._should_log("team_games_upcoming", 300): diff --git a/src/nhl_managers.py b/src/nhl_managers.py index b281abe0..0f6f4953 100644 --- a/src/nhl_managers.py +++ b/src/nhl_managers.py @@ -700,15 +700,30 @@ class NHLRecentManager(BaseNHLManager): # Filter for favorite teams only if the config is set if self.nhl_config.get("show_favorite_teams_only", False): - team_games = [game for game in processed_games - if game['home_abbr'] in self.favorite_teams or - game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in processed_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (most recent game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the most recent + team_specific_games.sort(key=lambda g: g.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + team_games.sort(key=lambda g: g.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) else: team_games = processed_games - - # Sort games by start time, most recent first, then limit to recent_games_to_show - team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) - team_games = team_games[:self.recent_games_to_show] + # Sort games by start time, most recent first, then limit to recent_games_to_show + team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.min.replace(tzinfo=timezone.utc), reverse=True) + team_games = team_games[:self.recent_games_to_show] self.logger.info(f"[NHL] Found {len(team_games)} recent games for favorite teams (limited to {self.recent_games_to_show})") @@ -805,15 +820,30 @@ class NHLUpcomingManager(BaseNHLManager): # Filter for favorite teams only if the config is set if self.nhl_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_upcoming_games - if game['home_abbr'] in self.favorite_teams or - game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_upcoming_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the earliest + team_specific_games.sort(key=lambda g: g.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda g: g.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) else: team_games = new_upcoming_games - - # Sort games by start time, soonest first, then limit to configured count - team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) - team_games = team_games[:self.upcoming_games_to_show] + # Sort games by start time, soonest first, then limit to configured count + team_games.sort(key=lambda x: x.get('start_time_utc') or datetime.max.replace(tzinfo=timezone.utc)) + team_games = team_games[:self.upcoming_games_to_show] # Only log if there's a change in games or enough time has passed should_log = ( diff --git a/src/soccer_managers.py b/src/soccer_managers.py index a0b27fc9..b39921dd 100644 --- a/src/soccer_managers.py +++ b/src/soccer_managers.py @@ -971,13 +971,30 @@ class SoccerRecentManager(BaseSoccerManager): # Filter for favorite teams only if the config is set if self.soccer_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_recent_games if game['home_abbr'] in self.favorite_teams or game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_recent_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (most recent game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the most recent + team_specific_games.sort(key=lambda g: g['start_time_utc'], reverse=True) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time (most recent first) + team_games.sort(key=lambda g: g['start_time_utc'], reverse=True) else: team_games = new_recent_games - - # Sort games by start time, most recent first, and limit to recent_games_to_show - team_games.sort(key=lambda x: x['start_time_utc'], reverse=True) - team_games = team_games[:self.recent_games_to_show] + # Sort games by start time, most recent first, and limit to recent_games_to_show + team_games.sort(key=lambda x: x['start_time_utc'], reverse=True) + team_games = team_games[:self.recent_games_to_show] # Update only if the list content changes new_ids = {g['id'] for g in team_games} @@ -1078,13 +1095,30 @@ class SoccerUpcomingManager(BaseSoccerManager): # Filter for favorite teams only if the config is set if self.soccer_config.get("show_favorite_teams_only", False): - team_games = [game for game in new_upcoming_games if game['home_abbr'] in self.favorite_teams or game['away_abbr'] in self.favorite_teams] + # Get all games involving favorite teams + favorite_team_games = [game for game in new_upcoming_games + if game['home_abbr'] in self.favorite_teams or + game['away_abbr'] in self.favorite_teams] + + # Select one game per favorite team (earliest upcoming game for each team) + team_games = [] + for team in self.favorite_teams: + # Find games where this team is playing + team_specific_games = [game for game in favorite_team_games + if game['home_abbr'] == team or game['away_abbr'] == team] + + if team_specific_games: + # Sort by game time and take the earliest + team_specific_games.sort(key=lambda g: g['start_time_utc']) + team_games.append(team_specific_games[0]) + + # Sort the final list by game time + team_games.sort(key=lambda g: g['start_time_utc']) else: team_games = new_upcoming_games - - # Sort games by start time, soonest first, then limit to configured count - team_games.sort(key=lambda x: x['start_time_utc']) - team_games = team_games[:self.upcoming_games_to_show] + # Sort games by start time, soonest first, then limit to configured count + team_games.sort(key=lambda x: x['start_time_utc']) + team_games = team_games[:self.upcoming_games_to_show] # Update only if the list content changes new_ids = {g['id'] for g in team_games}