iptv/M3U8/scrapers/ppv.py

159 lines
4.1 KiB
Python
Raw Normal View History

2025-09-04 19:53:27 -04:00
from functools import partial
2025-09-03 15:00:17 -04:00
from urllib.parse import urljoin
import httpx
2025-10-30 15:38:34 -04:00
from playwright.async_api import async_playwright
2025-09-04 19:53:27 -04:00
2025-10-01 11:57:49 -04:00
from .utils import Cache, Time, get_logger, leagues, network
2025-09-03 15:00:17 -04:00
log = get_logger(__name__)
2025-09-04 14:50:52 -04:00
urls: dict[str, dict[str, str | float]] = {}
2025-09-03 15:00:17 -04:00
2025-11-13 12:43:55 -05:00
CACHE_FILE = Cache("ppv.json", exp=10_800)
2025-09-03 15:00:17 -04:00
2025-11-13 12:43:55 -05:00
API_FILE = Cache("ppv-api.json", exp=28_800)
2025-09-03 15:00:17 -04:00
2025-10-31 14:45:23 -04:00
BASE_URL = "https://ppv.to"
2025-09-03 15:00:17 -04:00
2025-09-20 23:26:18 -04:00
2025-09-09 13:34:16 -04:00
async def refresh_api_cache(
2025-10-19 11:38:06 -04:00
client: httpx.AsyncClient,
url: str,
2025-09-09 13:34:16 -04:00
) -> dict[str, dict[str, str]]:
2025-09-03 15:00:17 -04:00
log.info("Refreshing API cache")
try:
r = await client.get(url)
r.raise_for_status()
except Exception as e:
2025-10-15 13:02:18 -04:00
log.error(f'Failed to fetch "{url}": {e}')
2025-11-13 12:43:55 -05:00
2025-09-03 15:00:17 -04:00
return {}
return r.json()
async def get_events(
client: httpx.AsyncClient,
2025-09-03 18:41:07 -04:00
cached_keys: set[str],
2025-09-04 19:53:27 -04:00
) -> list[dict[str, str]]:
2025-10-01 22:28:01 -04:00
if not (api_data := API_FILE.load(per_entry=False)):
2025-10-10 19:29:17 -04:00
api_data = await refresh_api_cache(
client,
urljoin(
2025-11-03 04:13:00 -05:00
BASE_URL,
2025-10-10 19:29:17 -04:00
"api/streams",
),
)
2025-09-20 23:26:18 -04:00
2025-10-01 11:57:49 -04:00
API_FILE.write(api_data)
2025-09-03 15:00:17 -04:00
2025-11-13 12:43:55 -05:00
events = []
2025-10-01 18:34:18 -04:00
2025-10-02 12:57:25 -04:00
now = Time.clean(Time.now())
2025-10-01 18:34:18 -04:00
start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30)
2025-10-01 14:56:15 -04:00
for stream_group in api_data["streams"]:
sport = stream_group["category"]
2025-09-03 15:00:17 -04:00
if sport == "24/7 Streams":
continue
2025-10-01 14:56:15 -04:00
for event in stream_group["streams"]:
2025-10-19 11:38:06 -04:00
name = event.get("name")
start_ts = event.get("starts_at")
logo = event.get("poster")
iframe = event.get("iframe")
if not (name and start_ts and iframe):
continue
2025-09-03 15:00:17 -04:00
2025-09-13 04:42:55 -04:00
key = f"[{sport}] {name} (PPV)"
2025-09-03 15:00:17 -04:00
2025-09-13 04:42:55 -04:00
if cached_keys & {key}:
2025-09-03 15:00:17 -04:00
continue
2025-10-01 18:34:18 -04:00
event_dt = Time.from_ts(start_ts)
2025-09-03 15:00:17 -04:00
2025-10-04 15:17:00 -04:00
if not start_dt <= event_dt <= end_dt:
2025-09-03 15:00:17 -04:00
continue
events.append(
{
"sport": sport,
"event": name,
2025-10-09 18:58:53 -04:00
"link": iframe,
2025-09-03 15:00:17 -04:00
"logo": logo,
2025-10-01 18:34:18 -04:00
"timestamp": event_dt.timestamp(),
2025-09-03 15:00:17 -04:00
}
)
return events
2025-09-20 23:26:18 -04:00
async def scrape(client: httpx.AsyncClient) -> None:
2025-10-01 11:57:49 -04:00
cached_urls = CACHE_FILE.load()
2025-09-03 15:00:17 -04:00
cached_count = len(cached_urls)
2025-09-05 15:56:07 -04:00
urls.update(cached_urls)
2025-09-03 15:00:17 -04:00
2025-10-01 11:57:49 -04:00
log.info(f"Loaded {cached_count} event(s) from cache")
2025-09-04 14:50:52 -04:00
2025-10-31 14:45:23 -04:00
log.info(f'Scraping from "{BASE_URL}"')
2025-09-08 12:02:36 -04:00
2025-11-03 04:13:00 -05:00
events = await get_events(client, set(cached_urls.keys()))
2025-09-03 15:00:17 -04:00
2025-09-05 15:56:07 -04:00
log.info(f"Processing {len(events)} new URL(s)")
2025-09-03 15:00:17 -04:00
2025-10-29 03:21:18 -04:00
if events:
async with async_playwright() as p:
2025-11-13 19:39:17 -05:00
browser, context = await network.browser(p, browser="brave")
2025-10-29 03:21:18 -04:00
for i, ev in enumerate(events, start=1):
handler = partial(
2025-10-30 15:38:34 -04:00
network.process_event,
2025-10-29 03:21:18 -04:00
url=ev["link"],
url_num=i,
context=context,
2025-10-30 15:38:34 -04:00
timeout=6,
log=log,
2025-10-29 03:21:18 -04:00
)
2025-09-19 02:05:40 -04:00
2025-10-29 03:21:18 -04:00
url = await network.safe_process(
handler,
url_num=i,
log=log,
2025-10-09 20:18:37 -04:00
)
2025-10-29 03:21:18 -04:00
if url:
sport, event, logo, ts = (
ev["sport"],
ev["event"],
ev["logo"],
ev["timestamp"],
)
2025-10-09 20:18:37 -04:00
2025-10-29 03:21:18 -04:00
key = f"[{sport}] {event} (PPV)"
2025-10-19 11:38:06 -04:00
2025-10-29 03:21:18 -04:00
tvg_id, pic = leagues.get_tvg_info(sport, event)
entry = {
"url": url,
"logo": logo or pic,
2025-10-31 14:45:23 -04:00
"base": BASE_URL,
2025-10-29 03:21:18 -04:00
"timestamp": ts,
"id": tvg_id or "Live.Event.us",
}
2025-09-13 04:42:55 -04:00
2025-10-29 03:21:18 -04:00
urls[key] = cached_urls[key] = entry
2025-09-03 15:00:17 -04:00
2025-10-29 03:21:18 -04:00
await browser.close()
2025-09-03 15:00:17 -04:00
2025-09-09 13:34:16 -04:00
if new_count := len(cached_urls) - cached_count:
2025-09-05 15:56:07 -04:00
log.info(f"Collected and cached {new_count} new event(s)")
else:
log.info("No new events found")
2025-09-03 15:00:17 -04:00
2025-10-01 11:57:49 -04:00
CACHE_FILE.write(cached_urls)