2 # -*- coding: utf-8 -*-
4 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
5 # Copyright (C) 2023 Free Software Foundation
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published
9 # by the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
27 def find_domains(tag: bs4.element.Tag) -> list:
28 # DEBUG: print(f"DEBUG: tag[]={type(tag)} - CALLED!")
29 if not isinstance(tag, bs4.element.Tag):
30 raise ValueError(f"Parameter tag[]={type(tag)} is not type of bs4.element.Tag")
31 elif not isinstance(tag, bs4.element.Tag):
32 raise KeyError("Cannot find table with instances!")
33 elif len(tag.select("tr")) == 0:
34 raise KeyError("No table rows found in table!")
37 for element in tag.select("tr"):
38 # DEBUG: print(f"DEBUG: element[]={type(element)}")
39 if not element.find("td"):
40 # DEBUG: print("DEBUG: Skipping element, no <td> found")
43 domain = fba.tidyup_domain(element.find("td").text)
44 reason = fba.tidyup_reason(element.findAll("td")[1].text)
46 # DEBUG: print(f"DEBUG: domain='{domain}',reason='{reason}'")
48 if fba.is_blacklisted(domain):
49 print(f"WARNING: domain='{domain}' is blacklisted - skipped!")
51 elif domain == "gab.com/.ai, develop.gab.com":
52 print(f"DEBUG: Multiple domains detected in one row")
62 "domain": "develop.gab.com",
66 elif not validators.domain(domain):
67 print(f"WARNING: domain='{domain}' is not a valid domain - skipped!")
70 # DEBUG: print(f"DEBUG: Adding domain='{domain}' ...")
76 # DEBUG: print(f"DEBUG: domains()={len(domains)} - EXIT!")
87 doc = bs4.BeautifulSoup(
88 reqto.get("https://meta.chaos.social/federation", headers=fba.headers, timeout=(config.get("connection_timeout"), config.get("read_timeout"))).text,
91 # DEBUG: print(f"DEBUG: doc()={len(doc)}[]={type(doc)}")
92 silenced = doc.find("h2", {"id": "silenced-instances"}).findNext("table")
94 # DEBUG: print(f"DEBUG: silenced[]={type(silenced)}")
95 domains["silenced"] = domains["silenced"] + find_domains(silenced)
96 blocked = doc.find("h2", {"id": "blocked-instances"}).findNext("table")
98 # DEBUG: print(f"DEBUG: blocked[]={type(blocked)}")
99 domains["blocked"] = domains["blocked"] + find_domains(blocked)
101 except BaseException as e:
102 print(f"ERROR: Cannot fetch from meta.chaos.social,exception[{type(e)}]:'{str(e)}'")
106 # DEBUG: print(f"DEBUG: domains()={len(domains)}")
108 print(f"INFO: Adding {len(domains)} new instances ...")
109 for block_level in domains:
110 # DEBUG: print(f"DEBUG: block_level='{block_level}'")
112 for row in domains[block_level]:
113 # DEBUG: print(f"DEBUG: row='{row}'")
114 if not fba.is_instance_registered(row["domain"]):
115 print(f"INFO: Fetching instances from domain='{row['domain']}' ...")
116 fba.fetch_instances(row["domain"], None, None, sys.argv[0])
118 if not fba.is_instance_blocked('chaos.social', row["domain"], block_level):
119 # DEBUG: print(f"DEBUG: domain='{row['domain']}',block_level='{block_level}' blocked by chaos.social, adding ...")
120 fba.block_instance('chaos.social', row["domain"], row["reason"], block_level)
122 # DEBUG: print("DEBUG: Committing changes ...")
123 fba.connection.commit()