This commit is contained in:
doms9 2025-09-09 13:34:16 -04:00
parent 8a5a52630f
commit 00000d99ac
3 changed files with 39 additions and 36 deletions

View file

@ -73,35 +73,40 @@ def near_hr(dt: datetime) -> float:
return dt.replace(minute=0, second=0, microsecond=0).timestamp()
def is_fresh(
entry: dict,
nearest_hr: bool,
exp: int,
) -> bool:
ts = entry.get("timestamp", 31496400)
if nearest_hr:
ts = near_hr(datetime.fromtimestamp(ts))
return now.timestamp() - ts < exp
def load_cache(
file: Path,
exp: int | float = None,
exp: int | float,
nearest_hr: bool = False,
per_entry: bool = True,
) -> dict[str, dict[str, str | float]]:
try:
data: dict[str, dict[str, str | float]] = json.loads(
file.read_text(encoding="utf-8")
)
return (
{
k: v
for k, v in data.items()
if now.timestamp()
- near_hr(datetime.fromtimestamp(v.get("timestamp", 31496400)))
< exp
}
if nearest_hr
else {
k: v
for k, v in data.items()
if now.timestamp() - v.get("timestamp", 31496400) < exp
}
)
data: dict = json.loads(file.read_text(encoding="utf-8"))
except (FileNotFoundError, json.JSONDecodeError):
return {}
if per_entry:
return {k: v for k, v in data.items() if is_fresh(v, nearest_hr, exp)}
ts = data.get("timestamp", 31496400)
if nearest_hr:
ts = near_hr(datetime.fromtimestamp(ts))
return data if now.timestamp() - ts < exp else {}
async def safe_process_event(
fn: Callable,