e
This commit is contained in:
parent
8a5a52630f
commit
00000d99ac
3 changed files with 39 additions and 36 deletions
|
|
@ -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")
|
||||
|
||||
try:
|
||||
|
|
@ -51,17 +53,6 @@ async def refresh_api_cache(client: httpx.AsyncClient, url: str) -> dict:
|
|||
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 with async_playwright() as p:
|
||||
browser = await p.firefox.launch(headless=True)
|
||||
|
|
@ -126,7 +117,14 @@ async def get_events(
|
|||
|
||||
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_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
|
||||
|
||||
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")
|
||||
|
||||
log.info(f"Collected and cached {new_count} new event(s)")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue