qBittorrent API

This commit is contained in:
2025-11-30 02:35:14 +01:00
parent ec9a2d4d36
commit 2098eee76e
4 changed files with 541 additions and 2 deletions
+77
View File
@@ -4,6 +4,7 @@ import logging
from ..api import tmdb_client, TMDBError, TMDBNotFoundError, TMDBAPIError, TMDBConfigurationError
from ..api.knaben import knaben_client, KnabenError, KnabenNotFoundError, KnabenAPIError
from ..api.qbittorrent import qbittorrent_client, QBittorrentError, QBittorrentAuthError, QBittorrentAPIError
logger = logging.getLogger(__name__)
@@ -90,6 +91,7 @@ def find_media_imdb_id(media_title: str) -> Dict[str, Any]:
"message": "An unexpected error occurred"
}
def find_torrent(media_title: str) -> Dict[str, Any]:
"""
Find torrents for a given media title using Knaben API.
@@ -166,3 +168,78 @@ def find_torrent(media_title: str) -> Dict[str, Any]:
"error": "internal_error",
"message": "An unexpected error occurred"
}
def add_torrent_to_qbittorrent(magnet_link: str) -> Dict[str, Any]:
"""
Add a torrent to qBittorrent using a magnet link.
This is a wrapper around the qBittorrent client that returns a standardized
dict format for compatibility with the agent's tool system.
Args:
magnet_link: Magnet link of the torrent to add
Returns:
Dict with success or error information:
- Success: {"status": "ok", "message": str}
- Error: {"error": str, "message": str}
Example:
>>> result = add_torrent_to_qbittorrent("magnet:?xt=urn:btih:...")
>>> print(result)
{'status': 'ok', 'message': 'Torrent added successfully'}
"""
try:
# Validate magnet link
if not magnet_link or not isinstance(magnet_link, str):
raise ValueError("Magnet link must be a non-empty string")
if not magnet_link.startswith("magnet:"):
raise ValueError("Invalid magnet link format")
logger.info("Adding torrent to qBittorrent")
# Add torrent to qBittorrent
success = qbittorrent_client.add_torrent(magnet_link)
if success:
logger.info("Torrent added successfully to qBittorrent")
return {
"status": "ok",
"message": "Torrent added successfully to qBittorrent"
}
else:
logger.warning("Failed to add torrent to qBittorrent")
return {
"error": "add_failed",
"message": "Failed to add torrent to qBittorrent"
}
except QBittorrentAuthError as e:
logger.error(f"qBittorrent authentication error: {e}")
return {
"error": "authentication_failed",
"message": "Failed to authenticate with qBittorrent"
}
except QBittorrentAPIError as e:
logger.error(f"qBittorrent API error: {e}")
return {
"error": "api_error",
"message": str(e)
}
except ValueError as e:
logger.error(f"Validation error: {e}")
return {
"error": "validation_failed",
"message": str(e)
}
except Exception as e:
logger.error(f"Unexpected error: {e}", exc_info=True)
return {
"error": "internal_error",
"message": "An unexpected error occurred"
}