]> git.mxchange.org Git - fba.git/blob - fetch_blocks.py
Adding another spastic fork
[fba.git] / fetch_blocks.py
1 from requests import get
2 from hashlib import sha256
3 import sqlite3
4 from bs4 import BeautifulSoup
5
6 headers = {
7     "user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
8 }
9
10
11 def get_mastodon_blocks(domain: str) -> dict:
12     blocks = {
13         "Suspended servers": [],
14         "Filtered media": [],
15         "Limited servers": [],
16         "Silenced servers": [],
17     }
18
19     translations = {
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",
29     }
30
31     try:
32         doc = BeautifulSoup(
33             get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
34             "html.parser",
35         )
36     except:
37         return {}
38
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(
46                     {
47                         "domain": line.find("span").text,
48                         "hash": line.find("span")["title"][9:],
49                         "reason": line.find_all("td")[1].text.strip(),
50                     }
51                 )
52     return {
53         "reject": blocks["Suspended servers"],
54         "media_removal": blocks["Filtered media"],
55         "federated_timeline_removal": blocks["Limited servers"]
56         + blocks["Silenced servers"],
57     }
58
59
60 def get_hash(domain: str) -> str:
61     return sha256(domain.encode("utf-8")).hexdigest()
62
63
64 def get_type(domain: str) -> str:
65     try:
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)
71         if res.ok:
72             if res.json()["software"]["name"] == "akkoma":
73                 return "pleroma"
74             elif res.json()["software"]["name"] == "hometown":
75                 return "mastodon"
76             else:
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)
80         if res.ok:
81             return "mastodon"
82     except:
83         return None
84
85
86 conn = sqlite3.connect("blocks.db")
87 c = conn.cursor()
88
89 c.execute(
90     "select domain, software from instances where software in ('pleroma', 'mastodon')"
91 )
92
93 for blocker, software in c.fetchall():
94     if software == "pleroma":
95         print(blocker)
96         try:
97             # Blocks
98             federation = get(
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"]}}
105                 ).items():
106                     for blocked in blocks:
107                         if blocked == "":
108                             continue
109                         blocked == blocked.lower()
110                         blocker == blocker.lower()
111                         c.execute(
112                             "select domain from instances where domain = ?", (blocked,)
113                         )
114                         if c.fetchone() == None:
115                             c.execute(
116                                 "insert into instances select ?, ?, ?",
117                                 (blocked, get_hash(blocked), get_type(blocked)),
118                             )
119                         c.execute(
120                             "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
121                             (blocker, blocked, block_level),
122                         )
123                         if c.fetchone() == None:
124                             c.execute(
125                                 "insert into blocks select ?, ?, '', ?",
126                                 (blocker, blocked, block_level),
127                             )
128             conn.commit()
129             # Reasons
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
135                     else {})}
136                 ).items():
137                     for blocked, reason in info.items():
138                         blocker == blocker.lower()
139                         blocked == blocked.lower()
140                         c.execute(
141                             "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
142                             (reason["reason"], blocker, blocked, block_level),
143                         )
144             conn.commit()
145         except Exception as e:
146             print("error:", e, blocker)
147     elif software == "mastodon":
148         print(blocker)
149         try:
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:
157                         c.execute(
158                             "select hash from instances where hash = ?", (blocked_hash,)
159                         )
160                         if c.fetchone() == None:
161                             c.execute(
162                                 "insert into instances select ?, ?, ?",
163                                 (blocked, get_hash(blocked), get_type(blocked)),
164                             )
165                     c.execute(
166                         "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
167                         (blocker, blocked, block_level),
168                     )
169                     if c.fetchone() == None:
170                         c.execute(
171                             "insert into blocks select ?, ?, ?, ?",
172                             (
173                                 blocker,
174                                 blocked if blocked.count("*") <= 1 else blocked_hash,
175                                 reason,
176                                 block_level,
177                             ),
178                         )
179             conn.commit()
180         except Exception as e:
181             print("error:", e, blocker)
182 conn.close()