diff --git a/scripts/lib/proxer_utils.py b/scripts/lib/proxer_utils.py index 3973325..3d3e936 100644 --- a/scripts/lib/proxer_utils.py +++ b/scripts/lib/proxer_utils.py @@ -32,10 +32,9 @@ class NotFoundError(ProxerError): class NetworkError(ProxerError): """Raised when there is a network-related error""" - def get_embed_url(episode_url: str, auth_cookie: tuple[str, str]) -> str: """ - Extracts the embed URL from a Proxer episode page. + Extracts the embed URL from a Proxer episode page with enhanced logging. :param episode_url: URL of the Proxer episode page :param auth_cookie: Tuple containing (cookie_name, cookie_value) @@ -45,12 +44,26 @@ def get_embed_url(episode_url: str, auth_cookie: tuple[str, str]) -> str: """ SEARCH_TOKEN = '"code":"' SEARCH_SERVER = "stream-service.proxer.me" - HEADERS = {'User-Agent': 'Mozilla/5.0'} + HEADERS = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' + } + + # Strip URL fragment + episode_url = episode_url.split("#")[0] try: - response = get(episode_url, cookies={auth_cookie[0]: auth_cookie[1], 'stream_choose': 'proxer-stream'}, headers=HEADERS) + logging.info(f"[DEBUG] Fetching episode URL: {episode_url}") + response = get( + episode_url, + cookies={auth_cookie[0]: auth_cookie[1], 'stream_choose': 'proxer-stream'}, + headers=HEADERS, + timeout=15 + ) response.raise_for_status() + logging.info(f"[DEBUG] HTTP status: {response.status_code}") content = response.text + logging.debug(f"[DEBUG] Page snippet: {content[:500].replace(chr(10), ' ')}") except RequestException as e: raise NetworkError(f"Failed to fetch episode page: {e}") @@ -59,16 +72,16 @@ def get_embed_url(episode_url: str, auth_cookie: tuple[str, str]) -> str: if code_index < len(SEARCH_TOKEN): raise NotFoundError("Embed code not found in page content.") - code : str = content[code_index:].partition('"')[0] - - # Detect the correct streaming service - host : str = "stream-service" if SEARCH_SERVER in content else "stream" - return f"https://{host}.proxer.me/embed-{code}.html" + code: str = content[code_index:].partition('"')[0] + host: str = "stream-service" if SEARCH_SERVER in content else "stream" + embed_url = f"https://{host}.proxer.me/embed-{code}.html" + logging.info(f"[DEBUG] Found embed URL: {embed_url}") + return embed_url def get_mp4_url(embed_url: str) -> str: """ - Extracts the direct MP4 URL from an embed page. + Extracts the direct MP4 URL from an embed page with enhanced logging. :param embed_url: URL of the Proxer embed player :returns: Direct MP4 URL @@ -76,26 +89,33 @@ def get_mp4_url(embed_url: str) -> str: :raises NetworkError: If a request fails """ SEARCH_TOKEN = 'video/mp4" src="' - HEADERS = {'User-Agent': 'Mozilla/5.0'} + HEADERS = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' + } try: - response = get(embed_url, headers=HEADERS) + logging.info(f"[DEBUG] Fetching embed URL: {embed_url}") + response = get(embed_url, headers=HEADERS, timeout=15) response.raise_for_status() + logging.info(f"[DEBUG] HTTP status: {response.status_code}") content = response.text + logging.debug(f"[DEBUG] Embed snippet: {content[:500].replace(chr(10), ' ')}") except RequestException as e: raise NetworkError(f"Failed to fetch embed page: {e}") - # Extract the MP4 URL link_index = content.find(SEARCH_TOKEN) + len(SEARCH_TOKEN) if link_index < len(SEARCH_TOKEN): raise NotFoundError("MP4 URL not found in embed page.") - return content[link_index:].partition('"')[0] + mp4_url = content[link_index:].partition('"')[0] + logging.info(f"[DEBUG] Found MP4 URL: {mp4_url}") + return mp4_url def download_mp4(mp4_url: str, save_path: str, label: str = "") -> None: """ - Downloads an MP4 file from a given URL and displays a progress bar. + Downloads an MP4 file from a given URL with enhanced logging. :param mp4_url: URL of the MP4 file :param save_path: Path to save the downloaded file @@ -105,21 +125,25 @@ def download_mp4(mp4_url: str, save_path: str, label: str = "") -> None: CHUNK_SIZE = 255 MAX_LABEL_LENGTH = 32 - # Adjust label for progress bar if label: label = label[:MAX_LABEL_LENGTH - 3] + "..." if len(label) > MAX_LABEL_LENGTH else label.ljust(MAX_LABEL_LENGTH) + logging.info(f"[DEBUG] Starting download: {mp4_url} -> {save_path}") + try: - response = get(mp4_url, stream=True) + response = get(mp4_url, stream=True, timeout=30) response.raise_for_status() total_length = int(response.headers.get('content-length', 0)) except RequestException as e: raise NetworkError(f"Failed to start download: {e}") - # Download file with progress bar with open(save_path, "wb") as file: if total_length: - for chunk in progress.bar(response.iter_content(chunk_size=CHUNK_SIZE), expected_size=total_length / CHUNK_SIZE, label=label): + for chunk in progress.bar( + response.iter_content(chunk_size=CHUNK_SIZE), + expected_size=total_length / CHUNK_SIZE, + label=label + ): if chunk: file.write(chunk) else: