From 08633e81b41b131d0d45d3f4d30b3cf8adcb9ec7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 22 May 2023 04:11:48 +0200 Subject: [PATCH] Continued: - added support for very old Mastodon instances, e.g. 2.x has no "software" in nodeinfo --- fba.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/fba.py b/fba.py index 6d20d6e..954318d 100644 --- a/fba.py +++ b/fba.py @@ -153,7 +153,8 @@ def fetch_nodeinfo(domain: str) -> list: # NOISY-DEBUG: print("DEBUG: Cannot fetch API request:", request) pass - if json is None: + # NOISY-DEBUG: print("DEBUG: json[]:", type(json)) + if json is None or len(json) == 0: print("WARNING: Failed fetching nodeinfo from domain:", domain) # NOISY-DEBUG: print("DEBUG: Returning json[]:", type(json)) @@ -164,11 +165,28 @@ def determine_software(domain: str) -> str: software = None json = fetch_nodeinfo(domain) - if len(json) == 0: - print("DEBUG: Could not determine software type:", domain) + # NOISY-DEBUG: print("DEBUG: json[]:", type(json)) + + if json is None or len(json) == 0: + # NOISY-DEBUG: print("DEBUG: Could not determine software type:", domain) + return None + + # NOISY-DEBUG: print("DEBUG: json():", len(json), json) + if "software" not in json or "name" not in json["software"]: + print("WARNING: JSON response does not include [software][name], guessing ...") + found = 0 + for element in {"uri", "title", "description", "email", "version", "urls", "stats", "thumbnail", "languages", "contact_account"}: + if element in json: + found = found + 1 + + # NOISY-DEBUG: print("DEBUG: Found elements:", found) + if found == len(json): + # NOISY-DEBUG: print("DEBUG: Maybe is Mastodon:", domain) + return "mastodon" + + print("WARNING: Cannot guess software type:", domain, found, len(json)) return None - # NOISY-DEBUG: print("DEBUG: json():", len(json)) software = tidyup(json["software"]["name"]) # NOISY-DEBUG: print("DEBUG: tidyup software:", software) @@ -193,7 +211,7 @@ def determine_software(domain: str) -> str: return software def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str): - # NOISY: print("--- Updating block reason:", reason, blocker, blocked, block_level) + # NOISY: # NOISY-DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level) try: cursor.execute( "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''", @@ -211,7 +229,7 @@ def update_block_reason(reason: str, blocker: str, blocked: str, block_level: st sys.exit(255) def update_last_seen(blocker: str, blocked: str, block_level: str): - # NOISY: print("--- Updating last_seen for:", blocker, blocked, block_level) + # NOISY: # NOISY-DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level) try: cursor.execute( "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?", @@ -236,7 +254,7 @@ def block_instance(blocker: str, blocked: str, reason: str, block_level: str): print("WARNING: Bad blocked:", blocked) raise - print("--- New block:", blocker, blocked, reason, block_level, first_added, last_seen) + print("INFO: New block:", blocker, blocked, reason, block_level, first_added, last_seen) try: cursor.execute( "INSERT INTO blocks (blocker, blocked, reason, block_level, first_added, last_seen) VALUES(?, ?, ?, ?, ?, ?)", @@ -266,7 +284,7 @@ def add_instance(domain: str, origin: str, originator: str): software = determine_software(domain) # NOISY-DEBUG: print("DEBUG: Determined software:", software) - print(f"--- Adding new instance {domain} (origin: {origin})") + print(f"INFO: Adding new instance {domain} (origin: {origin})") try: cursor.execute( "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)", -- 2.39.5