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

@ -311,7 +311,7 @@ async def main(client: httpx.AsyncClient) -> None:
urls[key] = cached_urls[key] = entry urls[key] = cached_urls[key] = entry
if (new_count := len(cached_urls) - cached_count) > 0: if new_count := len(cached_urls) - cached_count:
CACHE_FILE.write_text(json.dumps(cached_urls, indent=2), encoding="utf-8") CACHE_FILE.write_text(json.dumps(cached_urls, indent=2), encoding="utf-8")
log.info(f"Collected and cached {new_count} new event(s)") log.info(f"Collected and cached {new_count} new event(s)")

View file

@ -38,7 +38,9 @@ MIRRORS = [
] ]
async def refresh_api_cache(client: httpx.AsyncClient, url: str) -> dict: async def refresh_api_cache(
client: httpx.AsyncClient, url: str
) -> dict[str, dict[str, str]]:
log.info("Refreshing API cache") log.info("Refreshing API cache")
try: try:
@ -51,17 +53,6 @@ async def refresh_api_cache(client: httpx.AsyncClient, url: str) -> dict:
return r.json() return r.json()
def load_api_cache() -> dict[str, dict[str, str | str]]:
try:
data: dict = json.loads(API_FILE.read_text(encoding="utf-8"))
age: float = now.timestamp() - data.get("timestamp", 0)
return data if age < 86400 else {} # 24 hours
except (FileNotFoundError, json.JSONDecodeError):
return {}
async def process_event(url: str, url_num: int) -> str | None: async def process_event(url: str, url_num: int) -> str | None:
async with async_playwright() as p: async with async_playwright() as p:
browser = await p.firefox.launch(headless=True) browser = await p.firefox.launch(headless=True)
@ -126,7 +117,14 @@ async def get_events(
base_url = re.match(r"(https?://.+?)/", api_url)[1] base_url = re.match(r"(https?://.+?)/", api_url)[1]
if not (api_data := load_api_cache()): if not (
api_data := load_cache(
API_FILE,
exp=86400,
nearest_hr=True,
per_entry=False,
)
):
api_data = await refresh_api_cache(client, api_url) api_data = await refresh_api_cache(client, api_url)
API_FILE.write_text(json.dumps(api_data, indent=2), encoding="utf-8") API_FILE.write_text(json.dumps(api_data, indent=2), encoding="utf-8")
@ -211,7 +209,7 @@ async def main(client: httpx.AsyncClient) -> None:
urls[key] = cached_urls[key] = entry urls[key] = cached_urls[key] = entry
if (new_count := len(cached_urls) - cached_count) > 0: if new_count := len(cached_urls) - cached_count:
CACHE_FILE.write_text(json.dumps(cached_urls, indent=2), encoding="utf-8") CACHE_FILE.write_text(json.dumps(cached_urls, indent=2), encoding="utf-8")
log.info(f"Collected and cached {new_count} new event(s)") log.info(f"Collected and cached {new_count} new event(s)")

View file

@ -73,35 +73,40 @@ def near_hr(dt: datetime) -> float:
return dt.replace(minute=0, second=0, microsecond=0).timestamp() 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( def load_cache(
file: Path, file: Path,
exp: int | float = None, exp: int | float,
nearest_hr: bool = False, nearest_hr: bool = False,
per_entry: bool = True,
) -> dict[str, dict[str, str | float]]: ) -> dict[str, dict[str, str | float]]:
try: try:
data: dict[str, dict[str, str | float]] = json.loads( data: dict = json.loads(file.read_text(encoding="utf-8"))
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
}
)
except (FileNotFoundError, json.JSONDecodeError): except (FileNotFoundError, json.JSONDecodeError):
return {} 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( async def safe_process_event(
fn: Callable, fn: Callable,