From 27e711ba51ad7c1c39fdda42703c8f1ae1e9e1c7 Mon Sep 17 00:00:00 2001 From: mhdzumair Date: Thu, 19 Dec 2024 16:54:36 +0530 Subject: [PATCH] Show the torrent file extraction failure message when manually import torrent --- scrapers/routes.py | 7 ++++++- utils/torrent.py | 13 ++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/scrapers/routes.py b/scrapers/routes.py index 581908b..86921f3 100644 --- a/scrapers/routes.py +++ b/scrapers/routes.py @@ -211,7 +211,12 @@ async def add_torrent( raise_error("Failed to fetch torrent metadata.") torrent_data = data[0] elif torrent_file: - torrent_data = torrent.extract_torrent_metadata(await torrent_file.read()) + try: + torrent_data = torrent.extract_torrent_metadata( + await torrent_file.read(), is_raise_error=True + ) + except ValueError as e: + raise_error(str(e)) if not torrent_data: raise_error("Failed to extract torrent metadata.") if settings.adult_content_filter_in_torrent_title and torrent_data.get("adult"): diff --git a/utils/torrent.py b/utils/torrent.py index db480dd..08b7474 100644 --- a/utils/torrent.py +++ b/utils/torrent.py @@ -27,7 +27,9 @@ from utils.validation_helper import is_video_file logging.getLogger("demagnetize").setLevel(logging.CRITICAL) -def extract_torrent_metadata(content: bytes, is_parse_ptt: bool = True) -> dict: +def extract_torrent_metadata( + content: bytes, is_parse_ptt: bool = True, is_raise_error: bool = False +) -> dict: try: torrent_data = bencodepy.decode(content) info = torrent_data[b"info"] @@ -63,6 +65,11 @@ def extract_torrent_metadata(content: bytes, is_parse_ptt: bool = True) -> dict: "episodes": parsed_data.get("episodes"), } ) + if not file_data: + logging.warning("No video files found in torrent. Skipping") + if is_raise_error: + raise ValueError("No video files found in torrent") + return {} announce_list = [ tracker[0].decode() for tracker in torrent_data.get(b"announce-list", []) @@ -72,6 +79,8 @@ def extract_torrent_metadata(content: bytes, is_parse_ptt: bool = True) -> dict: logging.warning( f"Torrent name contains 18+ keywords: {torrent_name}. Skipping" ) + if is_raise_error: + raise ValueError("Torrent name contains 18+ keywords") return {} largest_file = max(file_data, key=lambda x: x["size"]) @@ -93,6 +102,8 @@ def extract_torrent_metadata(content: bytes, is_parse_ptt: bool = True) -> dict: return metadata except Exception as e: logging.exception(f"Error occurred: {e}") + if is_raise_error: + raise ValueError(f"Failed to extract torrent metadata from torrent: {e}") return {}