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"],
59 def get_friendica_blocks(domain: str) -> dict:
64 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
70 blocklist = doc.find(id="about_blocklist")
71 for line in blocklist.find("table").find_all("tr")[1:]:
74 "domain": line.find_all("td")[0].text.strip(),
75 "reason": line.find_all("td")[1].text.strip()
83 def get_hash(domain: str) -> str:
84 return sha256(domain.encode("utf-8")).hexdigest()
87 def get_type(domain: str) -> str:
89 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
90 if res.status_code == 404:
91 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
92 if res.status_code == 404:
93 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
94 if res.ok and "text/html" in res.headers["content-type"]:
95 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
97 if res.json()["software"]["name"] == "akkoma":
99 elif res.json()["software"]["name"] == "hometown":
102 return res.json()["software"]["name"]
103 elif res.status_code == 404:
104 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
111 conn = sqlite3.connect("blocks.db")
115 "select domain, software from instances where software in ('pleroma', 'mastodon')"
118 for blocker, software in c.fetchall():
119 if software == "pleroma":
124 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
125 ).json()["metadata"]["federation"]
126 if "mrf_simple" in federation:
127 for block_level, blocks in (
128 {**federation["mrf_simple"],
129 **{"quarantined_instances": federation["quarantined_instances"]}}
131 for blocked in blocks:
134 blocked == blocked.lower()
135 blocker == blocker.lower()
137 "select domain from instances where domain = ?", (blocked,)
139 if c.fetchone() == None:
141 "insert into instances select ?, ?, ?",
142 (blocked, get_hash(blocked), get_type(blocked)),
145 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
146 (blocker, blocked, block_level),
148 if c.fetchone() == None:
150 "insert into blocks select ?, ?, '', ?",
151 (blocker, blocked, block_level),
155 if "mrf_simple_info" in federation:
156 for block_level, info in (
157 {**federation["mrf_simple_info"],
158 **(federation["quarantined_instances_info"]
159 if "quarantined_instances_info" in federation
162 for blocked, reason in info.items():
163 blocker == blocker.lower()
164 blocked == blocked.lower()
166 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
167 (reason["reason"], blocker, blocked, block_level),
170 except Exception as e:
171 print("error:", e, blocker)
172 elif software == "mastodon":
175 json = get_mastodon_blocks(blocker)
176 for block_level, blocks in json.items():
177 for instance in blocks:
178 blocked, blocked_hash, reason = instance.values()
179 blocked == blocked.lower()
180 blocker == blocker.lower()
181 if blocked.count("*") <= 1:
183 "select hash from instances where hash = ?", (blocked_hash,)
185 if c.fetchone() == None:
187 "insert into instances select ?, ?, ?",
188 (blocked, get_hash(blocked), get_type(blocked)),
191 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
192 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
194 if c.fetchone() == None:
196 "insert into blocks select ?, ?, ?, ?",
199 blocked if blocked.count("*") <= 1 else blocked_hash,
205 except Exception as e:
206 print("error:", e, blocker)
207 elif software == "friendica"
210 json = get_friendica_blocks(blocker)
211 for blocks in json.items():
212 for instance in blocks:
213 blocked, reason = instance.values()
214 blocked == blocked.lower()
215 blocker == blocker.lower()
217 "select domain from instances where domain = ?", (blocked,)
219 if c.fetchone() == None:
221 "insert into instances select ?, ?, ?",
222 (blocked, get_hash(blocked), get_type(blocked)),
225 "select * from blocks where blocker = ? and blocked = ?",
228 if c.fetchone() == None:
230 "insert into blocks select ?, ?, ?, ?",
239 except Exception as e:
240 print("error:", e, blocker)