diff --git a/api/main.py b/api/main.py index 74d4885..85dd226 100644 --- a/api/main.py +++ b/api/main.py @@ -36,29 +36,31 @@ def load() -> dict: def sorted_matches(d: dict): - """Return all matches sorted by timestamp, fixing any year-rollover issues.""" - now_ts = datetime.now(PT_TZ).timestamp() + """ + 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() + max_future = now_ts + 60 * 60 * 24 * 180 # 6 months ahead - past = [] - future = [] + all_matches = [] + 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", []): - if m.get("timestamp", 0) > 0: - past.append(m) + # Deduplicate by (home, away, date) + seen, unique = set(), [] + 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", []): - if m.get("timestamp", 0) > 0: - future.append(m) + unique.sort(key=lambda x: x["timestamp"]) - # Sort both - past.sort(key=lambda x: x["timestamp"]) - 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] + past = [m for m in unique if m.get("score")] + future = [m for m in unique if not m.get("score")] return past, future