iptv/M3U8/scrapers/streameast.py
2025-09-28 11:28:28 -04:00

218 lines
5.9 KiB
Python

import asyncio
from datetime import datetime, timedelta
from functools import partial
from pathlib import Path
from urllib.parse import urljoin
import httpx
from playwright.async_api import async_playwright
from selectolax.parser import HTMLParser
from .utils import (
TZ,
capture_req,
get_base,
get_logger,
leagues,
load_cache,
new_browser,
now,
safe_process_event,
write_cache,
)
log = get_logger(__name__)
urls: dict[str, dict[str, str | float]] = {}
CACHE_FILE = Path(__file__).parent / "caches" / "streameast.json"
MIRRORS = [
"https://streameast.ga",
"https://streameast.tw",
"https://streameast.ph",
"https://streameast.sg",
"https://streameast.ch",
"https://streameast.ec",
"https://streameast.fi",
"https://streameast.ms",
"https://streameast.ps",
"https://streameast.cf",
"https://streameast.sk",
"https://thestreameast.co",
"https://thestreameast.fun",
"https://thestreameast.ru",
"https://thestreameast.su",
]
async def process_event(url: str, url_num: int) -> str | None:
async with async_playwright() as p:
browser, context = await new_browser(p, browser="brave")
page = await context.new_page()
captured: list[str] = []
got_one = asyncio.Event()
handler = partial(capture_req, captured=captured, got_one=got_one)
page.on("request", handler)
try:
await page.goto(url, wait_until="domcontentloaded", timeout=15_000)
wait_task = asyncio.create_task(got_one.wait())
try:
await asyncio.wait_for(wait_task, timeout=10)
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[-1]
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)
await page.close()
await browser.close()
async def get_events(
client: httpx.AsyncClient,
url: str,
cached_keys: set[str],
) -> list[dict[str, str]]:
try:
r = await client.get(url)
r.raise_for_status()
except Exception as e:
log.error(f'Failed to fetch "{url}"\n{e}')
return []
soup = HTMLParser(r.text)
events = []
start_dt = now - timedelta(minutes=30)
end_dt = now + timedelta(minutes=30)
for section in soup.css("div.se-sport-section"):
if not (sport := section.attributes.get("data-sport-name", "").strip()):
continue
for a in section.css("a.uefa-card"):
href = urljoin(url, a.attributes.get("href", ""))
team_spans = [t.text(strip=True) for t in a.css("span.uefa-name")]
if len(team_spans) == 2:
name = f"{team_spans[0]} vs {team_spans[1]}"
elif len(team_spans) == 1:
name = team_spans[0]
else:
continue
if not (time_span := a.css_first(".uefa-time")):
continue
time_text = time_span.text(strip=True)
timestamp = int(time_span.attributes.get("data-time", 0))
key = f"[{sport}] {name} (SEAST)"
if cached_keys & {key}:
continue
event_dt = datetime.fromtimestamp(timestamp, TZ)
if time_text == "LIVE" or (start_dt <= event_dt < end_dt):
events.append(
{
"sport": sport,
"event": name,
"link": href,
}
)
return events
async def scrape(client: httpx.AsyncClient) -> None:
cached_urls = load_cache(CACHE_FILE, exp=10_800)
cached_count = len(cached_urls)
urls.update(cached_urls)
log.info(f"Collected {cached_count} event(s) from cache")
if not (base_url := await get_base(client, MIRRORS)):
log.warning("No working StreamEast mirrors")
write_cache(CACHE_FILE, cached_urls)
return
log.info(f'Scraping from "{base_url}"')
events = await get_events(
client,
base_url,
set(cached_urls.keys()),
)
log.info(f"Processing {len(events)} new URL(s)")
for i, ev in enumerate(events, start=1):
url = await safe_process_event(
lambda: process_event(ev["link"], url_num=i),
url_num=i,
log=log,
)
if url:
sport, event = ev["sport"], ev["event"]
tvg_id, logo = leagues.info(sport)
if sport == "NBA" and leagues.is_valid(event, "WNBA"):
sport = "WNBA"
tvg_id, logo = leagues.info("WNBA")
key = f"[{sport}] {event} (SEAST)"
entry = {
"url": url,
"logo": logo,
"base": base_url,
"timestamp": now.timestamp(),
"id": tvg_id or "Live.Event.us",
}
urls[key] = cached_urls[key] = entry
if new_count := len(cached_urls) - cached_count:
log.info(f"Collected and cached {new_count} new event(s)")
else:
log.info("No new events found")
write_cache(CACHE_FILE, cached_urls)