Spaces:
Running
Running
Upload main.py with huggingface_hub
Browse files167 unmodified lines1681691701711721731741 unmodified line1761771781781791801811822 unmodified lines18518618718818919018719119219319419519619719819920020119420220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123220320420523323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426520720820926626726826927027127227327427523 unmodified lines2993003012413023033041 unmodified line3063073082493093103113126 unmodified lines319320321262322323324167 unmodified lines
# ===== VixSrc Scraper (Token-based m3u8 - Multi-language audio) =====# Flow: API → embed HTML → extract token → master m3u8 with multi-audio# Note: The src_path token expires VERY fast (~2s), so we must fetch immediately
async def scrape_vixsrc(tmdb_id: int, media_type: str, season: int = None, episode: int = None): results = []1 unmodified line
try: async with httpx.AsyncClient(timeout=15, follow_redirects=True, headers=HEADERS) as client: # Step 1: Get embed path # Step 1: Get embed path (contains short-lived token) if media_type == "movie": api_url = f"https://vixsrc.to/api/movie/{tmdb_id}" else:2 unmodified lines api_url = f"https://vixsrc.to/api/tv/{tmdb_id}/{s}/{e}"
api_resp = await client.get(api_url) # VixSrc sometimes returns HTML instead of JSON (Cloudflare) try: api_data = api_resp.json() api_data = api_resp.json() except: errors.append("VixSrc: API returned non-JSON (possible Cloudflare block)") return results, errors
src_path = api_data.get("src", "")
if not src_path: errors.append("VixSrc: No embed path returned") return results, errors
# Step 2: Fetch embed HTML # Step 2: IMMEDIATELY fetch embed HTML (token expires fast) embed_resp = await client.get(f"https://vixsrc.to{src_path}") if embed_resp.status_code == 410: # Token expired, retry once api_resp2 = await client.get(api_url) try: api_data2 = api_resp2.json() except: errors.append("VixSrc: API retry failed") return results, errors src_path2 = api_data2.get("src", "") if not src_path2: errors.append("VixSrc: No embed path on retry") return results, errors embed_resp = await client.get(f"https://vixsrc.to{src_path2}") src_path = src_path2
html = embed_resp.text
# Step 3: Extract token, expires, playlist URL # Try multiple regex patterns since the HTML format can vary token = None expires = None playlist = None
# Pattern 1: key:"value" format token_match = re.search(r'token["\']\s*:\s*["\']([^"\']+)', html) expires_match = re.search(r'expires["\']\s*:\s*["\']([^"\']+)', html) url_match = re.search(r'url\s*:\s*["\']([^"\']+)', html)
if not (token_match and expires_match and url_match): errors.append("VixSrc: Could not extract stream parameters") return results, errors if token_match: token = token_match.group(1) if expires_match: expires = expires_match.group(1) if url_match: playlist = url_match.group(1)
# Pattern 2: JavaScript variable format if not token: token_match2 = re.search(r'var\s+token\s*=\s*["\']([^"\']+)', html) if token_match2: token = token_match2.group(1) if not expires: expires_match2 = re.search(r'var\s+expires\s*=\s*["\']([^"\']+)', html) if expires_match2: expires = expires_match2.group(1) if not playlist: playlist_match2 = re.search(r'(?:playlist|file|src)\s*[:=]\s*["\']([^"\']+/playlist[^"\']*)', html) if playlist_match2: playlist = playlist_match2.group(1)
# Pattern 3: JSON object in script tag if not token: json_match = re.search(r'\{[^}]*token[^}]*\}', html) if json_match: try: j = json.loads(json_match.group(0)) token = j.get("token") expires = j.get("expires") playlist = j.get("url") or j.get("playlist") or j.get("file") except: pass
token = token_match.group(1) expires = expires_match.group(1) playlist = url_match.group(1) if not (token and playlist): errors.append("VixSrc: Could not extract stream parameters from embed") return results, errors
# Step 4: Build master URL if not expires: expires = str(int(time.time()) + 86400) sep = "&" if "?" in playlist else "?" master_url = f"{playlist}{sep}token={token}&expires={expires}&h=1"
23 unmodified lines quality = resolution.group(1) if resolution else "Auto" if i + 1 < len(lines) and lines[i + 1].startswith("http"): video_url = lines[i + 1].strip() # Add each audio track as a separate result if audio_tracks: for track in audio_tracks: results.append(StreamResult(1 unmodified line title=f"{track['language']} Audio", quality=quality, language=detect_language(track["language"]), url=video_url, # Video URL, audio is separate url=video_url, type="m3u8", )) else:6 unmodified lines type="m3u8", )) else: # Direct m3u8 URL results.append(StreamResult( source="VixSrc", title="Stream",main.py+71-12