Spaces:
Sleeping
Sleeping
Update engine/live_game_engine.py
Browse files- engine/live_game_engine.py +46 -84
engine/live_game_engine.py
CHANGED
|
@@ -244,103 +244,65 @@ def enrich_game_from_live_feed(game: dict[str, Any], feed: dict[str, Any]) -> di
|
|
| 244 |
linescore = live_data.get("linescore", {}) or {}
|
| 245 |
plays = live_data.get("plays", {}) or {}
|
| 246 |
current_play = plays.get("currentPlay", {}) or {}
|
|
|
|
|
|
|
| 247 |
matchup = current_play.get("matchup", {}) or {}
|
| 248 |
count = current_play.get("count", {}) or {}
|
| 249 |
|
| 250 |
offense = linescore.get("offense", {}) or {}
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
away_team_stats = teams.get("away", {}) or {}
|
| 254 |
-
home_team_stats = teams.get("home", {}) or {}
|
| 255 |
-
|
| 256 |
-
boxscore = live_data.get("boxscore", {}) or {}
|
| 257 |
-
boxscore_teams = boxscore.get("teams", {}) or {}
|
| 258 |
-
|
| 259 |
-
away_box = boxscore_teams.get("away", {}) or {}
|
| 260 |
-
home_box = boxscore_teams.get("home", {}) or {}
|
| 261 |
-
|
| 262 |
-
away_team_stats_batting = ((away_box.get("teamStats", {}) or {}).get("batting", {}) or {})
|
| 263 |
-
home_team_stats_batting = ((home_box.get("teamStats", {}) or {}).get("batting", {}) or {})
|
| 264 |
-
|
| 265 |
-
away_team_stats_fielding = ((away_box.get("teamStats", {}) or {}).get("fielding", {}) or {})
|
| 266 |
-
home_team_stats_fielding = ((home_box.get("teamStats", {}) or {}).get("fielding", {}) or {})
|
| 267 |
-
|
| 268 |
-
inning_half = str(linescore.get("inningHalf", "")).strip()
|
| 269 |
-
current_inning = linescore.get("currentInning")
|
| 270 |
-
outs = count.get("outs", linescore.get("outs"))
|
| 271 |
-
|
| 272 |
-
existing_status = str(out.get("status", "")).strip()
|
| 273 |
-
out["status"] = derive_feed_status(
|
| 274 |
-
feed=feed,
|
| 275 |
-
inning_half=inning_half,
|
| 276 |
-
current_inning=current_inning,
|
| 277 |
-
fallback=existing_status,
|
| 278 |
-
)
|
| 279 |
-
|
| 280 |
-
try:
|
| 281 |
-
out["outs"] = 0 if outs is None else int(outs)
|
| 282 |
-
except Exception:
|
| 283 |
-
out["outs"] = 0
|
| 284 |
-
|
| 285 |
-
out["balls"] = _safe_int(count.get("balls"))
|
| 286 |
-
out["strikes"] = _safe_int(count.get("strikes"))
|
| 287 |
-
|
| 288 |
-
out["runner_on_1b"] = bool(offense.get("first"))
|
| 289 |
-
out["runner_on_2b"] = bool(offense.get("second"))
|
| 290 |
-
out["runner_on_3b"] = bool(offense.get("third"))
|
| 291 |
|
| 292 |
batter = matchup.get("batter", {}) or {}
|
| 293 |
pitcher = matchup.get("pitcher", {}) or {}
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
out["pitcher_name"] = str(pitcher.get("fullName", "") or "").strip()
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
current_batter_id=batter_id,
|
| 303 |
-
)
|
| 304 |
-
out["on_deck_name"] = on_deck_name
|
| 305 |
-
out["in_hole_name"] = in_hole_name
|
| 306 |
-
out["three_away_name"] = three_away_name
|
| 307 |
-
|
| 308 |
-
out["away_score"] = _first_non_null(
|
| 309 |
-
away_team_stats.get("runs"),
|
| 310 |
-
away_team_stats_batting.get("runs"),
|
| 311 |
-
out.get("away_score"),
|
| 312 |
-
)
|
| 313 |
-
out["home_score"] = _first_non_null(
|
| 314 |
-
home_team_stats.get("runs"),
|
| 315 |
-
home_team_stats_batting.get("runs"),
|
| 316 |
-
out.get("home_score"),
|
| 317 |
-
)
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
out.get("away_hits"),
|
| 323 |
-
)
|
| 324 |
-
out["home_hits"] = _first_non_null(
|
| 325 |
-
home_team_stats.get("hits"),
|
| 326 |
-
home_team_stats_batting.get("hits"),
|
| 327 |
-
out.get("home_hits"),
|
| 328 |
-
)
|
| 329 |
|
| 330 |
-
out["
|
| 331 |
-
|
| 332 |
-
away_team_stats_fielding.get("errors"),
|
| 333 |
-
out.get("away_errors"),
|
| 334 |
-
)
|
| 335 |
-
out["home_errors"] = _first_non_null(
|
| 336 |
-
home_team_stats.get("errors"),
|
| 337 |
-
home_team_stats_fielding.get("errors"),
|
| 338 |
-
out.get("home_errors"),
|
| 339 |
-
)
|
| 340 |
|
| 341 |
-
|
| 342 |
-
out["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
def _safe_float(value: Any) -> float | None:
|
| 345 |
try:
|
| 346 |
if value is None:
|
|
|
|
| 244 |
linescore = live_data.get("linescore", {}) or {}
|
| 245 |
plays = live_data.get("plays", {}) or {}
|
| 246 |
current_play = plays.get("currentPlay", {}) or {}
|
| 247 |
+
|
| 248 |
+
|
| 249 |
matchup = current_play.get("matchup", {}) or {}
|
| 250 |
count = current_play.get("count", {}) or {}
|
| 251 |
|
| 252 |
offense = linescore.get("offense", {}) or {}
|
| 253 |
+
defense = linescore.get("defense", {}) or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
batter = matchup.get("batter", {}) or {}
|
| 256 |
pitcher = matchup.get("pitcher", {}) or {}
|
| 257 |
|
| 258 |
+
batter_name = str(batter.get("fullName", "") or "").strip()
|
| 259 |
+
pitcher_name = str(pitcher.get("fullName", "") or "").strip()
|
|
|
|
| 260 |
|
| 261 |
+
if not batter_name:
|
| 262 |
+
batter_obj = offense.get("batter", {}) or {}
|
| 263 |
+
batter_name = str(batter_obj.get("fullName", "") or "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
if not pitcher_name:
|
| 266 |
+
pitcher_obj = defense.get("pitcher", {}) or {}
|
| 267 |
+
pitcher_name = str(pitcher_obj.get("fullName", "") or "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
+
out["batter_name"] = batter_name
|
| 270 |
+
out["pitcher_name"] = pitcher_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
+
out["balls"] = count.get("balls")
|
| 273 |
+
out["strikes"] = count.get("strikes")
|
| 274 |
+
out["outs"] = count.get("outs", linescore.get("outs"))
|
| 275 |
+
|
| 276 |
+
out["runner_on_1b"] = offense.get("first") is not None
|
| 277 |
+
out["runner_on_2b"] = offense.get("second") is not None
|
| 278 |
+
out["runner_on_3b"] = offense.get("third") is not None
|
| 279 |
|
| 280 |
+
result = current_play.get("result", {}) or {}
|
| 281 |
+
out["last_play"] = str(result.get("description", "") or "").strip()
|
| 282 |
+
|
| 283 |
+
play_events = current_play.get("playEvents", []) or []
|
| 284 |
+
|
| 285 |
+
pitch_event = None
|
| 286 |
+
for event in reversed(play_events):
|
| 287 |
+
pitch_data = event.get("pitchData") or {}
|
| 288 |
+
if pitch_data:
|
| 289 |
+
pitch_event = event
|
| 290 |
+
break
|
| 291 |
+
|
| 292 |
+
if pitch_event:
|
| 293 |
+
pitch_data = pitch_event.get("pitchData", {}) or {}
|
| 294 |
+
pitch_breaks = pitch_data.get("breaks", {}) or {}
|
| 295 |
+
coords = pitch_data.get("coordinates", {}) or {}
|
| 296 |
+
details = pitch_event.get("details", {}) or {}
|
| 297 |
+
|
| 298 |
+
out["last_pitch"] = str(details.get("description", "") or "").strip()
|
| 299 |
+
out["pitch_type"] = str(((details.get("type", {}) or {}).get("description", "")) or "").strip()
|
| 300 |
+
out["pitch_velocity"] = pitch_data.get("startSpeed")
|
| 301 |
+
out["pitch_spin_rate"] = pitch_breaks.get("spinRate")
|
| 302 |
+
out["pitch_extension"] = pitch_data.get("extension")
|
| 303 |
+
out["pitch_pfx_x"] = coords.get("pfxX")
|
| 304 |
+
out["pitch_pfx_z"] = coords.get("pfxZ")
|
| 305 |
+
|
| 306 |
def _safe_float(value: Any) -> float | None:
|
| 307 |
try:
|
| 308 |
if value is None:
|