mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 21:33:00 +00:00
MLB Manager improvements: 1) Fetch games for yesterday, today, and tomorrow 2) Use ESPN API date parameter 3) Combine all games into single response
This commit is contained in:
@@ -218,17 +218,31 @@ class BaseMLBManager:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# ESPN API endpoint for MLB games
|
# Get dates for API request
|
||||||
url = "https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard"
|
now = datetime.now(timezone.utc)
|
||||||
|
yesterday = now - timedelta(days=1)
|
||||||
|
tomorrow = now + timedelta(days=1)
|
||||||
|
|
||||||
self.logger.info("Fetching MLB games from ESPN API")
|
# Format dates for API
|
||||||
|
dates = [
|
||||||
|
yesterday.strftime("%Y%m%d"),
|
||||||
|
now.strftime("%Y%m%d"),
|
||||||
|
tomorrow.strftime("%Y%m%d")
|
||||||
|
]
|
||||||
|
|
||||||
|
all_games = {}
|
||||||
|
|
||||||
|
# Fetch games for each date
|
||||||
|
for date in dates:
|
||||||
|
# ESPN API endpoint for MLB games with date parameter
|
||||||
|
url = f"https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard?dates={date}"
|
||||||
|
|
||||||
|
self.logger.info(f"Fetching MLB games from ESPN API for date: {date}")
|
||||||
response = self.session.get(url, headers=self.headers, timeout=10)
|
response = self.session.get(url, headers=self.headers, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
games = {}
|
|
||||||
|
|
||||||
for event in data.get('events', []):
|
for event in data.get('events', []):
|
||||||
game_id = event['id']
|
game_id = event['id']
|
||||||
status = event['status']['type']['name'].lower()
|
status = event['status']['type']['name'].lower()
|
||||||
@@ -274,13 +288,13 @@ class BaseMLBManager:
|
|||||||
strikes = 0
|
strikes = 0
|
||||||
bases_occupied = [False, False, False]
|
bases_occupied = [False, False, False]
|
||||||
|
|
||||||
games[game_id] = {
|
all_games[game_id] = {
|
||||||
'away_team': away_abbr,
|
'away_team': away_abbr,
|
||||||
'home_team': home_abbr,
|
'home_team': home_abbr,
|
||||||
'away_score': away_team['score'],
|
'away_score': away_team['score'],
|
||||||
'home_score': home_team['score'],
|
'home_score': home_team['score'],
|
||||||
'status': status,
|
'status': status,
|
||||||
'status_state': status_state, # Add status state
|
'status_state': status_state,
|
||||||
'inning': inning,
|
'inning': inning,
|
||||||
'inning_half': inning_half,
|
'inning_half': inning_half,
|
||||||
'balls': balls,
|
'balls': balls,
|
||||||
@@ -290,7 +304,7 @@ class BaseMLBManager:
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Only log favorite team games
|
# Only log favorite team games
|
||||||
favorite_games = [game for game in games.values()
|
favorite_games = [game for game in all_games.values()
|
||||||
if game['home_team'] in self.favorite_teams or
|
if game['home_team'] in self.favorite_teams or
|
||||||
game['away_team'] in self.favorite_teams]
|
game['away_team'] in self.favorite_teams]
|
||||||
if favorite_games:
|
if favorite_games:
|
||||||
@@ -298,7 +312,7 @@ class BaseMLBManager:
|
|||||||
for game in favorite_games:
|
for game in favorite_games:
|
||||||
self.logger.info(f"Favorite team game: {game['away_team']} @ {game['home_team']} (Status: {game['status']}, State: {game['status_state']})")
|
self.logger.info(f"Favorite team game: {game['away_team']} @ {game['home_team']} (Status: {game['status']}, State: {game['status_state']})")
|
||||||
|
|
||||||
return games
|
return all_games
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Error fetching MLB data from ESPN API: {e}")
|
self.logger.error(f"Error fetching MLB data from ESPN API: {e}")
|
||||||
@@ -624,14 +638,25 @@ class MLBUpcomingManager(BaseMLBManager):
|
|||||||
logger.info(f"Checking favorite team game: {game['away_team']} @ {game['home_team']} at {game_time}")
|
logger.info(f"Checking favorite team game: {game['away_team']} @ {game['home_team']} at {game_time}")
|
||||||
logger.info(f"Game status: {game['status']}, State: {game['status_state']}")
|
logger.info(f"Game status: {game['status']}, State: {game['status_state']}")
|
||||||
|
|
||||||
# Check if game is within our time window and is upcoming
|
# Check if game is within our time window
|
||||||
is_within_time = now <= game_time <= upcoming_cutoff
|
is_within_time = now <= game_time <= upcoming_cutoff
|
||||||
is_upcoming = game['status_state'] == 'pre' # Use status_state like NHL manager
|
|
||||||
|
# For upcoming games, we'll consider any game that:
|
||||||
|
# 1. Is within our time window
|
||||||
|
# 2. Is not final (not 'post' or 'final' state)
|
||||||
|
# 3. Has a future start time
|
||||||
|
is_upcoming = (
|
||||||
|
is_within_time and
|
||||||
|
game['status_state'] not in ['post', 'final', 'completed'] and
|
||||||
|
game_time > now
|
||||||
|
)
|
||||||
|
|
||||||
logger.info(f"Within time window: {is_within_time}")
|
logger.info(f"Within time window: {is_within_time}")
|
||||||
logger.info(f"Is upcoming status: {is_upcoming}")
|
logger.info(f"Is upcoming: {is_upcoming}")
|
||||||
|
logger.info(f"Game time > now: {game_time > now}")
|
||||||
|
logger.info(f"Status state not final: {game['status_state'] not in ['post', 'final', 'completed']}")
|
||||||
|
|
||||||
if is_upcoming and is_within_time:
|
if is_upcoming:
|
||||||
new_upcoming_games.append(game)
|
new_upcoming_games.append(game)
|
||||||
logger.info(f"Added favorite team game to upcoming list: {game['away_team']} @ {game['home_team']}")
|
logger.info(f"Added favorite team game to upcoming list: {game['away_team']} @ {game['home_team']}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user