- edit scraping for streambiz.py
This commit is contained in:
doms9 2026-06-09 22:27:25 -04:00
parent feac0cb9e1
commit 00000d9c3d

View file

@ -1,4 +1,5 @@
import asyncio import asyncio
import re
from functools import partial from functools import partial
from urllib.parse import urljoin from urllib.parse import urljoin
@ -117,6 +118,8 @@ async def process_event(
async def get_events(cached_links: set[str]) -> list[dict[str, str]]: async def get_events(cached_links: set[str]) -> list[dict[str, str]]:
now = Time.clean(Time.now())
tasks = [network.request(url, log=log) for url in SPORT_URLS.values()] tasks = [network.request(url, log=log) for url in SPORT_URLS.values()]
results = await asyncio.gather(*tasks) results = await asyncio.gather(*tasks)
@ -128,6 +131,11 @@ async def get_events(cached_links: set[str]) -> list[dict[str, str]]:
): ):
return events return events
start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30)
date_ptrn = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}Z", re.I)
for soup, url in soups: for soup, url in soups:
sport = next((k for k, v in SPORT_URLS.items() if v == url), "Live Event") sport = next((k for k, v in SPORT_URLS.items() if v == url), "Live Event")
@ -138,13 +146,25 @@ async def get_events(cached_links: set[str]) -> list[dict[str, str]]:
elif cached_links & {link := urljoin(BASE_URL, href)}: elif cached_links & {link := urljoin(BASE_URL, href)}:
continue continue
if not event.css_first("span.status-badge.badge.bg-success"): if (scr_elem := event.css_first("script")) and (
match := date_ptrn.search(scr_elem.text(strip=True))
):
event_dt = Time.fromisoformat(match[0]).to_tz("EST")
elif event.css_first("span.status-badge.badge.bg-success"):
event_dt = now
else:
continue
if not start_dt <= event_dt <= end_dt:
continue continue
events.append( events.append(
{ {
"sport": sport, "sport": sport,
"link": link, "link": link,
"timestamp": event_dt.timestamp(),
} }
) )
@ -169,8 +189,6 @@ async def scrape(browser: Browser) -> None:
if events := await get_events(cached_links): if events := await get_events(cached_links):
log.info(f"Processing {len(events)} URL(s)") log.info(f"Processing {len(events)} URL(s)")
now = Time.clean(Time.now())
async with network.event_context(browser, stealth=False) as context: async with network.event_context(browser, stealth=False) as context:
for i, ev in enumerate(events, start=1): for i, ev in enumerate(events, start=1):
async with network.event_page(context) as page: async with network.event_page(context) as page:
@ -196,7 +214,7 @@ async def scrape(browser: Browser) -> None:
"url": url, "url": url,
"logo": logo, "logo": logo,
"base": ifr_src, "base": ifr_src,
"timestamp": now.timestamp(), "timestamp": ev["timestamp"],
"id": tvg_id or "Live.Event.us", "id": tvg_id or "Live.Event.us",
"link": link, "link": link,
} }