From 623c81d406a86d6cfd37b9fec2c618c08656a8b7 Mon Sep 17 00:00:00 2001 From: Robert Rapp Date: Mon, 19 Jan 2026 19:17:05 +0100 Subject: [PATCH] fix: fallback PeeringDB info_type when empty --- asn-updater/update.py | 55 ++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/asn-updater/update.py b/asn-updater/update.py index 806489d..6a4ed4f 100644 --- a/asn-updater/update.py +++ b/asn-updater/update.py @@ -52,10 +52,10 @@ def pdb_headers(): # PeeringDB API Key (optional) return {"Accept": "application/json", "Authorization": f"Api-Key {PDB_API_KEY}"} -def fetch_pdb_page(skip: int): +def fetch_pdb_page(skip: int, info_type: str): url = f"{PDB_BASE}/api/net" params = { - "info_type": INFO_TYPE, + "info_type": info_type, "limit": LIMIT, "skip": skip, "fields": "asn,status,info_type", @@ -65,21 +65,37 @@ def fetch_pdb_page(skip: int): j = r.json() return j.get("data", []) -def update_nren_asns() -> None: +def update_nren_asns() -> str: + info_types = [INFO_TYPE] + # Alternate label seen in PeeringDB deployments. + if INFO_TYPE != "Research and Education": + info_types.append("Research and Education") + if INFO_TYPE != "Educational/Research": + info_types.append("Educational/Research") + asns = set() - skip = 0 - while True: - data = fetch_pdb_page(skip) - for obj in data: - if obj.get("status") != "ok": - continue - asn = obj.get("asn") - if isinstance(asn, int) and asn > 0: - asns.add(asn) - if len(data) < LIMIT: + used_info_type = INFO_TYPE + for info_type in info_types: + asns.clear() + skip = 0 + while True: + data = fetch_pdb_page(skip, info_type) + for obj in data: + if obj.get("status") != "ok": + continue + asn = obj.get("asn") + if isinstance(asn, int) and asn > 0: + asns.add(asn) + if len(data) < LIMIT: + break + skip += LIMIT + time.sleep(1.1) # sehr konservativ + if asns: + used_info_type = info_type break - skip += LIMIT - time.sleep(1.1) # sehr konservativ + + if not asns: + print(f"[warn] no ASNs found for info_type(s)={info_types}") out_txt = os.path.join(OUT_DIR, "nren_asns.txt") with tempfile.NamedTemporaryFile("w", delete=False, dir=OUT_DIR) as f: @@ -88,11 +104,12 @@ def update_nren_asns() -> None: tmp_path = f.name os.replace(tmp_path, out_txt) os.chmod(out_txt, 0o644) + return used_info_type -def write_meta(): +def write_meta(info_type: str): meta = { "updated_at_unix": int(time.time()), - "info_type": INFO_TYPE, + "info_type": info_type, "pdb_base": PDB_BASE, } with open(os.path.join(OUT_DIR, "metadata.json"), "w") as f: @@ -102,8 +119,8 @@ def write_meta(): def main(): os.makedirs(OUT_DIR, exist_ok=True) download_maxmind_mmdb() - update_nren_asns() - write_meta() + used_info_type = update_nren_asns() + write_meta(used_info_type) print("[ok] updated mmdb + nren_asns") if __name__ == "__main__":