edit scraping method for roxie.py
This commit is contained in:
doms9 2026-02-11 20:29:22 -05:00
parent 2e528dd8b2
commit 00000d9a6c
2 changed files with 54 additions and 48 deletions

View file

@ -65,7 +65,6 @@ async def main() -> None:
asyncio.create_task(embedhd.scrape(hdl_brwsr)), asyncio.create_task(embedhd.scrape(hdl_brwsr)),
asyncio.create_task(pixel.scrape(hdl_brwsr)), asyncio.create_task(pixel.scrape(hdl_brwsr)),
asyncio.create_task(ppv.scrape(xtrnl_brwsr)), asyncio.create_task(ppv.scrape(xtrnl_brwsr)),
asyncio.create_task(roxie.scrape(hdl_brwsr)),
asyncio.create_task(sport9.scrape(xtrnl_brwsr)), asyncio.create_task(sport9.scrape(xtrnl_brwsr)),
asyncio.create_task(streamcenter.scrape(xtrnl_brwsr)), asyncio.create_task(streamcenter.scrape(xtrnl_brwsr)),
# asyncio.create_task(streamhub.scrape(xtrnl_brwsr)), # asyncio.create_task(streamhub.scrape(xtrnl_brwsr)),
@ -79,6 +78,7 @@ async def main() -> None:
asyncio.create_task(fawa.scrape()), asyncio.create_task(fawa.scrape()),
asyncio.create_task(istreameast.scrape()), asyncio.create_task(istreameast.scrape()),
asyncio.create_task(pawa.scrape()), asyncio.create_task(pawa.scrape()),
asyncio.create_task(roxie.scrape()),
asyncio.create_task(shark.scrape()), asyncio.create_task(shark.scrape()),
asyncio.create_task(streambtw.scrape()), asyncio.create_task(streambtw.scrape()),
asyncio.create_task(xstreameast.scrape()), asyncio.create_task(xstreameast.scrape()),

View file

@ -1,8 +1,8 @@
import asyncio import asyncio
import re
from functools import partial from functools import partial
from urllib.parse import urljoin from urllib.parse import urljoin
from playwright.async_api import Browser
from selectolax.parser import HTMLParser from selectolax.parser import HTMLParser
from .utils import Cache, Time, get_logger, leagues, network from .utils import Cache, Time, get_logger, leagues, network
@ -30,6 +30,22 @@ SPORT_ENDPOINTS = {
} }
async def process_event(url: str, url_num: int) -> str | None:
if not (html_data := await network.request(url, log=log)):
return
valid_m3u8 = re.compile(r"'clappr',\s+'([^\"]*)'", re.IGNORECASE)
if not (match := valid_m3u8.search(html_data.text)):
log.info(f"URL {url_num}) No M3U8 found")
return
log.info(f"URL {url_num}) Captured M3U8")
return match[1]
async def refresh_html_cache( async def refresh_html_cache(
url: str, url: str,
sport: str, sport: str,
@ -99,8 +115,8 @@ async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
live = [] live = []
start_ts = now.delta(hours=-1).timestamp() start_ts = now.delta(minutes=-30).timestamp()
end_ts = now.delta(minutes=5).timestamp() end_ts = now.delta(minutes=30).timestamp()
for k, v in events.items(): for k, v in events.items():
if k in cached_keys: if k in cached_keys:
@ -114,14 +130,12 @@ async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
return live return live
async def scrape(browser: Browser) -> None: async def scrape() -> None:
cached_urls = CACHE_FILE.load() cached_urls = CACHE_FILE.load()
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]} cached_count = len(cached_urls)
valid_count = cached_count = len(valid_urls) urls.update(cached_urls)
urls.update(valid_urls)
log.info(f"Loaded {cached_count} event(s) from cache") log.info(f"Loaded {cached_count} event(s) from cache")
@ -132,52 +146,44 @@ async def scrape(browser: Browser) -> None:
log.info(f"Processing {len(events)} new URL(s)") log.info(f"Processing {len(events)} new URL(s)")
if events: if events:
async with network.event_context(browser) as context: for i, ev in enumerate(events, start=1):
for i, ev in enumerate(events, start=1): handler = partial(
async with network.event_page(context) as page: process_event,
handler = partial( url=ev["link"],
network.process_event, url_num=i,
url=ev["link"], )
url_num=i,
page=page,
log=log,
)
url = await network.safe_process( url = await network.safe_process(
handler, handler,
url_num=i, url_num=i,
semaphore=network.PW_S, semaphore=network.HTTP_S,
log=log, log=log,
) )
sport, event, ts, link = ( if url:
ev["sport"], sport, event, ts, link = (
ev["event"], ev["sport"],
ev["event_ts"], ev["event"],
ev["link"], ev["event_ts"],
) ev["link"],
)
tvg_id, logo = leagues.get_tvg_info(sport, event) tvg_id, logo = leagues.get_tvg_info(sport, event)
key = f"[{sport}] {event} ({TAG})" key = f"[{sport}] {event} ({TAG})"
entry = { entry = {
"url": url, "url": url,
"logo": logo, "logo": logo,
"base": BASE_URL, "base": BASE_URL,
"timestamp": ts, "timestamp": ts,
"id": tvg_id or "Live.Event.us", "id": tvg_id or "Live.Event.us",
"link": link, "link": link,
} }
cached_urls[key] = entry urls[key] = cached_urls[key] = entry
if url: if new_count := len(cached_urls) - cached_count:
valid_count += 1
urls[key] = entry
if new_count := valid_count - cached_count:
log.info(f"Collected and cached {new_count} new event(s)") log.info(f"Collected and cached {new_count} new event(s)")
else: else: