Spaces:
Sleeping
Sleeping

Refactor Embed class by removing unused Host header; clean up NetFree class by deleting VideoData dataclass; remove unused import in VidSrc class; add requirements file with dependencies.
2b8e50d
import requests | |
import re | |
import urllib.parse as urlparse | |
from helpers.functions import Utils | |
class VidSrc: | |
def __init__(self): | |
self.base_url = "https://vidsrc.su" | |
self.headers = { | |
'Accept': '*/*', | |
} | |
self.video_data_list = [] | |
self.utils = Utils() | |
def build_url(self, tmdb_id, media_type, season=None, episode=None): | |
if media_type == "movie": | |
return f"{self.base_url}/embed/movie/{tmdb_id}" | |
elif media_type == "tv": | |
return f"{self.base_url}/embed/tv/{tmdb_id}/{season}/{episode}" | |
else: | |
raise ValueError("Invalid media type. Must be 'movie' or 'tv'.") | |
def extract_urls(self, data): | |
return re.findall(r"url:\s*'(https?://[^\']+)'", data) | |
def sanitize_stream_url(self, raw_url): | |
try: | |
unquoted_url = urlparse.unquote(raw_url) | |
stream_part = unquoted_url.split("?url=")[-1].split(".m3u8")[0] + ".m3u8" | |
stream_url = urlparse.unquote(stream_part) | |
return stream_url | |
except Exception as e: | |
print(f"[sanitize_stream_url] Error: {e}") | |
return None | |
def get_stream(self, tmdb_id, imdb_id, media_type, title, year, season=None, episode=None): | |
self.video_data_list = [] | |
final_url = self.build_url(tmdb_id, media_type, season, episode) | |
response = requests.get(final_url, headers=self.headers) | |
if response.status_code != 200: | |
print(f"[get_stream] Failed to fetch embed page. Status code: {response.status_code}") | |
return self.video_data_list | |
data = response.text | |
stream_urls = self.extract_urls(data) | |
for stream_url in stream_urls: | |
if stream_url.endswith(".m3u8") or stream_url.endswith(".mp4"): | |
headers = { | |
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36", | |
'Referer': 'https://vidsrc.su/', | |
'Origin': 'https://vidsrc.su/', | |
} | |
if self.utils.is_accessible(stream_url, headers=headers): | |
self.video_data_list.append({ | |
"videoSource": f"VIDSRC_{len(self.video_data_list)+1}", | |
"videoUrl": stream_url, | |
"videoHeaders": { | |
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36", | |
'Referer': 'https://vidsrc.su/', | |
'Origin': 'https://vidsrc.su/', | |
}, | |
}) | |
else: | |
sanitized_url = self.sanitize_stream_url(stream_url) | |
if sanitized_url: | |
self.video_data_list.append({ | |
"videoSource": f"VIDSRC_{len(self.video_data_list)+1}", | |
"videoUrl": sanitized_url, | |
"videoHeaders": {} | |
}) | |
return self.video_data_list | |