iptv/M3U8/scrapers/roxie.py

182 lines
4.8 KiB
Python
Raw Normal View History

2025-12-08 13:21:43 -05:00
import asyncio
from functools import partial
from urllib.parse import urljoin
2026-02-15 02:18:22 -05:00
from playwright.async_api import Browser, Page, TimeoutError
2025-12-08 13:21:43 -05:00
from selectolax.parser import HTMLParser
from .utils import Cache, Time, get_logger, leagues, network
log = get_logger(__name__)
urls: dict[str, dict[str, str | float]] = {}
2025-12-13 16:57:14 -05:00
TAG = "ROXIE"
2026-03-22 12:14:17 -04:00
CACHE_FILE = Cache(TAG, exp=19_800)
2025-12-08 13:21:43 -05:00
2026-03-25 14:30:04 -04:00
BASE_URL = "https://roxiestreams.su"
2025-12-08 13:21:43 -05:00
2026-03-04 18:15:39 -05:00
SPORT_URLS = {
"March Madness": urljoin(BASE_URL, "march-madness"),
2026-03-04 18:15:39 -05:00
"Racing": urljoin(BASE_URL, "motorsports"),
# "American Football": urljoin(BASE_URL, "nfl"),
} | {
sport: urljoin(BASE_URL, sport.lower())
for sport in [
"Fighting",
"MLB",
"NBA",
"NHL",
"Soccer",
]
2025-12-08 13:21:43 -05:00
}
2026-02-14 15:43:53 -05:00
async def process_event(
url: str,
url_num: int,
page: Page,
2026-02-15 10:46:55 -05:00
) -> str | None:
2026-02-14 15:43:53 -05:00
try:
2026-03-02 20:02:26 -05:00
resp = await page.goto(
2026-02-14 15:43:53 -05:00
url,
wait_until="domcontentloaded",
timeout=6_000,
2026-02-14 15:43:53 -05:00
)
2026-03-03 16:59:09 -05:00
if not resp or resp.status != 200:
log.warning(
f"URL {url_num}) Status Code: {resp.status if resp else 'None'}"
)
2026-03-02 20:02:26 -05:00
return
2026-02-15 02:18:22 -05:00
try:
if btn := page.locator("button.streambutton").first:
await btn.click(
force=True,
click_count=2,
timeout=3_000,
)
2026-03-18 20:31:23 -04:00
await page.wait_for_function(
"() => typeof clapprPlayer !== 'undefined'",
timeout=6_000,
)
2026-03-18 20:31:23 -04:00
stream = await page.evaluate("() => clapprPlayer.options.source")
2026-02-15 02:18:22 -05:00
except TimeoutError:
2026-03-18 22:01:17 -04:00
log.warning(f"URL {url_num}) Could not find Clappr source")
2026-02-14 15:43:53 -05:00
return
2026-03-18 20:31:23 -04:00
log.info(f"URL {url_num}) Captured M3U8")
return stream
2026-02-14 15:43:53 -05:00
except Exception as e:
log.warning(f"URL {url_num}) {e}")
2026-02-14 15:43:53 -05:00
return
2025-12-18 03:04:11 -05:00
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
2026-03-22 12:14:17 -04:00
tasks = [network.request(url, log=log) for url in SPORT_URLS.values()]
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
results = await asyncio.gather(*tasks)
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
events = []
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
if not (
soups := [(HTMLParser(html.content), html.url) for html in results if html]
):
return events
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
for soup, url in soups:
sport = next((k for k, v in SPORT_URLS.items() if v == url), "Live Event")
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
for row in soup.css("table#eventsTable tbody tr"):
if not (a_tag := row.css_first("td a")):
continue
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
event = a_tag.text(strip=True)
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
if not (href := a_tag.attributes.get("href")):
continue
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
if f"[{sport}] {event} ({TAG})" in cached_keys:
continue
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
events.append(
{
"sport": sport,
"event": event,
2026-03-25 14:30:04 -04:00
"link": urljoin(BASE_URL, href),
2026-03-22 12:14:17 -04:00
}
)
2025-12-08 13:21:43 -05:00
2026-03-22 12:14:17 -04:00
return events
2025-12-08 13:21:43 -05:00
2026-02-13 15:21:16 -05:00
async def scrape(browser: Browser) -> None:
2025-12-08 13:21:43 -05:00
cached_urls = CACHE_FILE.load()
2025-12-18 04:14:54 -05:00
2026-02-12 18:15:01 -05:00
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
2025-12-18 04:14:54 -05:00
2026-02-12 18:15:01 -05:00
valid_count = cached_count = len(valid_urls)
urls.update(valid_urls)
2025-12-08 13:21:43 -05:00
log.info(f"Loaded {cached_count} event(s) from cache")
log.info(f'Scraping from "{BASE_URL}"')
2026-03-02 00:50:28 -05:00
if events := await get_events(cached_urls.keys()):
log.info(f"Processing {len(events)} new URL(s)")
2026-03-22 12:14:17 -04:00
now = Time.clean(Time.now())
2026-02-13 15:21:16 -05:00
async with network.event_context(browser) as context:
for i, ev in enumerate(events, start=1):
async with network.event_page(context) as page:
handler = partial(
2026-02-14 15:43:53 -05:00
process_event,
url=(link := ev["link"]),
2026-02-13 15:21:16 -05:00
url_num=i,
page=page,
)
url = await network.safe_process(
handler,
url_num=i,
semaphore=network.PW_S,
log=log,
)
2026-03-22 12:14:17 -04:00
sport, event = ev["sport"], ev["event"]
2026-02-13 15:21:16 -05:00
tvg_id, logo = leagues.get_tvg_info(sport, event)
key = f"[{sport}] {event} ({TAG})"
entry = {
"url": url,
"logo": logo,
"base": BASE_URL,
2026-03-22 12:14:17 -04:00
"timestamp": now.timestamp(),
2026-02-13 15:21:16 -05:00
"id": tvg_id or "Live.Event.us",
"link": link,
}
cached_urls[key] = entry
if url:
valid_count += 1
urls[key] = entry
2026-02-11 20:29:22 -05:00
2026-03-02 00:50:28 -05:00
log.info(f"Collected and cached {valid_count - cached_count} new event(s)")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
else:
log.info("No new events found")
CACHE_FILE.write(cached_urls)