mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 21:33:00 +00:00
Switch MLB data source to ESPN API for consistency with other sports
This commit is contained in:
@@ -154,65 +154,71 @@ class BaseMLBManager:
|
|||||||
return "TBD"
|
return "TBD"
|
||||||
|
|
||||||
def _fetch_mlb_api_data(self) -> Dict[str, Any]:
|
def _fetch_mlb_api_data(self) -> Dict[str, Any]:
|
||||||
"""Fetch MLB game data from the API."""
|
"""Fetch MLB game data from the ESPN API."""
|
||||||
try:
|
try:
|
||||||
# MLB Stats API endpoint for schedule
|
# ESPN API endpoint for MLB games
|
||||||
today = datetime.now().strftime('%Y-%m-%d')
|
url = "https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard"
|
||||||
url = f"https://statsapi.mlb.com/api/v1/schedule/games/{today}"
|
|
||||||
|
|
||||||
|
self.logger.info("Fetching MLB games from ESPN API")
|
||||||
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 = {}
|
games = {}
|
||||||
|
|
||||||
for date in data.get('dates', []):
|
for event in data.get('events', []):
|
||||||
for game in date.get('games', []):
|
game_id = event['id']
|
||||||
game_id = game['gamePk']
|
status = event['status']['type']['name'].lower()
|
||||||
|
|
||||||
# Get detailed game data for live games
|
# Get team information
|
||||||
if game['status']['abstractGameState'] == 'Live':
|
competitors = event['competitions'][0]['competitors']
|
||||||
game_url = f"https://statsapi.mlb.com/api/v1/game/{game_id}/linescore"
|
home_team = next(c for c in competitors if c['homeAway'] == 'home')
|
||||||
game_response = self.session.get(game_url, headers=self.headers, timeout=10)
|
away_team = next(c for c in competitors if c['homeAway'] == 'away')
|
||||||
game_response.raise_for_status()
|
|
||||||
game_data = game_response.json()
|
|
||||||
|
|
||||||
# Extract inning, count, and base runner info
|
# Get game state information
|
||||||
inning = game_data.get('currentInning', 1)
|
if status == 'in':
|
||||||
inning_half = game_data.get('inningHalf', '').lower()
|
# For live games, get detailed state
|
||||||
balls = game_data.get('balls', 0)
|
linescore = event['competitions'][0].get('linescores', [{}])[0]
|
||||||
strikes = game_data.get('strikes', 0)
|
inning = linescore.get('value', 1)
|
||||||
bases_occupied = [
|
inning_half = linescore.get('displayValue', '').lower()
|
||||||
game_data.get('offense', {}).get('first', False),
|
|
||||||
game_data.get('offense', {}).get('second', False),
|
|
||||||
game_data.get('offense', {}).get('third', False)
|
|
||||||
]
|
|
||||||
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': game['teams']['away']['team']['abbreviation'],
|
situation = event['competitions'][0].get('situation', {})
|
||||||
'home_team': game['teams']['home']['team']['abbreviation'],
|
balls = situation.get('balls', 0)
|
||||||
'away_score': game['teams']['away']['score'],
|
strikes = situation.get('strikes', 0)
|
||||||
'home_score': game['teams']['home']['score'],
|
|
||||||
'status': game['status']['abstractGameState'].lower(),
|
# Get base runners
|
||||||
'inning': inning,
|
bases_occupied = [
|
||||||
'inning_half': inning_half,
|
situation.get('onFirst', False),
|
||||||
'balls': balls,
|
situation.get('onSecond', False),
|
||||||
'strikes': strikes,
|
situation.get('onThird', False)
|
||||||
'bases_occupied': bases_occupied,
|
]
|
||||||
'start_time': game['gameDate']
|
else:
|
||||||
}
|
# Default values for non-live games
|
||||||
|
inning = 1
|
||||||
|
inning_half = 'top'
|
||||||
|
balls = 0
|
||||||
|
strikes = 0
|
||||||
|
bases_occupied = [False, False, False]
|
||||||
|
|
||||||
|
games[game_id] = {
|
||||||
|
'away_team': away_team['team']['abbreviation'],
|
||||||
|
'home_team': home_team['team']['abbreviation'],
|
||||||
|
'away_score': away_team['score'],
|
||||||
|
'home_score': home_team['score'],
|
||||||
|
'status': status,
|
||||||
|
'inning': inning,
|
||||||
|
'inning_half': inning_half,
|
||||||
|
'balls': balls,
|
||||||
|
'strikes': strikes,
|
||||||
|
'bases_occupied': bases_occupied,
|
||||||
|
'start_time': event['date']
|
||||||
|
}
|
||||||
|
|
||||||
return games
|
return games
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Error fetching MLB data: {e}")
|
self.logger.error(f"Error fetching MLB data from ESPN API: {e}")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
class MBLLiveManager(BaseMLBManager):
|
class MBLLiveManager(BaseMLBManager):
|
||||||
|
|||||||
Reference in New Issue
Block a user