Improve the logic to not be floats
This commit is contained in:
parent
8e8b460975
commit
490b5e0bd4
1 changed files with 6 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import datetime
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
@ -221,19 +222,21 @@ def preview_url(
|
||||||
for result in results:
|
for result in results:
|
||||||
# find the one with the closest timestamp
|
# find the one with the closest timestamp
|
||||||
metadata, _ts = result
|
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)
|
logging.debug("Optimal cache hit for %r", url)
|
||||||
return json.loads(metadata)
|
return json.loads(metadata)
|
||||||
else:
|
else:
|
||||||
logging.debug("No optimal cache matches for url %r.", url)
|
logging.debug("No optimal cache matches for url %r.", url)
|
||||||
# No close matches, get the latest one
|
# No close matches, get the latest one
|
||||||
metadata, _ts = results[-1]
|
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 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)
|
logging.debug("Stale cache hit for %r", url)
|
||||||
return json.loads(metadata)
|
return json.loads(metadata)
|
||||||
else:
|
else:
|
||||||
logging.debug("Cache miss for %r")
|
logging.debug("Cache miss for %r", url)
|
||||||
else:
|
else:
|
||||||
logging.debug("Full cache miss for %r", url)
|
logging.debug("Full cache miss for %r", url)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue