Update app.py
Browse files22 unmodified lines2324252627282935 unmodified lines656667686970716 unmodified lines7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411517 unmodified lines13313413513613713822 unmodified lines@app.get("/embed/{channel_id}", response_class=HTMLResponse)async def serve_player_page(channel_id: str, request: Request):# DYNAMIC FIX: Automatically detects the exact URL/domain you are currently visitingbase_url = str(request.base_url).rstrip('/')manifest_proxy_url = f"{base_url}/stream-data/{channel_id}"35 unmodified lines@app.get("/stream-data/{channel_id}")async def get_manifest_stream(channel_id: str):target_base = os.getenv("TARGET_DOMAIN", "https://wc.ppvtv.top").rstrip('/')embed_url = f"{target_base}/embed/{channel_id}"request_headers = {6 unmodified linesasync with httpx.AsyncClient(follow_redirects=True, timeout=15.0) as client:try:# 1. Fetch source pageresponse = await client.get(embed_url, headers=request_headers)# Emergency Backup Fallbackif response.status_code != 200:target_base = "https://ppvtv.top"embed_url = f"{target_base}/embed/{channel_id}"request_headers["Referer"] = f"{target_base}/"request_headers["Origin"] = target_baseresponse = await client.get(embed_url, headers=request_headers)if response.status_code != 200:raise HTTPException(status_code=502, detail=f"Source site down or blocked. Code: {response.status_code}")html = response.text# 2. Extract current streaming playlist token arraymatch = re.search(r'var\s+STREAMS\s*=\s*\[\s*["\']([^"\']+)["\']\s*\]', html)if not match:raise HTTPException(status_code=404, detail="Token signature pattern not found on target host.")relative_stream_path = match.group(1)absolute_stream_url = f"{target_base}{relative_stream_path}"# 3. Pull playlist data from streaming serversstream_response = await client.get(absolute_stream_url, headers=request_headers)if stream_response.status_code != 200:raise HTTPException(status_code=502, detail="Failed to fetch playlist array file from media server.")manifest_content = stream_response.text# 4. Correct relative internal data chunk pathwaysbase_stream_dir = absolute_stream_url.rsplit('/', 1)[0] + '/'fixed_manifest = []17 unmodified lines)except httpx.RequestError as exc:raise HTTPException(status_code=504, detail=f"Target proxy timeout connection error: {str(exc)}")except Exception as e:raise HTTPException(status_code=500, detail=str(e))22 unmodified lines23242526272835 unmodified lines64656667686970716 unmodified lines7879808182838485868788899091929394959697989910010110210310410510610710810911011111217 unmodified lines13013113213313413522 unmodified lines@app.get("/embed/{channel_id}", response_class=HTMLResponse)async def serve_player_page(channel_id: str, request: Request):base_url = str(request.base_url).rstrip('/')manifest_proxy_url = f"{base_url}/stream-data/{channel_id}"35 unmodified lines@app.get("/stream-data/{channel_id}")async def get_manifest_stream(channel_id: str):# FIXED: Lock base strictly to the streaming sub-clustertarget_base = "https://wc.ppvtv.top"embed_url = f"{target_base}/embed/{channel_id}"request_headers = {6 unmodified linesasync with httpx.AsyncClient(follow_redirects=True, timeout=15.0) as client:try:# 1. Fetch exact source embed layout from wc clusterresponse = await client.get(embed_url, headers=request_headers)# If sub-cluster responds with an error, look up the root backup but keep structural paths bound to wcif response.status_code != 200:backup_url = f"https://ppvtv.top/embed/{channel_id}"response = await client.get(backup_url, headers=request_headers)if response.status_code != 200:raise HTTPException(status_code=502, detail=f"Source server down. Code: {response.status_code}")html = response.text# 2. Extract current stream tokenmatch = re.search(r'var\s+STREAMS\s*=\s*\[\s*["\']([^"\']+)["\']\s*\]', html)if not match:raise HTTPException(status_code=404, detail="Token signature pattern not found.")relative_stream_path = match.group(1)absolute_stream_url = f"{target_base}{relative_stream_path}"# 3. Pull target playlist source textstream_response = await client.get(absolute_stream_url, headers=request_headers)if stream_response.status_code != 200:raise HTTPException(status_code=502, detail="Failed to fetch layout playlist array.")manifest_content = stream_response.text# 4. Reconstruct individual media segments relative to the streaming cluster basebase_stream_dir = absolute_stream_url.rsplit('/', 1)[0] + '/'fixed_manifest = []17 unmodified lines)except httpx.RequestError as exc:raise HTTPException(status_code=504, detail=f"Proxy timeout connection failure: {str(exc)}")except Exception as e:raise HTTPException(status_code=500, detail=str(e))
app.py+13-16