Spaces:
Running
Running
Upload main.py with huggingface_hub
Browse files167 unmodified lines1681691701711721731 unmodified line1751761771781791801812 unmodified lines18418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321423 unmodified lines2382392402412422432441 unmodified line2462472482492502512526 unmodified lines259260261262263264265167 unmodified lines# ===== VixSrc Scraper (Token-based m3u8 - Multi-language audio) =====# Flow: API → embed HTML → extract token → master m3u8 with multi-audioasync def scrape_vixsrc(tmdb_id: int, media_type: str, season: int = None, episode: int = None):results = []1 unmodified linetry:async with httpx.AsyncClient(timeout=15, follow_redirects=True, headers=HEADERS) as client:# Step 1: Get embed pathif media_type == "movie":api_url = f"https://vixsrc.to/api/movie/{tmdb_id}"else:2 unmodified linesapi_url = f"https://vixsrc.to/api/tv/{tmdb_id}/{s}/{e}"api_resp = await client.get(api_url)api_data = api_resp.json()src_path = api_data.get("src", "")if not src_path:errors.append("VixSrc: No embed path returned")return results, errors# Step 2: Fetch embed HTMLembed_resp = await client.get(f"https://vixsrc.to{src_path}")html = embed_resp.text# Step 3: Extract token, expires, playlist URLtoken_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, errorstoken = token_match.group(1)expires = expires_match.group(1)playlist = url_match.group(1)# Step 4: Build master URLsep = "&" if "?" in playlist else "?"master_url = f"{playlist}{sep}token={token}&expires={expires}&h=1"23 unmodified linesquality = 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 resultif audio_tracks:for track in audio_tracks:results.append(StreamResult(1 unmodified linetitle=f"{track['language']} Audio",quality=quality,language=detect_language(track["language"]),url=video_url, # Video URL, audio is separatetype="m3u8",))else:6 unmodified linestype="m3u8",))else:# Direct m3u8 URLresults.append(StreamResult(source="VixSrc",title="Stream",167 unmodified lines1681691701711721731741 unmodified line1761771781791801811822 unmodified lines18518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427523 unmodified lines2993003013023033041 unmodified line3063073083093103113126 unmodified lines319320321322323324167 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 immediatelyasync def scrape_vixsrc(tmdb_id: int, media_type: str, season: int = None, episode: int = None):results = []1 unmodified linetry:async with httpx.AsyncClient(timeout=15, follow_redirects=True, headers=HEADERS) as client:# 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 linesapi_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()except:errors.append("VixSrc: API returned non-JSON (possible Cloudflare block)")return results, errorssrc_path = api_data.get("src", "")if not src_path:errors.append("VixSrc: No embed path returned")return results, errors# 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 onceapi_resp2 = await client.get(api_url)try:api_data2 = api_resp2.json()except:errors.append("VixSrc: API retry failed")return results, errorssrc_path2 = api_data2.get("src", "")if not src_path2:errors.append("VixSrc: No embed path on retry")return results, errorsembed_resp = await client.get(f"https://vixsrc.to{src_path2}")src_path = src_path2html = embed_resp.text# Step 3: Extract token, expires, playlist URL# Try multiple regex patterns since the HTML format can varytoken = Noneexpires = Noneplaylist = None# Pattern 1: key:"value" formattoken_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 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 formatif 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 tagif 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:passif not (token and playlist):errors.append("VixSrc: Could not extract stream parameters from embed")return results, errors# Step 4: Build master URLif 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 linesquality = resolution.group(1) if resolution else "Auto"if i + 1 < len(lines) and lines[i + 1].startswith("http"):video_url = lines[i + 1].strip()if audio_tracks:for track in audio_tracks:results.append(StreamResult(1 unmodified linetitle=f"{track['language']} Audio",quality=quality,language=detect_language(track["language"]),url=video_url,type="m3u8",))else:6 unmodified linestype="m3u8",))else:results.append(StreamResult(source="VixSrc",title="Stream",
main.py+71-12