iptv/M3U8/scrapers/ppv.py

166 lines
4.5 KiB
Python
Raw Normal View History

2026-02-28 15:42:50 -05:00
import re
2025-12-08 13:21:43 -05:00
from functools import partial
from playwright.async_api import Browser
2025-12-08 13:21:43 -05:00
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 = "PPV"
CACHE_FILE = Cache(TAG, exp=10_800)
2025-12-08 13:21:43 -05:00
API_FILE = Cache(f"{TAG}-api", exp=19_800)
2025-12-08 13:21:43 -05:00
2026-02-28 15:42:50 -05:00
API_MIRRORS = [
2026-02-11 23:27:06 -05:00
"https://api.ppv.to/api/streams",
2026-02-22 00:37:27 -05:00
"https://api.ppv.cx/api/streams",
"https://api.ppv.sh/api/streams",
"https://api.ppv.la/api/streams",
2025-12-08 13:21:43 -05:00
]
2026-02-28 15:42:50 -05:00
def fix_url(s: str) -> str:
pattern = re.compile(r"index\.m3u8$", re.I)
return pattern.sub(r"tracks-v1a1/mono.ts.m3u8", s)
async def get_events(url: str, cached_keys: list[str]) -> list[dict[str, str]]:
now = Time.clean(Time.now())
2025-12-08 13:21:43 -05:00
if not (api_data := API_FILE.load(per_entry=False)):
2025-12-27 10:25:35 -05:00
log.info("Refreshing API cache")
api_data = {"timestamp": now.timestamp()}
2025-12-08 13:21:43 -05:00
2026-02-11 23:27:06 -05:00
if r := await network.request(url, log=log):
2025-12-18 03:04:11 -05:00
api_data: dict = r.json()
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
API_FILE.write(api_data)
2025-12-08 13:21:43 -05:00
events = []
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30)
for stream_group in api_data.get("streams", []):
sport = stream_group["category"]
if sport == "24/7 Streams":
continue
for event in stream_group.get("streams", []):
name = event.get("name")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
start_ts = event.get("starts_at")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
logo = event.get("poster")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
iframe = event.get("iframe")
if not (name and start_ts and iframe):
continue
2025-12-18 03:04:11 -05:00
if f"[{sport}] {name} ({TAG})" in cached_keys:
2025-12-08 13:21:43 -05:00
continue
event_dt = Time.from_ts(start_ts)
if not start_dt <= event_dt <= end_dt:
continue
events.append(
{
"sport": sport,
"event": name,
"link": iframe,
"logo": logo,
"timestamp": event_dt.timestamp(),
}
)
return events
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
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
2025-12-18 04:14:54 -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")
2026-02-28 15:42:50 -05:00
if not (api_url := await network.get_base(API_MIRRORS)):
2025-12-08 13:21:43 -05:00
log.warning("No working PPV mirrors")
2025-12-25 01:45:49 -05:00
2025-12-08 13:21:43 -05:00
CACHE_FILE.write(cached_urls)
2025-12-25 01:45:49 -05:00
2025-12-08 13:21:43 -05:00
return
2026-02-28 15:42:50 -05:00
log.info(f'Scraping from "{api_url}"')
2025-12-12 10:46:56 -05:00
2026-03-02 00:50:28 -05:00
if events := await get_events(api_url, cached_urls.keys()):
log.info(f"Processing {len(events)} new URL(s)")
2026-01-24 11:50:43 -05:00
async with network.event_context(browser, stealth=False) as context:
for i, ev in enumerate(events, start=1):
async with network.event_page(context) as page:
2025-12-23 13:28:56 -05:00
handler = partial(
network.process_event,
url=(link := ev["link"]),
2025-12-23 13:28:56 -05:00
url_num=i,
page=page,
2025-12-23 13:28:56 -05:00
timeout=6,
log=log,
2025-12-08 13:21:43 -05:00
)
2025-12-23 13:28:56 -05:00
url = await network.safe_process(
handler,
url_num=i,
semaphore=network.PW_S,
log=log,
)
sport, event, logo, ts = (
ev["sport"],
ev["event"],
ev["logo"],
ev["timestamp"],
)
key = f"[{sport}] {event} ({TAG})"
tvg_id, pic = leagues.get_tvg_info(sport, event)
entry = {
"url": url,
"logo": logo or pic,
"base": link,
"timestamp": ts,
"id": tvg_id or "Live.Event.us",
"link": link,
}
cached_urls[key] = entry
2025-12-23 13:28:56 -05:00
if url:
valid_count += 1
2026-02-28 15:42:50 -05:00
entry["url"] = fix_url(url)
urls[key] = entry
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)