feat: calendar eco

This commit is contained in:
OpenSquared
2026-07-21 12:41:26 +02:00
parent 4c2103156e
commit 0bdf3f382d
6 changed files with 69 additions and 16 deletions

View File

@@ -38,31 +38,42 @@ def calendar_sync(weeks_ahead: int = 8) -> Dict[str, Any]:
from services.ff_calendar import sync_live, scrape_upcoming
# Step 1 — live JSON (thisweek + nextweek) — picks up actuals in real time
print("[calendar_sync] step 1/3 — FF live JSON (thisweek + nextweek)", flush=True)
r_live = sync_live()
result["live"] = r_live
print(f"[calendar_sync] live result: {r_live}", flush=True)
# Step 2 — HTML scraper for weeks 3..N ahead (forecasts only, no actuals yet)
scrape_weeks = max(0, weeks_ahead - 2)
if scrape_weeks > 0:
print(f"[calendar_sync] step 2/3 — FF HTML scrape, {scrape_weeks} week(s) ahead", flush=True)
r_scrape = scrape_upcoming(weeks_ahead=scrape_weeks)
result["scrape"] = r_scrape
print(f"[calendar_sync] scrape result: {r_scrape}", flush=True)
# FF's scraper gets 403'd (Cloudflare) often enough that "weeks 3..N" can
# silently come back empty — fall back to FMP for that same range when it does.
if not r_scrape.get("total_upserted"):
print("[calendar_sync] step 3/3 — scrape got 0 events, checking FMP fallback", flush=True)
from services.fmp_calendar import check_fmp_key, fetch_upcoming
if check_fmp_key()["configured"]:
key_status = check_fmp_key()
print(f"[calendar_sync] FMP key configured: {key_status['configured']}", flush=True)
if key_status["configured"]:
r_fmp = fetch_upcoming(weeks_ahead=weeks_ahead)
result["fmp_fallback"] = r_fmp
print(f"[calendar_sync] FMP fallback result: {r_fmp}", flush=True)
if r_fmp.get("total_upserted"):
source = "ff+fmp"
else:
result["fmp_fallback"] = {"skipped": "no_key"}
else:
print("[calendar_sync] step 3/3 — scrape got events, FMP fallback not needed", flush=True)
else:
result["scrape"] = {"skipped": True}
except Exception as e:
logger.error(f"[calendar_sync] FF sync failed: {e}")
print(f"[calendar_sync] EXCEPTION: {e!r}", flush=True)
result = {"error": str(e)}
source = "error"