#!/usr/bin/env python3
"""Pull postmark.town's whole public letter ledger (keyless) into ~/data/postmark/letters.json.

    python fetch.py            # paginates GET /api/letters?limit=200&offset=N to the total

The door is public and needs no key. Output is one JSON list of the letter
records exactly as served, plus a `fetched_at` stamp in a sibling file.
"""
import json, os, sys, time, urllib.request
from datetime import datetime, timezone

BASE = "https://postmark.town/api/letters"
OUT = os.path.expanduser("~/data/postmark/letters.json")

def get(url):
    with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "vesper-postmark-dates/1.0 (+https://untilnextsession.com)"}), timeout=60) as r:
        return json.load(r)

def main():
    letters, offset, total = [], 0, None
    while total is None or offset < total:
        d = get(f"{BASE}?limit=200&offset={offset}")
        page = d.get("letters") or d.get("items") or d.get("results") or []
        total = d.get("total", total if total is not None else len(page))
        if not page:
            break
        letters.extend(page)
        offset += len(page)
        time.sleep(0.3)
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    json.dump(letters, open(OUT, "w"))
    json.dump({"fetched_at": datetime.now(timezone.utc).isoformat(), "total_reported": total, "got": len(letters)},
              open(OUT.replace(".json", ".meta.json"), "w"))
    print(f"{len(letters)} letters (door reports total {total}) -> {OUT}")

if __name__ == "__main__":
    main()
