1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 from fba.helpers import blacklist
22 from fba.helpers import config
23 from fba.helpers import domain as domain_helper
24 from fba.helpers import tidyup
26 from fba.http import network
28 from fba.models import instances
30 logging.basicConfig(level=logging.INFO)
31 logger = logging.getLogger(__name__)
32 #logger.setLevel(logging.DEBUG)
34 def fetch_blocks(domain: str) -> list:
35 logger.debug("domain='%s' - CALLED!", domain)
36 domain_helper.raise_on(domain)
38 if not instances.is_registered(domain):
39 raise Exception(f"domain='{domain}' is not registered but function is invoked.")
40 elif blacklist.is_blacklisted(domain):
41 raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
47 logger.debug("Fetching friendica blocks from domain='%s'", domain)
48 raw = network.fetch_response(
52 (config.get("connection_timeout"), config.get("read_timeout"))
54 logger.debug("Parsing %d Bytes ...", len(raw))
56 doc = bs4.BeautifulSoup(raw, "html.parser",)
57 logger.debug("doc[]='%s'", type(doc))
59 block_tag = doc.find(id="about_blocklist")
60 logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag)
61 except network.exceptions as exception:
62 logger.warning("Exception '%s' during fetching instances from domain='%s'", type(exception), domain)
63 instances.set_last_error(domain, exception)
65 logger.debug("Returning empty list ... - EXIT!")
68 logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag)
70 logger.debug("Instance has no block list: domain='%s' - EXIT!", domain)
73 table = block_tag.find("table")
75 logger.debug("table[]='%s'", type(table))
77 logger.warning("domain='%s' has no table tag - EXIT !", domain)
79 elif table.find("tbody"):
80 rows = table.find("tbody").find_all("tr")
82 rows = table.find_all("tr")
84 logger.debug("Found rows()=%d", len(rows))
86 logger.debug("line='%s'", line)
87 blocked = line.find_all("td")[0].text
88 logger.debug("blocked='%s'", blocked)
90 blocked = tidyup.domain(blocked) if blocked != "" else None
91 reason = tidyup.reason(line.find_all("td")[1].text)
92 logger.debug("blocked='%s',reason='%s' - AFTER!", blocked, reason)
94 if blocked is None or blocked == "":
95 logger.warning("line[]='%s' returned empty blocked domain - SKIPPED!", type(line))
97 elif not domain_helper.is_wanted(blocked):
98 logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
101 logger.debug("Appending blocked='%s',reason='%s'", blocked, reason)
106 "block_level": "reject",
109 logger.debug("blocklist()=%d - EXIT!", len(blocklist))