#!/usr/bin/env python3 """Fetch Nasdaq earnings calendar (date + symbol + surprise%) for all weekdays in the DBEQ range, for a proper PEAD test. Free, no key. Cached per date; gentle pacing + backoff. """ import datetime import json import os import time import urllib.request OUT = "data/surfer/earnings" START = datetime.date(2023, 3, 28) END = datetime.date(2026, 5, 31) def get(u): for a in range(4): try: req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0", "Accept": "application/json"}) return json.loads(urllib.request.urlopen(req, timeout=25).read()) except Exception: if a == 3: return None time.sleep(4 * (a + 1)) return None def main(): os.makedirs(OUT, exist_ok=True) d = START n = fail = 0 while d <= END: if d.weekday() < 5: f = f"{OUT}/{d}.json" if not os.path.exists(f): r = get(f"https://api.nasdaq.com/api/calendar/earnings?date={d}") if r is None: fail += 1 else: rows = (r.get("data") or {}).get("rows") or [] json.dump([(x.get("symbol"), x.get("surprise")) for x in rows], open(f, "w")) n += 1 time.sleep(1.3) d += datetime.timedelta(days=1) print(f"DONE: fetched {n} new days, {fail} failed; cache dir {OUT}") if __name__ == "__main__": main()