- change caching process for select sites
This commit is contained in:
doms9 2026-04-16 17:42:28 -04:00
parent 6b209de1d8
commit 00000d9ef7
9 changed files with 138 additions and 212 deletions

View file

@ -53,7 +53,7 @@ async def process_event(url: str, url_num: int) -> tuple[str | None, str | None]
return match[3], iframe_src
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
async def get_events() -> list[dict[str, str]]:
events = []
if not (html_data := await network.request(BASE_URL, log=log)):
@ -76,9 +76,6 @@ async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
event_name = event_name_elem.text(strip=True)
if f"[{sport}] {event_name} ({TAG})" in cached_keys:
continue
events.append(
{
"sport": sport,
@ -91,20 +88,17 @@ async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
async def scrape() -> None:
cached_urls = CACHE_FILE.load()
if cached_urls := CACHE_FILE.load():
urls.update(cached_urls)
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
log.info(f"Loaded {len(urls)} event(s) from cache")
valid_count = cached_count = len(valid_urls)
urls.update(valid_urls)
log.info(f"Loaded {cached_count} event(s) from cache")
return
log.info(f'Scraping from "{BASE_URL}"')
if events := await get_events(cached_urls.keys()):
log.info(f"Processing {len(events)} new URL(s)")
if events := await get_events():
log.info(f"Processing {len(events)} URL(s)")
now = Time.clean(Time.now())
@ -140,13 +134,11 @@ async def scrape() -> None:
cached_urls[key] = entry
if url:
valid_count += 1
urls[key] = entry
log.info(f"Collected and cached {valid_count - cached_count} new event(s)")
log.info(f"Collected and cached {len(urls)} event(s)")
else:
log.info("No new events found")
log.info("No events found")
CACHE_FILE.write(cached_urls)