From: Roland Häder Date: Sat, 20 May 2023 07:39:57 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ee44628a02f8fb81f981150b57f8a1d423c6473a;p=fba.git Continued: - introduced fba.fetch_nodeinfo() --- diff --git a/fba.py b/fba.py index 18f2b70..c8e2ed0 100644 --- a/fba.py +++ b/fba.py @@ -55,40 +55,53 @@ def post_json_api(domain: str, path: str, data: str) -> list: # NOISY-DEBUG: print("DEBUG: Returning doc():", len(doc)) return doc -def determine_software(domain: str) -> str: - # NOISY-DEBUG: print("DEBUG: Determining software for domain:", domain) - software = None +def fetch_nodeinfo(domain: str) -> list: + print("DEBUG: Fetching nodeinfo from domain:", domain) + json = None try: + print("DEBUG: Fetching 2.1.json from domain:", domain) res = reqto.get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5) - # NOISY-DEBUG: print("DEBUG: domain,res.ok,res.status_code:", domain, res.ok, res.status_code) - if res.status_code == 404: + if res.status_code == 404 or "text/html" in res.headers["content-type"]: + print("DEBUG: Fetching 2.0 from domain:", domain) res = reqto.get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5) - if res.status_code == 404: + if res.status_code == 404 or "text/html" in res.headers["content-type"]: + print("DEBUG: Fetching 2.0.json from domain:", domain) res = reqto.get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5) if res.ok and "text/html" in res.headers["content-type"]: + print("DEBUG: Fetching 2.1 from domain:", domain) res = reqto.get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5) - if res.ok: - json = res.json() - # NOISY-DEBUG: print("DEBUG: json():", len(json)) - - if json["software"]["name"] in ["akkoma", "rebased"]: - software = "pleroma" - elif json["software"]["name"] in ["hometown", "ecko"]: - software = "mastodon" - elif json["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]: - software = "misskey" - else: - software = json["software"]["name"] - elif res.status_code == 404: + if res.status_code == 404 or "text/html" in res.headers["content-type"]: + print("DEBUG: Fetching /api/v1/instance from domain:", domain) res = reqto.get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5) + + print("DEBUG: domain,res.ok,res.status_code:", domain, res.ok, res.status_code) if res.ok: - software = "mastodon" + json = res.json() + except: - print("WARNING: Failed fetching instance meta data:", domain, software) + print("WARNING: Failed fetching nodeinfo from domain:", domain) + + print("DEBUG: Returning json():", len(json)) + return json + +def determine_software(domain: str) -> str: + # NOISY-DEBUG: print("DEBUG: Determining software for domain:", domain) + software = None + json = fetch_nodeinfo(domain) + # NOISY-DEBUG: print("DEBUG: json():", len(json)) + + if json["software"]["name"] in ["akkoma", "rebased"]: + software = "pleroma" + elif json["software"]["name"] in ["hometown", "ecko"]: + software = "mastodon" + elif json["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]: + software = "misskey" + else: + software = json["software"]["name"] # NOISY-DEBUG: print("DEBUG: Returning domain,software:", domain, software) return software diff --git a/fetch_blocks.py b/fetch_blocks.py index 170e52a..2cc6180 100644 --- a/fetch_blocks.py +++ b/fetch_blocks.py @@ -23,8 +23,12 @@ for blocker, software in fba.c.fetchall(): print("INFO: blocker:", blocker) try: # Blocks - federation = reqto.get(f"https://{blocker}/nodeinfo/2.1.json", headers=fba.headers, timeout=5 - ).json()["metadata"]["federation"] + json = fba.fetch_nodeinfo(blocker) + if json is None: + print("WARNING: Could not fetch nodeinfo from blocker:", blocker) + continue + + federation = json["metadata"]["federation"] if "enabled" in federation: # NOISY-DEBUG: print("DEBUG: Instance has no block list to analyze:", blocker)