fix: fallback PeeringDB info_type when empty

This commit is contained in:
2026-01-19 19:17:05 +01:00
parent 92d7a19186
commit 623c81d406

View File

@@ -52,10 +52,10 @@ def pdb_headers():
# PeeringDB API Key (optional) # PeeringDB API Key (optional)
return {"Accept": "application/json", "Authorization": f"Api-Key {PDB_API_KEY}"} 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" url = f"{PDB_BASE}/api/net"
params = { params = {
"info_type": INFO_TYPE, "info_type": info_type,
"limit": LIMIT, "limit": LIMIT,
"skip": skip, "skip": skip,
"fields": "asn,status,info_type", "fields": "asn,status,info_type",
@@ -65,21 +65,37 @@ def fetch_pdb_page(skip: int):
j = r.json() j = r.json()
return j.get("data", []) 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() asns = set()
skip = 0 used_info_type = INFO_TYPE
while True: for info_type in info_types:
data = fetch_pdb_page(skip) asns.clear()
for obj in data: skip = 0
if obj.get("status") != "ok": while True:
continue data = fetch_pdb_page(skip, info_type)
asn = obj.get("asn") for obj in data:
if isinstance(asn, int) and asn > 0: if obj.get("status") != "ok":
asns.add(asn) continue
if len(data) < LIMIT: 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 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") out_txt = os.path.join(OUT_DIR, "nren_asns.txt")
with tempfile.NamedTemporaryFile("w", delete=False, dir=OUT_DIR) as f: with tempfile.NamedTemporaryFile("w", delete=False, dir=OUT_DIR) as f:
@@ -88,11 +104,12 @@ def update_nren_asns() -> None:
tmp_path = f.name tmp_path = f.name
os.replace(tmp_path, out_txt) os.replace(tmp_path, out_txt)
os.chmod(out_txt, 0o644) os.chmod(out_txt, 0o644)
return used_info_type
def write_meta(): def write_meta(info_type: str):
meta = { meta = {
"updated_at_unix": int(time.time()), "updated_at_unix": int(time.time()),
"info_type": INFO_TYPE, "info_type": info_type,
"pdb_base": PDB_BASE, "pdb_base": PDB_BASE,
} }
with open(os.path.join(OUT_DIR, "metadata.json"), "w") as f: with open(os.path.join(OUT_DIR, "metadata.json"), "w") as f:
@@ -102,8 +119,8 @@ def write_meta():
def main(): def main():
os.makedirs(OUT_DIR, exist_ok=True) os.makedirs(OUT_DIR, exist_ok=True)
download_maxmind_mmdb() download_maxmind_mmdb()
update_nren_asns() used_info_type = update_nren_asns()
write_meta() write_meta(used_info_type)
print("[ok] updated mmdb + nren_asns") print("[ok] updated mmdb + nren_asns")
if __name__ == "__main__": if __name__ == "__main__":