]> git.mxchange.org Git - fba.git/blobdiff - fba/federation/misskey.py
WIP:
[fba.git] / fba / federation / misskey.py
index 598e80e0edc8b9ea6f3ff8f8118ddb1d10aca371..9e5c9af91be977846162f7e35690b94a46b00330 100644 (file)
@@ -20,13 +20,14 @@ from fba import blacklist
 from fba import config
 from fba import fba
 from fba import instances
+from fba import network
 
 def fetch_peers(domain: str) -> list:
     # DEBUG: print(f"DEBUG: domain({len(domain)})={domain} - CALLED!")
-    if type(domain) != str:
+    if not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
     elif domain == "":
-        raise ValueError(f"Parameter 'domain' is empty")
+        raise ValueError("Parameter 'domain' is empty")
 
     # DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
     peers = list()
@@ -39,7 +40,7 @@ def fetch_peers(domain: str) -> list:
     while True:
         # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
         if offset == 0:
-            fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+            fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
                 "sort" : "+pubAt",
                 "host" : None,
                 "limit": step
@@ -47,7 +48,7 @@ def fetch_peers(domain: str) -> list:
                 "Origin": domain
             })
         else:
-            fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+            fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
                 "sort"  : "+pubAt",
                 "host"  : None,
                 "limit" : step,
@@ -80,7 +81,7 @@ def fetch_peers(domain: str) -> list:
             if not "host" in row:
                 print(f"WARNING: row()={len(row)} does not contain key 'host': {row},domain='{domain}'")
                 continue
-            elif type(row["host"]) != str:
+            elif not isinstance(row["host"], str):
                 print(f"WARNING: row[host][]={type(row['host'])} is not 'str'")
                 continue
             elif blacklist.is_blacklisted(row["host"]):
@@ -99,7 +100,7 @@ def fetch_peers(domain: str) -> list:
             break
 
     # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
-    instances.set("total_peers", domain, len(peers))
+    instances.set_data("total_peers", domain, len(peers))
 
     # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
     instances.update_last_instance_fetch(domain)
@@ -108,13 +109,13 @@ def fetch_peers(domain: str) -> list:
     return peers
 
 def fetch_blocks(domain: str) -> dict:
-    print(f"DEBUG: domain='{domain}' - CALLED!")
-    if type(domain) != str:
+    # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
+    if not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
     elif domain == "":
-        raise ValueError(f"Parameter 'domain' is empty")
+        raise ValueError("Parameter 'domain' is empty")
 
-    print("DEBUG: Fetching misskey blocks from domain:", domain)
+    # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
     blocklist = {
         "suspended": [],
         "blocked"  : []
@@ -127,10 +128,10 @@ def fetch_blocks(domain: str) -> dict:
         # instances page-by-page, since that troonware doesn't support
         # sending them all at once
         try:
-            print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
+            # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
             if offset == 0:
-                print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
-                fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+                # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+                fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
                     "sort"     : "+pubAt",
                     "host"     : None,
                     "suspended": True,
@@ -139,8 +140,8 @@ def fetch_blocks(domain: str) -> dict:
                     "Origin": domain
                 })
             else:
-                print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
-                fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+                # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+                fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
                     "sort"     : "+pubAt",
                     "host"     : None,
                     "suspended": True,
@@ -150,15 +151,15 @@ def fetch_blocks(domain: str) -> dict:
                     "Origin": domain
                 })
 
-            print("DEBUG: fetched():", len(fetched))
+            # DEBUG: print("DEBUG: fetched():", len(fetched))
             if len(fetched) == 0:
-                print("DEBUG: Returned zero bytes, exiting loop:", domain)
+                # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
                 break
             elif len(fetched) != config.get("misskey_limit"):
-                print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
+                # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
                 offset = offset + (config.get("misskey_limit") - len(fetched))
             else:
-                print("DEBUG: Raising offset by step:", step)
+                # DEBUG: print("DEBUG: Raising offset by step:", step)
                 offset = offset + step
 
             count = 0
@@ -174,14 +175,14 @@ def fetch_blocks(domain: str) -> dict:
                         }
                     )
 
-            print(f"DEBUG: count={count}")
+            # DEBUG: print(f"DEBUG: count={count}")
             if count == 0:
-                print(f"DEBUG: API is no more returning new instances, aborting loop!")
+                # DEBUG: print("DEBUG: API is no more returning new instances, aborting loop!")
                 break
 
-        except BaseException as e:
-            print("WARNING: Caught error, exiting loop:", domain, e)
-            instances.update_last_error(domain, e)
+        except BaseException as exception:
+            print("WARNING: Caught error, exiting loop:", domain, exception)
+            instances.update_last_error(domain, exception)
             offset = 0
             break
 
@@ -189,8 +190,8 @@ def fetch_blocks(domain: str) -> dict:
         # same shit, different asshole ("blocked" aka full suspend)
         try:
             if offset == 0:
-                print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
-                fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+                # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+                fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
                     "sort"   : "+pubAt",
                     "host"   : None,
                     "blocked": True,
@@ -199,8 +200,8 @@ def fetch_blocks(domain: str) -> dict:
                     "Origin": domain
                 })
             else:
-                print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
-                fetched = fba.post_json_api(domain, "/api/federation/instances", json.dumps({
+                # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
+                fetched = network.post_json_api(domain, "/api/federation/instances", json.dumps({
                     "sort"   : "+pubAt",
                     "host"   : None,
                     "blocked": True,
@@ -210,15 +211,15 @@ def fetch_blocks(domain: str) -> dict:
                     "Origin": domain
                 })
 
-            print("DEBUG: fetched():", len(fetched))
+            # DEBUG: print("DEBUG: fetched():", len(fetched))
             if len(fetched) == 0:
-                print("DEBUG: Returned zero bytes, exiting loop:", domain)
+                # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
                 break
             elif len(fetched) != config.get("misskey_limit"):
-                print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
+                # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
                 offset = offset + (config.get("misskey_limit") - len(fetched))
             else:
-                print("DEBUG: Raising offset by step:", step)
+                # DEBUG: print("DEBUG: Raising offset by step:", step)
                 offset = offset + step
 
             count = 0
@@ -231,21 +232,21 @@ def fetch_blocks(domain: str) -> dict:
                         "reason": None
                     })
 
-            print(f"DEBUG: count={count}")
+            # DEBUG: print(f"DEBUG: count={count}")
             if count == 0:
-                print(f"DEBUG: API is no more returning new instances, aborting loop!")
+                # DEBUG: print("DEBUG: API is no more returning new instances, aborting loop!")
                 break
 
-        except BaseException as e:
-            print("ERROR: Exception during POST:", domain, e)
-            instances.update_last_error(domain, e)
+        except BaseException as exception:
+            print("ERROR: Exception during POST:", domain, exception)
+            instances.update_last_error(domain, exception)
             offset = 0
             break
 
-    print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
+    # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
     instances.update_last_instance_fetch(domain)
 
-    print(f"DEBUG: Returning for domain='{domain}',blocked()={len(blocklist['blocked'])},suspended()={len(blocklist['suspended'])}")
+    # DEBUG: print(f"DEBUG: Returning for domain='{domain}',blocked()={len(blocklist['blocked'])},suspended()={len(blocklist['suspended'])}")
     return {
         "reject"        : blocklist["blocked"],
         "followers_only": blocklist["suspended"]