import re import requests import base64 from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse, PlainTextResponse app = FastAPI() CLAPPR_TEMPLATE = """ Live Stream - {channel}
""" @app.get("/embed/{channel}") async def get_embed(channel: str): target_url = f"https://wc.ppvtv.top/embed/{channel}" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", "Referer": "https://ppvtv.top/" } try: response = requests.get(target_url, headers=headers, timeout=10) response.raise_for_status() html_content = response.text match = re.search(r'(/stream/[a-f0-9]+)', html_content) if match: path = match.group(1) stream_url = f"https://wc.ppvtv.top{path}" encoded_url = base64.b64encode(stream_url.encode('utf-8')).decode('utf-8') html_response = CLAPPR_TEMPLATE.format(channel=channel.upper(), encoded_url=encoded_url) return HTMLResponse(content=html_response, status_code=200) else: # Removed the debug text that leaks the stream URL layout raise HTTPException(status_code=404, detail="Stream link not found or channel is currently offline.") except requests.exceptions.HTTPError as http_err: # Handles 404, 500, etc., from the target source securely status_code = http_err.response.status_code if http_err.response else 502 raise HTTPException(status_code=status_code, detail="The requested stream is unavailable.") except requests.RequestException: # Handles timeouts, network drops, etc. raise HTTPException(status_code=502, detail="Failed to connect to streaming gateway.") # OPTIONAL SECURITY NOTE: Consider removing or commenting out the debug route entirely in production, # as anyone guessing it could read the source HTML of your stream provider. @app.get("/debug/{channel}") async def debug_source(channel: str): target_url = f"https://wc.ppvtv.top/embed/{channel}" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", "Referer": "https://ppvtv.top/" } try: response = requests.get(target_url, headers=headers, timeout=10) return PlainTextResponse(content=response.text) except Exception: return PlainTextResponse(content="Error fetching page safely.")