e
This commit is contained in:
parent
bb04b5b8fc
commit
00000d9243
4 changed files with 119 additions and 103 deletions
|
|
@ -68,20 +68,36 @@ def get_logger(name: str | None = None) -> logging.Logger:
|
|||
return logger
|
||||
|
||||
|
||||
def near_hr(dt: datetime) -> float:
|
||||
return dt.replace(minute=0, second=0, microsecond=0).timestamp()
|
||||
|
||||
|
||||
def load_cache(
|
||||
file: Path,
|
||||
exp: int | float = None,
|
||||
nearest_hr: bool = False,
|
||||
) -> 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() - v.get("timestamp", 0) < exp
|
||||
}
|
||||
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
|
||||
}
|
||||
)
|
||||
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue