]> git.mxchange.org Git - fba.git/blobdiff - fba/networks/mastodon.py
WIP(?):
[fba.git] / fba / networks / mastodon.py
index 25d11f416595afee42a0238b6a7032e35565389b..6b1b967f7418a97380faa2953d23b69324943b8c 100644 (file)
 import inspect
 
 import bs4
+import requests
 import validators
 
 from fba import blacklist
 from fba import blocks
 from fba import config
+from fba import csrf
 from fba import fba
 from fba import instances
 from fba import network
@@ -128,35 +130,42 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
     elif nodeinfo_url == "":
         raise ValueError("Parameter 'nodeinfo_url' is empty")
 
+    print(f"DEBUG: Checking CSRF for domain='{domain}'")
+    headers = csrf.determine(domain, dict())
+
     try:
         # json endpoint for newer mastodongs
         blockdict = list()
-        try:
-            rows = {
-                "reject"        : [],
-                "media_removal" : [],
-                "followers_only": [],
-                "report_removal": [],
-            }
+        rows = {
+            "reject"        : [],
+            "media_removal" : [],
+            "followers_only": [],
+            "report_removal": [],
+        }
 
-            # DEBUG: print("DEBUG: Querying API domain_blocks:", domain)
-            data = network.get_json_api(
-                domain,
-                "/api/v1/instance/domain_blocks",
-                (config.get("connection_timeout"), config.get("read_timeout"))
-            )
-
-            if "error_message" in data:
-                print(f"WARNING: Was not able to fetch domain_blocks from domain='{domain}': status_code='{data['status_code']}',error_message='{data['error_message']}'")
-                instances.update_last_error(domain, data)
+        # DEBUG: print("DEBUG: Querying API domain_blocks:", domain)
+        data = network.get_json_api(
+            domain,
+            "/api/v1/instance/domain_blocks",
+            headers,
+            (config.get("connection_timeout"), config.get("read_timeout"))
+        )
 
+        if "error_message" in data:
+            print(f"WARNING: Was not able to fetch domain_blocks from domain='{domain}': status_code='{data['status_code']}',error_message='{data['error_message']}'")
+            instances.update_last_error(domain, data)
+        else:
+            # Getting blocklist
             blocklist = data["json"]
+
+        if len(blocklist) > 0:
             print(f"INFO: Checking {len(blocklist)} entries from domain='{domain}',software='mastodon' ...")
             for block in blocklist:
+                # Map block -> entry
                 entry = {
-                    'domain': block['domain'],
-                    'hash'  : block['digest'],
-                    'reason': block['comment']
+                    "domain": block["domain"],
+                    "hash"  : block["digest"],
+                    "reason": block["comment"]
                 }
 
                 # DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
@@ -174,9 +183,8 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
                     rows['report_removal'].append(entry)
                 else:
                     print("WARNING: Unknown severity:", block['severity'], block['domain'])
-
-        except BaseException as exception:
-            # DEBUG: print(f"DEBUG: Failed, trying mastodon-specific fetches: domain='{domain}',exception[{type(exception)}]={str(exception)}")
+        else:
+            # DEBUG: print(f"DEBUG: domain='{domain}' has returned zero rows, trying /about/more page ...")
             rows = fetch_blocks_from_about(domain)
 
         print(f"INFO: Checking {len(rows.items())} entries from domain='{domain}',software='mastodon' ...")
@@ -258,7 +266,7 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
 
         # DEBUG: print("DEBUG: Committing changes ...")
         fba.connection.commit()
-    except BaseException as exception:
+    except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as exception:
         print(f"ERROR: domain='{domain}',software='mastodon',exception[{type(exception)}]:'{str(exception)}'")
 
     # DEBUG: print("DEBUG: EXIT!")