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:102.0) Gecko/20100101 Firefox/102.0"
11 def get_mastodon_blocks(domain: str) -> dict:
13 "Suspended servers": [],
15 "Limited servers": [],
16 "Silenced servers": [],
20 "Silenced instances": "Silenced servers",
21 "Suspended instances": "Suspended servers",
22 "Gesperrte Server": "Suspended servers",
23 "Gefilterte Medien": "Filtered media",
24 "Stummgeschaltete Server": "Silenced servers",
25 "停止済みのサーバー": "Suspended servers",
26 "メディアを拒否しているサーバー": "Filtered media",
27 "サイレンス済みのサーバー": "Silenced servers",
28 "Serveurs suspendus": "Suspended servers",
29 "Médias filtrés": "Filtered media",
30 "Serveurs limités": "Silenced servers",
35 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
41 for header in doc.find_all("h3"):
42 for line in header.find_next_siblings("table")[0].find_all("tr")[1:]:
43 header_text = header.text
44 if header_text in translations:
45 header_text = translations[header_text]
46 if header_text in blocks:
47 blocks[header_text].append(
49 "domain": line.find("span").text,
50 "hash": line.find("span")["title"][9:],
51 "reason": line.find_all("td")[1].text.strip(),
55 "reject": blocks["Suspended servers"],
56 "media_removal": blocks["Filtered media"],
57 "federated_timeline_removal": blocks["Limited servers"]
58 + blocks["Silenced servers"],
61 def get_friendica_blocks(domain: str) -> dict:
66 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
72 blocklist = doc.find(id="about_blocklist")
73 for line in blocklist.find("table").find_all("tr")[1:]:
76 "domain": line.find_all("td")[0].text.strip(),
77 "reason": line.find_all("td")[1].text.strip()
85 def get_hash(domain: str) -> str:
86 return sha256(domain.encode("utf-8")).hexdigest()
89 def get_type(domain: str) -> str:
91 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
92 if res.status_code == 404:
93 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
94 if res.status_code == 404:
95 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
96 if res.ok and "text/html" in res.headers["content-type"]:
97 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
99 if res.json()["software"]["name"] == "akkoma":
101 elif res.json()["software"]["name"] == "rebased":
103 elif res.json()["software"]["name"] == "hometown":
105 elif res.json()["software"]["name"] == "ecko":
108 return res.json()["software"]["name"]
109 elif res.status_code == 404:
110 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
117 conn = sqlite3.connect("blocks.db")
121 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica')"
124 for blocker, software in c.fetchall():
125 if software == "pleroma":
130 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
131 ).json()["metadata"]["federation"]
132 if "mrf_simple" in federation:
133 for block_level, blocks in (
134 {**federation["mrf_simple"],
135 **{"quarantined_instances": federation["quarantined_instances"]}}
137 for blocked in blocks:
140 blocked == blocked.lower()
141 blocker == blocker.lower()
143 "select domain from instances where domain = ?", (blocked,)
145 if c.fetchone() == None:
147 "insert into instances select ?, ?, ?",
148 (blocked, get_hash(blocked), get_type(blocked)),
151 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
152 (blocker, blocked, block_level),
154 if c.fetchone() == None:
156 "insert into blocks select ?, ?, '', ?",
157 (blocker, blocked, block_level),
161 if "mrf_simple_info" in federation:
162 for block_level, info in (
163 {**federation["mrf_simple_info"],
164 **(federation["quarantined_instances_info"]
165 if "quarantined_instances_info" in federation
168 for blocked, reason in info.items():
169 blocker == blocker.lower()
170 blocked == blocked.lower()
172 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
173 (reason["reason"], blocker, blocked, block_level),
176 except Exception as e:
177 print("error:", e, blocker)
178 elif software == "mastodon":
181 json = get_mastodon_blocks(blocker)
182 for block_level, blocks in json.items():
183 for instance in blocks:
184 blocked, blocked_hash, reason = instance.values()
185 blocked == blocked.lower()
186 blocker == blocker.lower()
187 if blocked.count("*") <= 1:
189 "select hash from instances where hash = ?", (blocked_hash,)
191 if c.fetchone() == None:
193 "insert into instances select ?, ?, ?",
194 (blocked, get_hash(blocked), get_type(blocked)),
197 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
198 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
200 if c.fetchone() == None:
202 "insert into blocks select ?, ?, ?, ?",
205 blocked if blocked.count("*") <= 1 else blocked_hash,
211 except Exception as e:
212 print("error:", e, blocker)
213 elif software == "friendica":
216 json = get_friendica_blocks(blocker)
217 for block_level, blocks in json.items():
218 for instance in blocks:
219 blocked, reason = instance.values()
220 blocked == blocked.lower()
221 blocker == blocker.lower()
223 "select domain from instances where domain = ?", (blocked,)
225 if c.fetchone() == None:
227 "insert into instances select ?, ?, ?",
228 (blocked, get_hash(blocked), get_type(blocked)),
231 "select * from blocks where blocker = ? and blocked = ?",
234 if c.fetchone() == None:
236 "insert into blocks select ?, ?, ?, ?",
245 except Exception as e:
246 print("error:", e, blocker)