52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Steg 3 fixad: GLEIF via initialRegistrationDate-split (undviker 10k djup-gräns)."""
|
|
import json, time, requests
|
|
|
|
OUT = "/data/0pt/output/gleif_se.jsonl"
|
|
sess = requests.Session()
|
|
total = 0
|
|
YEARS = [(f"{y}-01-01", f"{y}-12-31") for y in range(2012, 2027)]
|
|
with open(OUT, "w") as out:
|
|
for y0, y1 in YEARS:
|
|
page = 1
|
|
while True:
|
|
ok = False
|
|
for attempt in range(5):
|
|
try:
|
|
r = sess.get("https://api.gleif.org/api/v1/lei-records", params={
|
|
"filter[entity.legalAddress.country]": "SE",
|
|
"filter[registration.initialRegistrationDate]": f"{y0}..{y1}",
|
|
"page[size]": 200, "page[number]": page,
|
|
}, timeout=30)
|
|
if r.status_code == 429:
|
|
time.sleep(5); continue
|
|
r.raise_for_status()
|
|
ok = True; break
|
|
except Exception as e:
|
|
if attempt == 4:
|
|
print(f" FAIL {y0} p{page}: {e}", flush=True)
|
|
break
|
|
time.sleep(2 ** attempt)
|
|
if not ok: break
|
|
d = r.json()
|
|
items = d.get("data", [])
|
|
if not items: break
|
|
for it in items:
|
|
a = it.get("attributes", {})
|
|
e = a.get("entity", {})
|
|
reg = a.get("registration", {})
|
|
out.write(json.dumps({
|
|
"lei": a.get("lei"),
|
|
"orgnr": (e.get("registeredAs") or "").strip(),
|
|
"namn": (e.get("legalName") or {}).get("name"),
|
|
"status": e.get("status"),
|
|
"reg_status": reg.get("status"),
|
|
}, ensure_ascii=False) + "\n")
|
|
total += 1
|
|
meta = d.get("meta", {}).get("pagination", {})
|
|
if page >= meta.get("lastPage", 1): break
|
|
page += 1
|
|
time.sleep(0.3)
|
|
print(f" {y0[:4]}: klart (totalt {total:,})", flush=True)
|
|
print(f"[KLAR] {total:,} LEI -> {OUT}")
|