Update api/main.py
All checks were successful
Build & Push Football Docker Images / build-push-update (push) Successful in 7s

This commit is contained in:
2026-04-12 14:10:18 +00:00
parent a319b77d2e
commit 6baac48733

View File

@@ -36,29 +36,31 @@ def load() -> dict:
def sorted_matches(d: dict): def sorted_matches(d: dict):
"""Return all matches sorted by timestamp, fixing any year-rollover issues.""" """
Split by score: has score = past, no score = future.
Combines all_past + all_future, deduplicates, filters year-rollover ghosts.
"""
now_ts = datetime.now(PT_TZ).timestamp() now_ts = datetime.now(PT_TZ).timestamp()
max_future = now_ts + 60 * 60 * 24 * 180 # 6 months ahead
past = [] all_matches = []
future = [] for m in d.get("all_past", []) + d.get("all_future", []):
ts = m.get("timestamp", 0)
if ts > 0 and ts < max_future:
all_matches.append(m)
for m in d.get("all_past", []): # Deduplicate by (home, away, date)
if m.get("timestamp", 0) > 0: seen, unique = set(), []
past.append(m) for m in all_matches:
key = (m.get("home"), m.get("away"), m.get("date"))
if key not in seen:
seen.add(key)
unique.append(m)
for m in d.get("all_future", []): unique.sort(key=lambda x: x["timestamp"])
if m.get("timestamp", 0) > 0:
future.append(m)
# Sort both past = [m for m in unique if m.get("score")]
past.sort(key=lambda x: x["timestamp"]) future = [m for m in unique if not m.get("score")]
future.sort(key=lambda x: x["timestamp"])
# Edge case: some "future" matches may have wrong year from scraper
# Keep only those with timestamp > now
future = [m for m in future if m["timestamp"] > now_ts]
# Keep only past with timestamp <= now
past = [m for m in past if m["timestamp"] <= now_ts]
return past, future return past, future