]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 27 May 2023 13:29:54 +0000 (15:29 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 27 May 2023 13:29:54 +0000 (15:29 +0200)
- introduced fba.is_instance_registered()

fba.py
fetch_blocks.py
fetch_instances.py

diff --git a/fba.py b/fba.py
index 2d8baa4227571a6ff84b1e1c3d4668ddff5282ad..3f3e30b6fc428e9201fc55e9a98becb78d9c0bab 100644 (file)
--- 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):
index 7d4ce24c2135de007af65561f4d4954e2e24b581..ca6dc1d17c2144259abd43a0b2f1702aa35215d7 100644 (file)
@@ -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)
 
index 20383cf0d08801eddba2bec0969155964681d2eb..34c9df952013e0a221a68daa589ededa91ada751 100644 (file)
@@ -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