From bfd9196f5e87bace56717cdb7e9c0260ed180553 Mon Sep 17 00:00:00 2001 From: maxid Date: Sat, 20 Dec 2025 13:08:05 +0100 Subject: [PATCH] add support for handling invalid URL schemas and following redirects for torrent downloads --- media_manager/torrent/utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/media_manager/torrent/utils.py b/media_manager/torrent/utils.py index d14bb70..1156903 100644 --- a/media_manager/torrent/utils.py +++ b/media_manager/torrent/utils.py @@ -2,6 +2,7 @@ import hashlib import logging import mimetypes import re +from asyncio import timeout from pathlib import Path, UnsupportedOperation import shutil @@ -9,8 +10,11 @@ import bencoder import patoolib import requests import libtorrent +from requests.exceptions import InvalidSchema + from media_manager.config import AllEncompassingConfig from media_manager.indexer.schemas import IndexerQueryResult +from media_manager.indexer.utils import follow_redirects_to_final_torrent_url from media_manager.torrent.schemas import Torrent log = logging.getLogger(__name__) @@ -141,6 +145,15 @@ def get_torrent_hash(torrent: IndexerQueryResult) -> str: response = requests.get(str(torrent.download_url), timeout=30) response.raise_for_status() torrent_content = response.content + except InvalidSchema as e: + log.debug(f"Invalid schema for URL {torrent.download_url}: {e}") + final_url = follow_redirects_to_final_torrent_url( + initial_url=torrent.download_url, + session=requests.Session(), + timeout=AllEncompassingConfig().indexers.prowlarr.timeout_seconds, + ) + torrent_hash = str(libtorrent.parse_magnet_uri(final_url).info_hash) + return torrent_hash except Exception as e: log.error(f"Failed to download torrent file: {e}") raise