From 8ed5c8e49871db3b4650a211a04d5d8901b7053e Mon Sep 17 00:00:00 2001 From: nex Date: Sat, 10 Feb 2024 01:53:09 +0000 Subject: [PATCH] Properly re-load the fetched cache data --- server.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index 017127a..5ef11de 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,7 @@ import io +import json import os +import time import uuid import fastapi @@ -203,13 +205,13 @@ def preview_url( metadata, _ts = result if ts is None or abs(ts - _ts) < 3600: logging.debug("Optimal cache hit for %r", url) - return metadata + return json.loads(metadata) # No close matches, get the latest one metadata, _ts = results[-1] # If the latest one is more than 3 hours old, re-fetch. Otherwise, return. if ts is None or abs(ts - _ts) < 10800: logging.debug("Cache hit for %r", url) - return metadata + return json.loads(metadata) domain = os.environ.get("PREVIEW_HOMESERVER", "https://" + req.url.hostname) @@ -308,7 +310,7 @@ def preview_url( with sqlite3.connect(CACHE_FILE) as conn: conn.execute( "INSERT INTO cache (uuid, url, ts, metadata) VALUES (?, ?, ?, ?)", - (str(uuid.uuid4()), url, int(response.headers["date"]), str(og_tags)) + (str(uuid.uuid4()), url, round(time.time()), json.dumps(og_tags)) ) return og_tags