]> git.mxchange.org Git - fba.git/blobdiff - fba/network.py
WIP:
[fba.git] / fba / network.py
index 78bb30460cdbbb3ebdcea59db448343179f025d0..4894c0f7e9bfa17ac44eb22415867f742204cbd9 100644 (file)
@@ -19,6 +19,7 @@ import reqto
 import requests
 
 from fba import config
+from fba import csrf
 from fba import fba
 from fba import instances
 
@@ -46,13 +47,17 @@ def post_json_api(domain: str, path: str, parameter: str, extra_headers: dict =
     elif not isinstance(parameter, str):
         raise ValueError(f"parameter[]={type(parameter)} is not 'str'")
 
-    # DEBUG: print("DEBUG: Sending POST to domain,path,parameter:", domain, path, parameter, extra_headers)
+    # DEBUG: print(f"DEBUG: Determining if CSRF header needs to be sent for domain='{domain}' ...")
+    headers = csrf.determine(domain, {**api_headers, **extra_headers})
+
     data = {}
+
     try:
+        # DEBUG: print(f"DEBUG: Sending POST to domain='{domain}',path='{path}',parameter='{parameter}',extra_headers({len(extra_headers)})={extra_headers}")
         response = reqto.post(
             f"https://{domain}{path}",
             data=parameter,
-            headers={**api_headers, **extra_headers},
+            headers=headers,
             timeout=(config.get("connection_timeout"), config.get("read_timeout"))
         )
 
@@ -113,55 +118,6 @@ def send_bot_post(domain: str, blocklist: dict):
 
     return True
 
-def fetch_friendica_blocks(domain: str) -> dict:
-    # 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("Parameter 'domain' is empty")
-
-    # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
-    blocked = list()
-
-    try:
-        doc = bs4.BeautifulSoup(
-            fetch_response(domain, "/friendica", web_headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
-            "html.parser",
-        )
-    except BaseException as exception:
-        print("WARNING: Failed to fetch /friendica from domain:", domain, exception)
-        instances.update_last_error(domain, exception)
-        return {}
-
-    blocklist = doc.find(id="about_blocklist")
-
-    # Prevents exceptions:
-    if blocklist is None:
-        # DEBUG: print("DEBUG: Instance has no block list:", domain)
-        return {}
-
-    table = blocklist.find("table")
-
-    # DEBUG: print(f"DEBUG: table[]='{type(table)}'")
-    if table.find("tbody"):
-        rows = table.find("tbody").find_all("tr")
-    else:
-        rows = table.find_all("tr")
-
-    # DEBUG: print(f"DEBUG: Found rows()={len(rows)}")
-    for line in rows:
-        # DEBUG: print(f"DEBUG: line='{line}'")
-        blocked.append({
-            "domain": fba.tidyup_domain(line.find_all("td")[0].text),
-            "reason": fba.tidyup_reason(line.find_all("td")[1].text)
-        })
-        # DEBUG: print("DEBUG: Next!")
-
-    # DEBUG: print("DEBUG: Returning blocklist() for domain:", domain, len(blocklist))
-    return {
-        "reject": blocked
-    }
-
 def fetch_response(domain: str, path: str, headers: dict, timeout: list) -> requests.models.Response:
     # DEBUG: print(f"DEBUG: domain='{domain}',path='{path}',headers()={len(headers)},timeout={timeout} - CALLED!")
     if not isinstance(domain, str):
@@ -173,13 +129,17 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: list) -> requ
     elif path == "":
         raise ValueError("Parameter 'path' is empty")
 
+    # DEBUG: print(f"DEBUG: Determining if CSRF header needs to be sent for domain='{domain}',headers()='{len(headers)}' ...")
+    headers = csrf.determine(domain, headers)
+
     try:
-        # DEBUG: print(f"DEBUG: Sending request to '{domain}{path}' ...")
+        # DEBUG: print(f"DEBUG: Sending GET request to '{domain}{path}' ...")
         response = reqto.get(
             f"https://{domain}{path}",
             headers=headers,
             timeout=timeout
-        );
+        )
+
     except requests.exceptions.ConnectionError as exception:
         # DEBUG: print(f"DEBUG: Fetching '{path}' from '{domain}' failed. exception[{type(exception)}]='{str(exception)}'")
         instances.update_last_error(domain, exception)