mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 05:13:01 +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,79 +218,93 @@ 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
|
||||||
response = self.session.get(url, headers=self.headers, timeout=10)
|
dates = [
|
||||||
response.raise_for_status()
|
yesterday.strftime("%Y%m%d"),
|
||||||
|
now.strftime("%Y%m%d"),
|
||||||
|
tomorrow.strftime("%Y%m%d")
|
||||||
|
]
|
||||||
|
|
||||||
data = response.json()
|
all_games = {}
|
||||||
|
|
||||||
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}"
|
||||||
|
|
||||||
for event in data.get('events', []):
|
self.logger.info(f"Fetching MLB games from ESPN API for date: {date}")
|
||||||
game_id = event['id']
|
response = self.session.get(url, headers=self.headers, timeout=10)
|
||||||
status = event['status']['type']['name'].lower()
|
response.raise_for_status()
|
||||||
status_state = event['status']['type']['state'].lower()
|
|
||||||
|
|
||||||
# Get team information
|
data = response.json()
|
||||||
competitors = event['competitions'][0]['competitors']
|
|
||||||
home_team = next(c for c in competitors if c['homeAway'] == 'home')
|
|
||||||
away_team = next(c for c in competitors if c['homeAway'] == 'away')
|
|
||||||
|
|
||||||
# Get team abbreviations
|
for event in data.get('events', []):
|
||||||
home_abbr = home_team['team']['abbreviation']
|
game_id = event['id']
|
||||||
away_abbr = away_team['team']['abbreviation']
|
status = event['status']['type']['name'].lower()
|
||||||
|
status_state = event['status']['type']['state'].lower()
|
||||||
|
|
||||||
# Only log detailed information for favorite teams
|
# Get team information
|
||||||
is_favorite_game = (home_abbr in self.favorite_teams or away_abbr in self.favorite_teams)
|
competitors = event['competitions'][0]['competitors']
|
||||||
if is_favorite_game:
|
home_team = next(c for c in competitors if c['homeAway'] == 'home')
|
||||||
self.logger.info(f"Found favorite team game: {away_abbr} @ {home_abbr} (Status: {status}, State: {status_state})")
|
away_team = next(c for c in competitors if c['homeAway'] == 'away')
|
||||||
|
|
||||||
# Get game state information
|
# Get team abbreviations
|
||||||
if status_state == 'in':
|
home_abbr = home_team['team']['abbreviation']
|
||||||
# For live games, get detailed state
|
away_abbr = away_team['team']['abbreviation']
|
||||||
linescore = event['competitions'][0].get('linescores', [{}])[0]
|
|
||||||
inning = linescore.get('value', 1)
|
|
||||||
inning_half = linescore.get('displayValue', '').lower()
|
|
||||||
|
|
||||||
# Get count and bases from situation
|
# Only log detailed information for favorite teams
|
||||||
situation = event['competitions'][0].get('situation', {})
|
is_favorite_game = (home_abbr in self.favorite_teams or away_abbr in self.favorite_teams)
|
||||||
balls = situation.get('balls', 0)
|
if is_favorite_game:
|
||||||
strikes = situation.get('strikes', 0)
|
self.logger.info(f"Found favorite team game: {away_abbr} @ {home_abbr} (Status: {status}, State: {status_state})")
|
||||||
|
|
||||||
# Get base runners
|
# Get game state information
|
||||||
bases_occupied = [
|
if status_state == 'in':
|
||||||
situation.get('onFirst', False),
|
# For live games, get detailed state
|
||||||
situation.get('onSecond', False),
|
linescore = event['competitions'][0].get('linescores', [{}])[0]
|
||||||
situation.get('onThird', False)
|
inning = linescore.get('value', 1)
|
||||||
]
|
inning_half = linescore.get('displayValue', '').lower()
|
||||||
else:
|
|
||||||
# Default values for non-live games
|
|
||||||
inning = 1
|
|
||||||
inning_half = 'top'
|
|
||||||
balls = 0
|
|
||||||
strikes = 0
|
|
||||||
bases_occupied = [False, False, False]
|
|
||||||
|
|
||||||
games[game_id] = {
|
# Get count and bases from situation
|
||||||
'away_team': away_abbr,
|
situation = event['competitions'][0].get('situation', {})
|
||||||
'home_team': home_abbr,
|
balls = situation.get('balls', 0)
|
||||||
'away_score': away_team['score'],
|
strikes = situation.get('strikes', 0)
|
||||||
'home_score': home_team['score'],
|
|
||||||
'status': status,
|
# Get base runners
|
||||||
'status_state': status_state, # Add status state
|
bases_occupied = [
|
||||||
'inning': inning,
|
situation.get('onFirst', False),
|
||||||
'inning_half': inning_half,
|
situation.get('onSecond', False),
|
||||||
'balls': balls,
|
situation.get('onThird', False)
|
||||||
'strikes': strikes,
|
]
|
||||||
'bases_occupied': bases_occupied,
|
else:
|
||||||
'start_time': event['date']
|
# Default values for non-live games
|
||||||
}
|
inning = 1
|
||||||
|
inning_half = 'top'
|
||||||
|
balls = 0
|
||||||
|
strikes = 0
|
||||||
|
bases_occupied = [False, False, False]
|
||||||
|
|
||||||
|
all_games[game_id] = {
|
||||||
|
'away_team': away_abbr,
|
||||||
|
'home_team': home_abbr,
|
||||||
|
'away_score': away_team['score'],
|
||||||
|
'home_score': home_team['score'],
|
||||||
|
'status': status,
|
||||||
|
'status_state': status_state,
|
||||||
|
'inning': inning,
|
||||||
|
'inning_half': inning_half,
|
||||||
|
'balls': balls,
|
||||||
|
'strikes': strikes,
|
||||||
|
'bases_occupied': bases_occupied,
|
||||||
|
'start_time': event['date']
|
||||||
|
}
|
||||||
|
|
||||||
# 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