e
This commit is contained in:
parent
8796e2dfc6
commit
00000d9ef1
7 changed files with 318 additions and 44 deletions
|
|
@ -14,6 +14,7 @@ from .utils import (
|
|||
LOGOS,
|
||||
TZ,
|
||||
capture_req,
|
||||
firefox,
|
||||
get_logger,
|
||||
load_cache,
|
||||
now,
|
||||
|
|
@ -96,11 +97,7 @@ async def fetch_xml_stream(url: str, ssl_ctx: ssl.SSLContext) -> io.BytesIO | No
|
|||
|
||||
async def process_event(url: str, url_num: int) -> str | None:
|
||||
async with async_playwright() as p:
|
||||
browser = await p.firefox.launch(headless=True)
|
||||
|
||||
context = await browser.new_context(
|
||||
ignore_https_errors=True # website doesn't send valid certs
|
||||
)
|
||||
browser, context = await firefox(p, ignore_https_errors=True)
|
||||
|
||||
page = await context.new_page()
|
||||
|
||||
|
|
@ -217,7 +214,9 @@ async def get_events(
|
|||
) -> list[dict[str, str]]:
|
||||
|
||||
events: list[dict[str, str]] = []
|
||||
window_start, window_end = now - timedelta(hours=1), now + timedelta(minutes=30)
|
||||
|
||||
start_dt = now - timedelta(minutes=30)
|
||||
end_dt = now + timedelta(minutes=30)
|
||||
|
||||
if buffer := await fetch_xml_stream(url, ssl_ctx):
|
||||
pub_date_format = "%a, %d %b %Y %H:%M:%S %z"
|
||||
|
|
@ -236,30 +235,33 @@ async def get_events(
|
|||
elem.clear()
|
||||
continue
|
||||
|
||||
if window_start <= dt <= window_end:
|
||||
sport, event = (
|
||||
(
|
||||
desc.split(".")[0].strip(),
|
||||
" ".join(p.strip() for p in desc.split(".")[1:]),
|
||||
)
|
||||
if desc
|
||||
else ("", "")
|
||||
if not start_dt <= dt <= end_dt:
|
||||
elem.clear()
|
||||
continue
|
||||
|
||||
sport, event = (
|
||||
(
|
||||
desc.split(".")[0].strip(),
|
||||
" ".join(p.strip() for p in desc.split(".")[1:]),
|
||||
)
|
||||
if desc
|
||||
else ("", "")
|
||||
)
|
||||
|
||||
key = f"[{sport}: {event}] {title}"
|
||||
key = f"[{sport}: {event}] {title}"
|
||||
|
||||
if key in cached_keys:
|
||||
elem.clear()
|
||||
continue
|
||||
if key in cached_keys:
|
||||
elem.clear()
|
||||
continue
|
||||
|
||||
events.append(
|
||||
{
|
||||
"sport": sport,
|
||||
"event": event,
|
||||
"title": title,
|
||||
"link": link,
|
||||
}
|
||||
)
|
||||
events.append(
|
||||
{
|
||||
"sport": sport,
|
||||
"event": event,
|
||||
"title": title,
|
||||
"link": link,
|
||||
}
|
||||
)
|
||||
|
||||
elem.clear()
|
||||
|
||||
|
|
@ -312,8 +314,8 @@ async def main(client: httpx.AsyncClient) -> None:
|
|||
urls[key] = cached_urls[key] = entry
|
||||
|
||||
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)")
|
||||
else:
|
||||
log.info("No new events found")
|
||||
|
||||
CACHE_FILE.write_text(json.dumps(cached_urls, indent=2), encoding="utf-8")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue