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/>.
23 from fba.helpers import config
24 from fba.helpers import domain as domain_helper
25 from fba.helpers import tidyup
27 from fba.http import network
29 from fba.models import instances
31 logging.basicConfig(level=logging.INFO)
32 logger = logging.getLogger(__name__)
33 #logger.setLevel(logging.DEBUG)
35 def fetch_blocks(domain: str) -> list:
36 logger.debug("domain='%s' - CALLED!", domain)
37 domain_helper.raise_on(domain)
43 logger.debug("Fetching friendica blocks from domain='%s'", domain)
44 raw = network.fetch_response(
48 (config.get("connection_timeout"), config.get("read_timeout"))
50 logger.debug("Parsing %d Bytes ...", len(raw))
52 doc = bs4.BeautifulSoup(raw, "html.parser",)
53 logger.debug("doc[]='%s'", type(doc))
55 block_tag = doc.find(id="about_blocklist")
56 logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag)
57 except network.exceptions as exception:
58 logger.warning("Exception '%s' during fetching instances from domain='%s'", type(exception), domain)
59 instances.set_last_error(domain, exception)
61 logger.debug("Returning empty list ... - EXIT!")
64 logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag)
66 logger.debug("Instance has no block list: domain='%s' - EXIT!", domain)
69 table = block_tag.find("table")
71 logger.debug("table[]='%s'", type(table))
72 if table.find("tbody"):
73 rows = table.find("tbody").find_all("tr")
75 rows = table.find_all("tr")
77 logger.debug("Found rows()=%d", len(rows))
79 logger.debug("line='%s'", line)
80 blocked = tidyup.domain(line.find_all("td")[0].text)
81 reason = tidyup.reason(line.find_all("td")[1].text)
82 logger.debug("blocked='%s',reason='%s'", blocked, reason)
85 logger.debug("line[]='%s' returned empty blocked domain - SKIPPED!", type(line))
87 elif not domain_helper.is_wanted(blocked):
88 logger.debug("blocked='%s' is not wanted - SKIPPED!", domain)
91 logger.debug("blocked='%s',domain='%s' - BEFORE!", blocked, domain)
92 blocked = utils.deobfuscate(blocked, domain)
94 logger.debug("blocked[%s]='%s' - DEOBFUSCATED!", type(blocked), blocked)
95 if not domain_helper.is_wanted(blocked):
96 logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
99 logger.debug("Appending blocked='%s',reason='%s'", blocked, reason)
102 "blocked" : tidyup.domain(blocked),
103 "reason" : tidyup.reason(reason),
104 "block_level": "reject",
107 logger.debug("blocklist()=%d - EXIT!", len(blocklist))