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__)
34 def fetch_blocks(domain: str) -> dict:
35 logger.debug("domain(%d)='%s' - CALLED!", len(domain), domain)
36 domain_helper.raise_on(domain)
42 logger.debug("Fetching friendica blocks from domain:", domain)
43 doc = bs4.BeautifulSoup(
44 network.fetch_response(
48 (config.get("connection_timeout"), config.get("read_timeout"))
52 logger.debug("doc[]='%s'", type(doc))
54 block_tag = doc.find(id="about_blocklist")
55 except network.exceptions as exception:
56 logger.warning(f"Exception '{type(exception)}' during fetching instances (friendica) from domain='{domain}'")
57 instances.set_last_error(domain, exception)
60 # Prevents exceptions:
62 logger.debug("Instance has no block list:", domain)
65 table = block_tag.find("table")
67 logger.debug(f"table[]='{type(table)}'")
68 if table.find("tbody"):
69 rows = table.find("tbody").find_all("tr")
71 rows = table.find_all("tr")
73 logger.debug(f"Found rows()={len(rows)}")
75 logger.debug(f"line='{line}'")
76 blocked = tidyup.domain(line.find_all("td")[0].text)
77 reason = tidyup.reason(line.find_all("td")[1].text)
78 logger.debug(f"blocked='{blocked}',reason='{reason}'")
80 if not utils.is_domain_wanted(blocked):
81 logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
84 logger.debug(f"Appending blocked='{blocked}',reason='{reason}'")
86 "domain": tidyup.domain(blocked),
87 "reason": tidyup.reason(reason)
91 logger.debug("Returning blocklist() for domain:", domain, len(blocklist))