fix: fallback PeeringDB info_type when empty
This commit is contained in:
@@ -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,11 +65,21 @@ 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()
|
||||||
|
used_info_type = INFO_TYPE
|
||||||
|
for info_type in info_types:
|
||||||
|
asns.clear()
|
||||||
skip = 0
|
skip = 0
|
||||||
while True:
|
while True:
|
||||||
data = fetch_pdb_page(skip)
|
data = fetch_pdb_page(skip, info_type)
|
||||||
for obj in data:
|
for obj in data:
|
||||||
if obj.get("status") != "ok":
|
if obj.get("status") != "ok":
|
||||||
continue
|
continue
|
||||||
@@ -80,6 +90,12 @@ def update_nren_asns() -> None:
|
|||||||
break
|
break
|
||||||
skip += LIMIT
|
skip += LIMIT
|
||||||
time.sleep(1.1) # sehr konservativ
|
time.sleep(1.1) # sehr konservativ
|
||||||
|
if asns:
|
||||||
|
used_info_type = info_type
|
||||||
|
break
|
||||||
|
|
||||||
|
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__":
|
||||||
|
|||||||
Reference in New Issue
Block a user