From 490b5e0bd4ffd13233c2052403fdf26846c3c587 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Thu, 22 Feb 2024 18:32:49 +0000 Subject: [PATCH] Improve the logic to not be floats --- server.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index f51142e..9a8d57d 100644 --- a/server.py +++ b/server.py @@ -1,4 +1,5 @@ import contextlib +import datetime import io import json import os @@ -221,19 +222,21 @@ def preview_url( for result in results: # find the one with the closest timestamp metadata, _ts = result - if ts is None or abs(ts - _ts) < 3600: + created_at = datetime.datetime.fromtimestamp(_ts) + if ts is None or created_at <= datetime.datetime.fromtimestamp(ts): logging.debug("Optimal cache hit for %r", url) return json.loads(metadata) else: logging.debug("No optimal cache matches for url %r.", url) # No close matches, get the latest one metadata, _ts = results[-1] + created_at = datetime.datetime.fromtimestamp(_ts) # If the latest one is more than a week old, re-fetch. Otherwise, return. - if ts is None or abs(ts - _ts) < 604800: + if ts is None or created_at < (datetime.datetime.now() - datetime.timedelta(days=7)): logging.debug("Stale cache hit for %r", url) return json.loads(metadata) else: - logging.debug("Cache miss for %r") + logging.debug("Cache miss for %r", url) else: logging.debug("Full cache miss for %r", url)