From: Roland Häder Date: Sat, 27 May 2023 13:29:54 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a570a124564dfb0164dfd8b9649482606f7bed75;p=fba.git Continued: - introduced fba.is_instance_registered() --- diff --git a/fba.py b/fba.py index 2d8baa4..3f3e30b 100644 --- a/fba.py +++ b/fba.py @@ -724,6 +724,25 @@ def block_instance(blocker: str, blocked: str, reason: str, block_level: str): # NOISY-DEBUG: print("DEBUG: EXIT!") +def is_instance_registered(domain: str) -> bool: + # NOISY-DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!") + # Default is not registered + registered = False + + try: + cursor.execute( + "SELECT rowid FROM instances WHERE domain = ? LIMIT 1", [domain] + ) + + # Check condition + registered = cursor.fetchone() != None + except BaseException as e: + print("ERROR: failed SQL query:", blocker, blocked, reason, block_level, first_added, last_seen, e) + sys.exit(255) + + # NOISY-DEBUG: print("DEBUG: registered='{registered}' - EXIT!") + return registered + def add_instance(domain: str, origin: str, originator: str): # NOISY-DEBUG: print("DEBUG: domain,origin:", domain, origin, originator) if not validators.domain(domain): diff --git a/fetch_blocks.py b/fetch_blocks.py index 7d4ce24..ca6dc1d 100644 --- a/fetch_blocks.py +++ b/fetch_blocks.py @@ -79,16 +79,12 @@ for blocker, software in rows: # NOISY-DEBUG: print("DEBUG: Looked up domain:", blocked) # NOISY-DEBUG: print("DEBUG: Looking up instance by domain:", blocked) - fba.cursor.execute( - "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [blocked] - ) - - if fba.cursor.fetchone() == None: + if not fba.is_instance_registered(blocked): # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker) fba.add_instance(blocked, blocker, argv[0]) fba.cursor.execute( - "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?", + "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", ( blocker, blocked, @@ -158,7 +154,7 @@ for blocker, software in rows: fba.connection.commit() except Exception as e: - print("error:", e, blocker, software) + print(f"ERROR: blocker='{blocker}',software='{software}',exception='{e}'") elif software == "mastodon": print("INFO: blocker:", blocker) try: @@ -250,7 +246,7 @@ for blocker, software in rows: blocked = searchres[0] fba.cursor.execute( - "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?", + "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", ( blocker, blocked if blocked.count("*") <= 1 else blocked_hash, @@ -270,13 +266,13 @@ for blocker, software in rows: else: fba.update_last_seen(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level) - if reason != '': + if reason != "": # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason) fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level) fba.connection.commit() except Exception as e: - print("error:", e, blocker, software) + print(f"ERROR: blocker='{blocker}',software='{software}',exception='{e}'") elif software == "friendica" or software == "misskey" or software == "bookwyrm" or software == "takahe": print("INFO: blocker:", blocker) try: @@ -327,11 +323,7 @@ for blocker, software in rows: blocked = searchres[0] # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked) - fba.cursor.execute( - "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [blocked] - ) - - if fba.cursor.fetchone() == None: + if not fba.is_instance_registered(blocked): # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker) fba.add_instance(blocked, blocker) @@ -339,6 +331,7 @@ for blocker, software in rows: "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?", (blocker, blocked), ) + if fba.cursor.fetchone() == None: fba.block_instance(blocker, blocked, reason, block_level) @@ -357,7 +350,7 @@ for blocker, software in rows: fba.connection.commit() except Exception as e: - print("error:", e, blocker, software) + print(f"ERROR: blocker='{blocker}',software='{software}',exception='{e}'") elif software == "gotosocial": print("INFO: blocker:", blocker) try: @@ -389,16 +382,12 @@ for blocker, software in rows: if searchres != None: blocked = searchres[0] - fba.cursor.execute( - "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [blocked] - ) - - if fba.cursor.fetchone() == None: - # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason) - fba.update_block_reason(reason, blocker, blocked, block_level) + if not fba.is_instance_registered(blocked): + # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker) + fba.add_instance(blocked, blocker) fba.cursor.execute( - "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?", + "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", ( blocker, blocked, @@ -407,6 +396,7 @@ for blocker, software in rows: ) if fba.cursor.fetchone() == None: + # NOISY-DEBUG: print(f"DEBUG: blocker='{blocker}' is blocking '{blocked}' for unknown reason at this point") fba.block_instance(blocker, blocked, "unknown", "reject") blockdict.append( @@ -423,11 +413,12 @@ for blocker, software in rows: for entry in blockdict: if entry["blocked"] == blocked: + # NOISY-DEBUG: print(f"DEBUG: Setting block reason for blocked='{blocked}':'{peer['public_comment']}'") entry["reason"] = peer["public_comment"] fba.connection.commit() except Exception as e: - print("error:", e, blocker, software) + print(f"ERROR: blocker='{blocker}',software='{software}',exception='{e}'") else: print("WARNING: Unknown software:", blocker, software) diff --git a/fetch_instances.py b/fetch_instances.py index 20383cf..34c9df9 100644 --- a/fetch_instances.py +++ b/fetch_instances.py @@ -7,11 +7,7 @@ import fba def fetch_instances(domain: str, origin: str, software: str): # NOISY-DEBUG: print("DEBUG: domain,origin,software:", domain, origin, software) - fba.cursor.execute( - "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [domain] - ) - - if fba.cursor.fetchone() == None: + if not fba.is_instance_registered(domain): # NOISY-DEBUG: print("DEBUG: Adding new domain:", domain, origin) fba.add_instance(domain, origin, sys.argv[0]) @@ -43,16 +39,9 @@ def fetch_instances(domain: str, origin: str, software: str): # NOISY-DEBUG: print("DEBUG: Handling instance:", instance) try: - fba.cursor.execute( - "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [instance] - ) - - if fba.cursor.fetchone() == None: + if not fba.is_instance_registered(instance): # NOISY-DEBUG: print("DEBUG: Adding new instance:", instance, domain) fba.add_instance(instance, domain, sys.argv[0]) - - fba.connection.commit() - except BaseException as e: print(f"ERROR: instance='{instance}',exception:'{e}'") continue