#!/usr/bin/env python3 import asyncio import json import re 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 .utils import ( LOGOS, TZ, capture_req, get_base, get_logger, load_cache, new_browser, now, safe_process_event, write_cache, ) log = get_logger(__name__) urls: dict[str, dict[str, str | float]] = {} API_FILE = Path(__file__).parent / "caches" / "ppv_api.json" CACHE_FILE = Path(__file__).parent / "caches" / "ppv.json" MIRRORS = [ "https://ppvs.su", "https://ppv.to", "https://ppv.wtf", "https://ppv.land", "https://freeppv.fun", ] async def refresh_api_cache( client: httpx.AsyncClient, url: str ) -> dict[str, dict[str, str]]: log.info("Refreshing API cache") try: r = await client.get(url) r.raise_for_status() except Exception as e: log.error(f'Failed to fetch "{url}"\n{e}') return {} return r.json() async def process_event(url: str, url_num: int) -> str | None: async with async_playwright() as p: browser, context = await new_browser(p) 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, api_url: str, cached_keys: set[str], ) -> list[dict[str, str]]: events: list[dict[str, str]] = [] base_url = re.match(r"(https?://.+?)/", api_url)[1] if not ( api_data := load_cache( API_FILE, exp=86400, nearest_hr=True, per_entry=False, ) ): api_data = await refresh_api_cache(client, api_url) API_FILE.write_text(json.dumps(api_data, indent=2), encoding="utf-8") for stream_group in api_data["streams"]: sport = stream_group["category"] if sport == "24/7 Streams": continue for event in stream_group["streams"]: name, start_ts, end_ts, logo, uri_name = ( event["name"], event["starts_at"], event["ends_at"], event.get("poster", LOGOS["default"]), event["uri_name"], ) key = f"[{sport}] {name} (PPV)" if cached_keys & {key}: continue start_dt = datetime.fromtimestamp(start_ts, tz=TZ) - timedelta(minutes=30) end_dt = datetime.fromtimestamp(end_ts, tz=TZ) + timedelta(minutes=30) if not start_dt <= now < end_dt: continue events.append( { "sport": sport, "event": name, "link": urljoin(base_url, f"live/{uri_name}"), "logo": logo, } ) return events async def main(client: httpx.AsyncClient) -> None: cached_urls = load_cache(CACHE_FILE, exp=10800) 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 PPV mirrors") write_cache(CACHE_FILE, cached_urls) return log.info(f'Scraping from "{base_url}"') events = await get_events( client, urljoin(base_url, "api/streams"), 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: key = f"[{ev['sport']}] {ev['event']} (PPV)" entry = { "url": url, "logo": ev["logo"], "base": base_url, "timestamp": now.timestamp(), } 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) # works if no cloudflare bot detection