justabranch/M3U8/scrapers/roxie.py

252 lines
6.1 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"
CACHE_FILE = Cache(TAG, exp=10_800)
2025-12-08 13:21:43 -05:00
HTML_CACHE = Cache(f"{TAG}-html", exp=19_800)
2025-12-08 13:21:43 -05:00
2026-02-01 00:38:51 -05:00
BASE_URL = "https://roxiestreams.info"
2025-12-08 13:21:43 -05:00
SPORT_ENDPOINTS = {
"fighting": "Fighting",
2026-01-02 08:01:51 -05:00
# "mlb": "MLB",
2025-12-08 13:21:43 -05:00
"motorsports": "Racing",
"nba": "NBA",
# "nfl": "American Football",
"nhl": "NHL",
2025-12-08 13:21:43 -05:00
"soccer": "Soccer",
}
async def refresh_html_cache(
url: str,
sport: str,
now_ts: float,
) -> dict[str, dict[str, str | float]]:
2025-12-18 03:04:11 -05:00
events = {}
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
if not (html_data := await network.request(url, log=log)):
return events
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
soup = HTMLParser(html_data.content)
2025-12-08 13:21:43 -05:00
for row in soup.css("table#eventsTable tbody tr"):
if not (a_tag := row.css_first("td a")):
continue
event = a_tag.text(strip=True)
if not (href := a_tag.attributes.get("href")):
continue
if not (span := row.css_first("span.countdown-timer")):
continue
data_start = span.attributes["data-start"].rsplit(":", 1)[0]
event_dt = Time.from_str(data_start, timezone="PST")
event_sport = SPORT_ENDPOINTS[sport]
key = f"[{event_sport}] {event} ({TAG})"
events[key] = {
"sport": event_sport,
"event": event,
"link": href,
"event_ts": event_dt.timestamp(),
"timestamp": now_ts,
}
return events
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
captured: list[str] = []
got_one = asyncio.Event()
handler = partial(
network.capture_req,
captured=captured,
got_one=got_one,
)
page.on("request", handler)
try:
await page.goto(
url,
wait_until="domcontentloaded",
timeout=15_000,
)
2026-02-15 02:18:22 -05:00
try:
if btn := await page.wait_for_selector(
"button:has-text('Stream 1')",
timeout=5_000,
):
await btn.click()
except TimeoutError:
pass
2026-02-14 15:43:53 -05:00
wait_task = asyncio.create_task(got_one.wait())
try:
await asyncio.wait_for(wait_task, timeout=6)
except asyncio.TimeoutError:
log.warning(f"URL {url_num}) Timed out waiting for M3U8.")
return
finally:
if not wait_task.done():
wait_task.cancel()
try:
await wait_task
except asyncio.CancelledError:
pass
if captured:
log.info(f"URL {url_num}) Captured M3U8")
return captured[0]
log.warning(f"URL {url_num}) No M3U8 captured after waiting.")
return
except Exception as e:
log.warning(f"URL {url_num}) Exception while processing: {e}")
return
finally:
page.remove_listener("request", handler)
2025-12-18 03:04:11 -05:00
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
2025-12-08 13:21:43 -05:00
now = Time.clean(Time.now())
if not (events := HTML_CACHE.load()):
log.info("Refreshing HTML cache")
sport_urls = {sport: urljoin(BASE_URL, sport) for sport in SPORT_ENDPOINTS}
2025-12-08 13:21:43 -05:00
tasks = [
refresh_html_cache(
url,
sport,
now.timestamp(),
)
for sport, url in sport_urls.items()
]
results = await asyncio.gather(*tasks)
events = {k: v for data in results for k, v in data.items()}
HTML_CACHE.write(events)
live = []
2026-02-13 15:21:16 -05:00
start_ts = now.delta(hours=-1).timestamp()
end_ts = now.delta(minutes=5).timestamp()
2025-12-08 13:21:43 -05:00
for k, v in events.items():
2025-12-18 03:04:11 -05:00
if k in cached_keys:
2025-12-08 13:21:43 -05:00
continue
if not start_ts <= v["event_ts"] <= end_ts:
continue
2026-02-12 18:15:01 -05:00
live.append(v)
2025-12-08 13:21:43 -05:00
return live
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}"')
2025-12-18 03:04:11 -05:00
events = await get_events(cached_urls.keys())
2025-12-08 13:21:43 -05:00
log.info(f"Processing {len(events)} new URL(s)")
if events:
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,
2026-02-13 15:21:16 -05:00
url=ev["link"],
url_num=i,
page=page,
)
url = await network.safe_process(
handler,
url_num=i,
semaphore=network.PW_S,
log=log,
)
sport, event, ts, link = (
ev["sport"],
ev["event"],
ev["event_ts"],
ev["link"],
)
tvg_id, logo = leagues.get_tvg_info(sport, event)
key = f"[{sport}] {event} ({TAG})"
entry = {
"url": url,
"logo": logo,
"base": BASE_URL,
"timestamp": ts,
"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-02-12 18:15:01 -05:00
if new_count := valid_count - cached_count:
2025-12-08 13:21:43 -05:00
log.info(f"Collected and cached {new_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)