1 from requests import get
2 from hashlib import sha256
4 from bs4 import BeautifulSoup
7 "user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
11 def get_mastodon_blocks(domain: str) -> dict:
13 "Suspended servers": [],
15 "Limited servers": [],
16 "Silenced servers": [],
20 "Gesperrte Server": "Suspended servers",
21 "Gefilterte Medien": "Filtered media",
22 "Stummgeschaltete Server": "Silenced servers",
23 "停止済みのサーバー": "Suspended servers",
24 "メディアを拒否しているサーバー": "Filtered media",
25 "サイレンス済みのサーバー": "Silenced servers",
26 "Serveurs suspendus": "Suspended servers",
27 "Médias filtrés": "Filtered media",
28 "Serveurs limités": "Silenced servers",
33 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
39 for header in doc.find_all("h3"):
40 for line in header.find_next_siblings("table")[0].find_all("tr")[1:]:
41 header_text = header.text
42 if header_text in translations:
43 header_text = translations[header_text]
44 if header_text in blocks:
45 blocks[header_text].append(
47 "domain": line.find("span").text,
48 "hash": line.find("span")["title"][9:],
49 "reason": line.find_all("td")[1].text.strip(),
53 "reject": blocks["Suspended servers"],
54 "media_removal": blocks["Filtered media"],
55 "federated_timeline_removal": blocks["Limited servers"]
56 + blocks["Silenced servers"],
60 def get_hash(domain: str) -> str:
61 return sha256(domain.encode("utf-8")).hexdigest()
64 def get_type(domain: str) -> str:
66 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
67 if res.status_code == 404:
68 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
69 if res.ok and "text/html" in res.headers["content-type"]:
70 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
72 if res.json()["software"]["name"] == "akkoma":
74 elif res.json()["software"]["name"] == "hometown":
77 return res.json()["software"]["name"]
78 elif res.status_code == 404:
79 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
86 conn = sqlite3.connect("blocks.db")
90 "select domain, software from instances where software in ('pleroma', 'mastodon')"
93 for blocker, software in c.fetchall():
94 if software == "pleroma":
99 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
100 ).json()["metadata"]["federation"]
101 if "mrf_simple" in federation:
102 for block_level, blocks in (
103 {**federation["mrf_simple"],
104 **{"quarantined_instances": federation["quarantined_instances"]}}
106 for blocked in blocks:
109 blocked == blocked.lower()
110 blocker == blocker.lower()
112 "select domain from instances where domain = ?", (blocked,)
114 if c.fetchone() == None:
116 "insert into instances select ?, ?, ?",
117 (blocked, get_hash(blocked), get_type(blocked)),
120 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
121 (blocker, blocked, block_level),
123 if c.fetchone() == None:
125 "insert into blocks select ?, ?, '', ?",
126 (blocker, blocked, block_level),
130 if "mrf_simple_info" in federation:
131 for block_level, info in (
132 {**federation["mrf_simple_info"],
133 **(federation["quarantined_instances_info"]
134 if "quarantined_instances_info" in federation
137 for blocked, reason in info.items():
138 blocker == blocker.lower()
139 blocked == blocked.lower()
141 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
142 (reason["reason"], blocker, blocked, block_level),
145 except Exception as e:
146 print("error:", e, blocker)
147 elif software == "mastodon":
150 json = get_mastodon_blocks(blocker)
151 for block_level, blocks in json.items():
152 for instance in blocks:
153 blocked, blocked_hash, reason = instance.values()
154 blocked == blocked.lower()
155 blocker == blocker.lower()
156 if blocked.count("*") <= 1:
158 "select hash from instances where hash = ?", (blocked_hash,)
160 if c.fetchone() == None:
162 "insert into instances select ?, ?, ?",
163 (blocked, get_hash(blocked), get_type(blocked)),
166 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
167 (blocker, blocked, block_level),
169 if c.fetchone() == None:
171 "insert into blocks select ?, ?, ?, ?",
174 blocked if blocked.count("*") <= 1 else blocked_hash,
180 except Exception as e:
181 print("error:", e, blocker)