From: Roland Häder Date: Fri, 9 Jun 2023 10:04:53 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=aebc7ea13c75852a00128deda1ae451f8f4af117;p=fba.git Continued: - ops, have forgotten to move this function to own module --- diff --git a/fba/federation/__init__.py b/fba/federation/__init__.py index 31e5992..af23ef3 100644 --- a/fba/federation/__init__.py +++ b/fba/federation/__init__.py @@ -1,4 +1,5 @@ __all__ = [ + 'friendica', 'lemmy', 'mastodon', 'misskey', diff --git a/fba/federation/friendica.py b/fba/federation/friendica.py new file mode 100644 index 0000000..a596a6c --- /dev/null +++ b/fba/federation/friendica.py @@ -0,0 +1,71 @@ +# Fedi API Block - An aggregator for fetching blocking data from fediverse nodes +# Copyright (C) 2023 Free Software Foundation +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import bs4 + +from fba import config +from fba import fba +from fba import instances +from fba import network + +def fetch_blocks(domain: str) -> dict: + # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!") + if not isinstance(domain, str): + raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'") + elif domain == "": + raise ValueError("Parameter 'domain' is empty") + + # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain) + blocked = list() + + try: + doc = bs4.BeautifulSoup( + network.fetch_response(domain, "/friendica", network.web_headers, (config.get("connection_timeout"), config.get("read_timeout"))).text, + "html.parser", + ) + except BaseException as exception: + print("WARNING: Failed to fetch /friendica from domain:", domain, exception) + instances.update_last_error(domain, exception) + return {} + + blocklist = doc.find(id="about_blocklist") + + # Prevents exceptions: + if blocklist is None: + # DEBUG: print("DEBUG: Instance has no block list:", domain) + return {} + + table = blocklist.find("table") + + # DEBUG: print(f"DEBUG: table[]='{type(table)}'") + if table.find("tbody"): + rows = table.find("tbody").find_all("tr") + else: + rows = table.find_all("tr") + + # DEBUG: print(f"DEBUG: Found rows()={len(rows)}") + for line in rows: + # DEBUG: print(f"DEBUG: line='{line}'") + blocked.append({ + "domain": fba.tidyup_domain(line.find_all("td")[0].text), + "reason": fba.tidyup_reason(line.find_all("td")[1].text) + }) + # DEBUG: print("DEBUG: Next!") + + # DEBUG: print("DEBUG: Returning blocklist() for domain:", domain, len(blocklist)) + return { + "reject": blocked + }