This commit is contained in:
doms9 2025-10-02 12:57:25 -04:00
parent 9caa2f50d1
commit 00000d9342
6 changed files with 10 additions and 12 deletions

View file

@ -209,7 +209,7 @@ async def get_events(
events: list[dict[str, str]] = [] events: list[dict[str, str]] = []
now = Time.now() now = Time.clean(Time.now())
start_dt = now.delta(minutes=-30) start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30) end_dt = now.delta(minutes=30)

View file

@ -133,7 +133,7 @@ async def get_events(
events: list[dict[str, str]] = [] events: list[dict[str, str]] = []
now = Time.now() now = Time.clean(Time.now())
start_dt = now.delta(minutes=-30) start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30) end_dt = now.delta(minutes=30)

View file

@ -102,8 +102,7 @@ async def get_events(
soup = HTMLParser(r.text) soup = HTMLParser(r.text)
events = [] events = []
now = Time.now() now = Time.clean(Time.now())
start_dt = now.delta(minutes=-30) start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30) end_dt = now.delta(minutes=30)

View file

@ -134,8 +134,8 @@ async def get_events(
HTML_CACHE.write(events) HTML_CACHE.write(events)
live = [] live = []
now = Time.now()
now = Time.clean(Time.now())
start_ts = now.delta(minutes=-30).timestamp() start_ts = now.delta(minutes=-30).timestamp()
end_ts = now.delta(minutes=30).timestamp() end_ts = now.delta(minutes=30).timestamp()

View file

@ -1,5 +1,4 @@
import json import json
from datetime import datetime
from pathlib import Path from pathlib import Path
from .config import Time from .config import Time
@ -12,14 +11,10 @@ class Cache:
self.exp = exp self.exp = exp
self.now_ts = Time.now().timestamp() self.now_ts = Time.now().timestamp()
@staticmethod
def clean(dt: datetime) -> float:
return dt.replace(second=0, microsecond=0).timestamp()
def is_fresh(self, entry: dict) -> bool: def is_fresh(self, entry: dict) -> bool:
ts: float | int = entry.get("timestamp", 31496400) ts: float | int = entry.get("timestamp", 31496400)
dt_ts = self.clean(Time.from_ts(ts)) dt_ts = Time.clean(Time.from_ts(ts)).timestamp()
return self.now_ts - dt_ts < self.exp return self.now_ts - dt_ts < self.exp
@ -34,7 +29,7 @@ class Cache:
ts: float | int = data.get("timestamp", 31496400) ts: float | int = data.get("timestamp", 31496400)
dt_ts = self.clean(Time.from_ts(ts)) dt_ts = Time.clean(Time.from_ts(ts)).timestamp()
return data if self.is_fresh({"timestamp": dt_ts}) else {} return data if self.is_fresh({"timestamp": dt_ts}) else {}

View file

@ -26,6 +26,10 @@ class Time(datetime):
new_dt = super().__add__(timedelta(**kwargs)) new_dt = super().__add__(timedelta(**kwargs))
return self.__class__.fromtimestamp(new_dt.timestamp(), tz=new_dt.tzinfo) return self.__class__.fromtimestamp(new_dt.timestamp(), tz=new_dt.tzinfo)
def clean(self) -> "Time":
new_dt = super().replace(second=0, microsecond=0)
return self.__class__.fromtimestamp(new_dt.timestamp(), tz=new_dt.tzinfo)
@classmethod @classmethod
def from_str(cls, s: str, fmt: str | None = None) -> "Time": def from_str(cls, s: str, fmt: str | None = None) -> "Time":
pattern = r"\b(ET|UTC|EST|EDT)\b" pattern = r"\b(ET|UTC|EST|EDT)\b"