From a534df845331afa189c3b7c7aaea84a38567f715 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 2 Jul 2023 19:26:16 +0200 Subject: [PATCH] Continued: - added command update_nodeinfo(), please only execute this when you have trouble in federation --- fba/boot.py | 7 +++++++ fba/commands.py | 16 ++++++++++++++++ fba/helpers/cookies.py | 4 ++-- fba/http/network.py | 26 +++++++++++++------------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/fba/boot.py b/fba/boot.py index 157d7a1..d04fd95 100644 --- a/fba/boot.py +++ b/fba/boot.py @@ -182,6 +182,13 @@ def init_parser(): parser.add_argument("--software", help="Name of software, e.g. 'lemmy'") parser.add_argument("--all", action="store_true", help="Include also already existing instances, otherwise only new are checked") + ### Update nodeinfo ### + parser = subparser_command.add_parser( + "update_nodeinfo", + help="Updates nodeinfo for all instances", + ) + parser.set_defaults(command=commands.update_nodeinfo) + logger.debug("EXIT!") def run_command(): diff --git a/fba/commands.py b/fba/commands.py index 938f8c8..cf88126 100644 --- a/fba/commands.py +++ b/fba/commands.py @@ -1370,3 +1370,19 @@ def fetch_fedilist(args: argparse.Namespace) -> int: logger.debug("Success! - EXIT!") return 0 + +def update_nodeinfo(args: argparse.Namespace) -> int: + logger.debug("args[]='%s' - CALLED!", type(args)) + + database.cursor.execute("SELECT domain FROM instances WHERE last_nodeinfo < ?", [time.time() - config.get("recheck_block")]) + + domains = database.cursor.fetchall() + + logger.info("Checking %d domain(s) ...", len(domains)) + for row in domains: + logger.info("Updating nodeinfo for row[domain]='%s' ...", row["domain"]) + federation.fetch_nodeinfo(row["domain"]) + instances.set_last_nodeinfo(row["domain"]) + + logger.debug("Success! - EXIT!") + return 0 diff --git a/fba/helpers/cookies.py b/fba/helpers/cookies.py index 3c4fcfd..b963a13 100644 --- a/fba/helpers/cookies.py +++ b/fba/helpers/cookies.py @@ -38,8 +38,8 @@ def get_all(domain: str) -> dict: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) - if not has(domain): - raise Exception(f"domain='{domain}' has no cookies stored, maybe invoke store() first?") + if domain not in _cookies: + return dict() logger.debug("_cookies[%s]()=%d - EXIT!", domain, len(_cookies[domain])) return _cookies[domain] diff --git a/fba/http/network.py b/fba/http/network.py index 229c474..c7816c6 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -80,16 +80,16 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() data=data, headers={**api_headers, **headers}, timeout=(config.get("connection_timeout"), config.get("read_timeout")), - cookies=cookies.get_all(domain) if cookies.has(domain) else dict(), + cookies=cookies.get_all(domain), allow_redirects=False ) logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) json_reply["json"] = json_helper.from_response(response) - logger.debug("response.ok='%s',response.status_code=%d,json_reply[]='%s'", response.ok, response.status_code, type(json_reply)) - if not response.ok or response.status_code >= 400 or len(json_reply["json"]) == 0: - logger.warning("Cannot query JSON API: domain='%s',path='%s',data()=%d,response.status_code=%d,json_reply[]='%s'", domain, path, len(data), response.status_code, type(json_reply)) + logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text)) + if not response.ok or response.status_code >= 300 or len(response.text.strip()) == 0: + logger.warning("Cannot query JSON API: domain='%s',path='%s',data()=%d,response.status_code=%d,response.text()=%d", domain, path, len(data), response.status_code, len(response.text)) json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason instances.set_last_error(domain, response) @@ -126,9 +126,9 @@ def fetch_api_url(url: str, timeout: tuple) -> dict: logger.debug("Parsing JSON response from url='%s' ...", url) json_reply["json"] = json_helper.from_response(response) - logger.debug("response.ok='%s',response.status_code='%s',json_reply[]='%s'", response.ok, response.status_code, type(json_reply)) - if not response.ok or response.status_code >= 400 or len(json_reply["json"]) == 0: - logger.warning("Cannot query JSON API: url='%s',response.status_code=%d,json_reply[]='%s'", url, response.status_code, type(json_reply)) + logger.debug("response.ok='%s',response.status_code='%s',response.text()=%d", response.ok, response.status_code, len(response.text), response.text) + if not response.ok or response.status_code >= 300 or len(response.text) == 0: + logger.warning("Cannot query JSON API: url='%s',response.status_code=%d,response.text()=%d", url, response.status_code, len(response.text)) json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason del json_reply["json"] @@ -166,7 +166,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: f"https://{domain}{path}", headers={**api_headers, **headers}, timeout=timeout, - cookies=cookies.get_all(domain) if cookies.has(domain) else {}, + cookies=cookies.get_all(domain), allow_redirects=False ) @@ -181,13 +181,13 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) json_reply["json"] = json_helper.from_response(response) - logger.debug("response.ok='%s',response.status_code=%d,json_reply[]='%s'", response.ok, response.status_code, type(json_reply)) - if not response.ok or response.status_code >= 400 or len(json_reply["json"]) == 0: - logger.warning("Cannot query JSON API: domain='%s',path='%s',response.status_code=%d,json_reply[]='%s'", domain, path, response.status_code, type(json_reply)) + logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text)) + if not response.ok or response.status_code >= 300 or len(response.text) == 0: + logger.warning("Cannot query JSON API: domain='%s',path='%s',response.status_code=%d,response.text()=%d", domain, path, response.status_code, len(response.text)) json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason - del json_reply["json"] instances.set_last_error(domain, response) + del json_reply["json"] logger.debug("Returning json_reply(%d)[]='%s' - EXIT!", len(json_reply), type(json_reply)) return json_reply @@ -257,7 +257,7 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: tuple) -> req f"https://{domain}{path}", headers=headers, timeout=timeout, - cookies=cookies.get_all(domain) if cookies.has(domain) else {}, + cookies=cookies.get_all(domain), allow_redirects=False ) -- 2.39.5