From c15b58ee9b0fc16de14f12c751ec3bd4f2284fc5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 7 Jul 2023 22:42:51 +0200 Subject: [PATCH] Continued: - check response.status_code and length of response.text - handle None (NULL) values for last_updated/last_seen --- daemon.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/daemon.py b/daemon.py index 459f0fc..71e76ce 100755 --- a/daemon.py +++ b/daemon.py @@ -326,7 +326,7 @@ def list_domains(request: Request, mode: str, value: str, amount: int = config.g format = config.get("timestamp_format") for row in domainlist: row["first_seen"] = datetime.utcfromtimestamp(row["first_seen"]).strftime(format) - row["last_updated"] = datetime.utcfromtimestamp(row["last_updated"]).strftime(format) + row["last_updated"] = datetime.utcfromtimestamp(row["last_updated"]).strftime(format) if isinstance(row["last_updated"], float) else None return templates.TemplateResponse("views/list.html", { "request" : request, @@ -350,14 +350,14 @@ def top(request: Request, mode: str, value: str, amount: int = config.get("api_l found = 0 blocklist = list() - if response is not None and response.ok: + if response is not None and response.ok and response.status_code >= 300 and len(response.text) > 0: blocklist = response.json() format = config.get("timestamp_format") for block_level in blocklist: - for block in blocklist[block_level]: - block["first_seen"] = datetime.utcfromtimestamp(block["first_seen"]).strftime(format) - block["last_seen"] = datetime.utcfromtimestamp(block["last_seen"]).strftime(format) + for row in blocklist[block_level]: + row["first_seen"] = datetime.utcfromtimestamp(row["first_seen"]).strftime(format) + row["last_seen"] = datetime.utcfromtimestamp(row["last_seen"]).strftime(format) if isinstance(row["last_updated"], float) else None found = found + 1 return templates.TemplateResponse("views/top.html", { @@ -381,7 +381,7 @@ def rss(request: Request, domain: str): response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/domain.json?domain={domain}") - if not response.ok or response.status_code > 200 or response.text.strip() == "": + if not response.ok or response.status_code >= 300 or response.text.strip() == "": raise HTTPException(status_code=response.status_code, detail=response.reason) domain_data = response.json() @@ -390,9 +390,9 @@ def rss(request: Request, domain: str): format = config.get("timestamp_format") instance = dict() for key in domain_data.keys(): - if key in ["last_nodeinfo", "last_blocked", "first_seen", "last_updated", "last_instance_fetch"]: + if key in ["last_nodeinfo", "last_blocked", "first_seen", "last_updated", "last_instance_fetch"] and isinstance(domain_data, float): # Timestamps - instance[key] = datetime.utcfromtimestamp(domain_data[key]).strftime(format) if isinstance(domain_data[key], float) else "-" + instance[key] = datetime.utcfromtimestamp(domain_data[key]).strftime(format) else: # Generic instance[key] = domain_data[key] -- 2.39.5