X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=fba%2Fnetworks%2Ffriendica.py;h=1434697ae1ac06146eb4b78d79eb275e961c38ef;hb=f9d221393d508c052c56cbc8abf04aa411776454;hp=ceba93da31dafdafcb1132ea8da1fa3921693d0d;hpb=f248d55bfebb9cd7885486637e89ad0b1960899a;p=fba.git diff --git a/fba/networks/friendica.py b/fba/networks/friendica.py index ceba93d..1434697 100644 --- a/fba/networks/friendica.py +++ b/fba/networks/friendica.py @@ -14,59 +14,94 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import bs4 +import logging -from fba import config -from fba import network +import bs4 +from fba.helpers import config +from fba.helpers import domain as domain_helper from fba.helpers import tidyup -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") +from fba.http import network + +from fba.models import instances + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) +#logger.setLevel(logging.DEBUG) + +def fetch_blocks(domain: str) -> list: + logger.debug("domain='%s' - CALLED!", domain) + domain_helper.raise_on(domain) + + if not instances.is_registered(domain): + raise Exception(f"domain='{domain}' is not registered but function is invoked.") - # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain) - blocked = list() + blocklist = list() + block_tag = None - doc = bs4.BeautifulSoup( - network.fetch_response( + try: + logger.debug("Fetching friendica blocks from domain='%s'", domain) + raw = network.fetch_response( domain, "/friendica", network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")) - ).text, - "html.parser", - ) - print(f"DEBUG: doc[]='{type(doc)}'") + ).text + logger.debug("Parsing %d Bytes ...", len(raw)) - blocklist = doc.find(id="about_blocklist") + doc = bs4.BeautifulSoup(raw, "html.parser",) + logger.debug("doc[]='%s'", type(doc)) - # Prevents exceptions: - if blocklist is None: - # DEBUG: print("DEBUG: Instance has no block list:", domain) - return {} + block_tag = doc.find(id="about_blocklist") + logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag) + except network.exceptions as exception: + logger.warning("Exception '%s' during fetching instances from domain='%s'", type(exception), domain) + instances.set_last_error(domain, exception) - table = blocklist.find("table") + logger.debug("Returning empty list ... - EXIT!") + return list() - # DEBUG: print(f"DEBUG: table[]='{type(table)}'") - if table.find("tbody"): + logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag) + if block_tag is None: + logger.debug("Instance has no block list: domain='%s' - EXIT!", domain) + return list() + + table = block_tag.find("table") + + logger.debug("table[]='%s'", type(table)) + if table is None: + logger.warning("domain='%s' has no table tag - EXIT !", domain) + return list() + elif table.find("tbody"): rows = table.find("tbody").find_all("tr") else: rows = table.find_all("tr") - # DEBUG: print(f"DEBUG: Found rows()={len(rows)}") + logger.debug("Found rows()=%d", len(rows)) for line in rows: - # DEBUG: print(f"DEBUG: line='{line}'") - blocked.append({ - "domain": tidyup.domain(line.find_all("td")[0].text), - "reason": tidyup.reason(line.find_all("td")[1].text) + logger.debug("line='%s'", line) + blocked = line.find_all("td")[0].text + logger.debug("blocked='%s'", blocked) + + blocked = tidyup.domain(blocked) if blocked != "" else None + reason = tidyup.reason(line.find_all("td")[1].text) + logger.debug("blocked='%s',reason='%s' - AFTER!", blocked, reason) + + if blocked is None or blocked == "": + logger.warning("line[]='%s' returned empty blocked domain - SKIPPED!", type(line)) + continue + elif not domain_helper.is_wanted(blocked): + logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked) + continue + + logger.debug("Appending blocked='%s',reason='%s'", blocked, reason) + blocklist.append({ + "blocker" : domain, + "blocked" : blocked, + "reason" : reason, + "block_level": "reject", }) - # DEBUG: print("DEBUG: Next!") - # DEBUG: print("DEBUG: Returning blocklist() for domain:", domain, len(blocklist)) - return { - "reject": blocked - } + logger.debug("blocklist()=%d - EXIT!", len(blocklist)) + return blocklist