]> git.mxchange.org Git - fba.git/commitdiff
refactored get_mastodon_blocks
authorEnju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com>
Fri, 8 Apr 2022 21:12:01 +0000 (23:12 +0200)
committerEnju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com>
Fri, 8 Apr 2022 21:12:01 +0000 (23:12 +0200)
fetch_blocks.py

index 60d90c1bfff8b5acd683a9e6b7e76c815c4b6c58..7a3de5f15f910792c6517b4d398ff0e2667370ab 100644 (file)
@@ -8,24 +8,36 @@ headers = {
 }
 
 def get_mastodon_blocks(domain: str) -> dict:
-    try:
-        reject = []
-        media_removal = []
-        federated_timeline_removal = []
+    blocks = {
+        "Suspended servers": [],
+        "Filtered media": [],
+        "Limited servers": [],
+        "Silenced servers": [],
+    }
 
-        doc = BeautifulSoup(get(f"https://{domain}/about/more", headers=headers, timeout=5).text, "html.parser")
-        for header in doc.find_all("h3"):
-            if header.text == "Suspended servers":
-                for line in header.find_next_siblings("table")[0].find_all("tr")[1:]:
-                    reject.append({"domain": line.find("span").text, "hash": line.find("span")["title"][9:], "reason": line.find_all("td")[1].text.strip()})
-            elif header.text == "Filtered media":
-                for line in header.find_next_siblings("table")[0].find_all("tr")[1:]:
-                    media_removal.append({"domain": line.find("span").text, "hash": line.find("span")["title"][9:], "reason": line.find_all("td")[1].text.strip()})
-            elif header.text in ["Limited servers", "Silenced servers"]:
-                for line in header.find_next_siblings("table")[0].find_all("tr")[1:]:
-                    federated_timeline_removal.append({"domain": line.find("span").text, "hash": line.find("span")["title"][9:], "reason": line.find_all("td")[1].text.strip()})
-    finally:
-        return {"reject": reject, "media_removal": media_removal, "federated_timeline_removal": federated_timeline_removal}
+    try:
+        doc = BeautifulSoup(
+            get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
+            "html.parser",
+        )
+    except:
+        return {}
+        
+    for header in doc.find_all("h3"):
+        for line in header.find_next_siblings("table")[0].find_all("tr")[1:]:
+            if header.text in blocks:
+                blocks[header.text].append(
+                    {
+                        "domain": line.find("span").text,
+                        "hash": line.find("span")["title"][9:],
+                        "reason": line.find_all("td")[1].text.strip(),
+                    }
+                )
+    return {
+        "reject": blocks["Suspended servers"],
+        "media_removal": blocks["Filtered media"],
+        "federated_timeline_removal": blocks["Limited servers"] + blocks["Silenced servers"],
+    }
 
 
 def get_type(domain: str) -> str: