Knaben API

This commit is contained in:
2025-11-30 02:17:19 +01:00
parent 2eed654f89
commit ec9a2d4d36
5 changed files with 357 additions and 15 deletions
+87 -9
View File
@@ -3,6 +3,7 @@ from typing import Dict, Any
import logging
from ..api import tmdb_client, TMDBError, TMDBNotFoundError, TMDBAPIError, TMDBConfigurationError
from ..api.knaben import knaben_client, KnabenError, KnabenNotFoundError, KnabenAPIError
logger = logging.getLogger(__name__)
@@ -10,18 +11,18 @@ logger = logging.getLogger(__name__)
def find_media_imdb_id(media_title: str) -> Dict[str, Any]:
"""
Find the IMDb ID for a given media title using TMDB API.
This is a wrapper around the TMDB client that returns a standardized
dict format for compatibility with the agent's tool system.
Args:
media_title: Title of the media to search for
Returns:
Dict with IMDb ID or error information:
- Success: {"status": "ok", "imdb_id": str, "title": str, ...}
- Error: {"error": str, "message": str}
Example:
>>> result = find_media_imdb_id("Inception")
>>> print(result)
@@ -30,7 +31,7 @@ def find_media_imdb_id(media_title: str) -> Dict[str, Any]:
try:
# Use the TMDB client to search for media
result = tmdb_client.search_media(media_title)
# Check if IMDb ID was found
if result.imdb_id:
logger.info(f"IMDb ID found for '{media_title}': {result.imdb_id}")
@@ -53,35 +54,112 @@ def find_media_imdb_id(media_title: str) -> Dict[str, Any]:
"media_type": result.media_type,
"tmdb_id": result.tmdb_id
}
except TMDBNotFoundError as e:
logger.info(f"Media not found: {e}")
return {
"error": "not_found",
"message": str(e)
}
except TMDBConfigurationError as e:
logger.error(f"TMDB configuration error: {e}")
return {
"error": "configuration_error",
"message": str(e)
}
except TMDBAPIError as e:
logger.error(f"TMDB 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"
}
def find_torrent(media_title: str) -> Dict[str, Any]:
"""
Find torrents for a given media title using Knaben API.
This is a wrapper around the Knaben client that returns a standardized
dict format for compatibility with the agent's tool system.
Args:
media_title: Title of the media to search for
Returns:
Dict with torrent information or error details:
- Success: {"status": "ok", "torrents": List[Dict[str, Any]]}
- Error: {"error": str, "message": str}
"""
try:
# Search for torrents
results = knaben_client.search(media_title, limit=10)
if not results:
logger.info(f"No torrents found for '{media_title}'")
return {
"error": "not_found",
"message": f"No torrents found for '{media_title}'"
}
# Convert to dict format
torrents = []
for torrent in results:
torrents.append({
"name": torrent.title,
"size": torrent.size,
"seeders": torrent.seeders,
"leechers": torrent.leechers,
"magnet": torrent.magnet,
"info_hash": torrent.info_hash,
"tracker": torrent.tracker,
"upload_date": torrent.upload_date,
"category": torrent.category
})
logger.info(f"Found {len(torrents)} torrents for '{media_title}'")
return {
"status": "ok",
"torrents": torrents,
"count": len(torrents)
}
except KnabenNotFoundError as e:
logger.info(f"Torrents not found: {e}")
return {
"error": "not_found",
"message": str(e)
}
except KnabenAPIError as e:
logger.error(f"Knaben 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 {