import csv, json, sys, urllib.request EXPECTED_COLS = ["iso3", "iso2", "name", "region", "incomeLevel", "capital", "latitude", "longitude"] CSV_PATH = "/root/troupe-standup/wizard-work/ledger/countries-table-2026-11-02.csv" def fetch(): url = "https://api.worldbank.org/v2/country/all?format=json&per_page=1000" req = urllib.request.Request(url, headers={"User-Agent": "ledger-wizard-extract/1.0"}) with urllib.request.urlopen(req, timeout=60) as r: payload = json.load(r) out = [] for rec in payload[1]: out.append((rec.get("id", ""), rec.get("iso2Code", ""), rec.get("name", ""), ((rec.get("region") or {}).get("value", "")), ((rec.get("incomeLevel") or {}).get("value", "")), rec.get("capitalCity", ""), rec.get("latitude", ""), rec.get("longitude", ""))) return out def main(): fresh = sorted(fetch()) with open(CSV_PATH, newline="") as f: reader = csv.reader(f) header = next(reader) rows = [tuple(r) for r in reader] rows = sorted(rows) errors = [] if header != EXPECTED_COLS: errors.append("header mismatch: %r" % (header,)) if len(rows) != len(fresh): errors.append("row count mismatch: artifact=%d fresh=%d" % (len(rows), len(fresh))) if rows != fresh: for i, (a, b) in enumerate(zip(rows, fresh)): if a != b: errors.append("first diff at row %d: artifact=%r fresh=%r" % (i, a, b)) break if errors: print("FAIL") for e in errors: print(" -", e) sys.exit(1) print("OK: %d rows match fresh World Bank extraction; header=%r" % (len(rows), header)) if __name__ == "__main__": main()