diff --git a/media_manager/indexer/service.py b/media_manager/indexer/service.py index 467bd90..92dbc78 100644 --- a/media_manager/indexer/service.py +++ b/media_manager/indexer/service.py @@ -6,7 +6,9 @@ from media_manager.indexer.indexers.jackett import Jackett from media_manager.indexer.indexers.prowlarr import Prowlarr from media_manager.indexer.schemas import IndexerQueryResultId, IndexerQueryResult from media_manager.indexer.repository import IndexerRepository -from media_manager.notification.manager import notification_manager +from media_manager.movies.schemas import Movie +from media_manager.torrent.utils import remove_special_chars_and_parentheses +from media_manager.tv.schemas import Show log = logging.getLogger(__name__) @@ -30,13 +32,11 @@ class IndexerService: Search for results using the indexers based on a query. :param is_tv: Whether the search is for TV shows or movies. - :param query: The search query. - :param db: The database session. + :param query: The search query, is used as a fallback in case indexers don't support e.g. TMDB ID based search. :return: A list of search results. """ log.debug(f"Searching for: {query}") results = [] - failed_indexers = [] for indexer in self.indexers: try: @@ -46,25 +46,52 @@ class IndexerService: f"Indexer {indexer.__class__.__name__} returned {len(indexer_results)} results for query: {query}" ) except Exception as e: - failed_indexers.append(indexer.__class__.__name__) log.error( f"Indexer {indexer.__class__.__name__} failed for query '{query}': {e}" ) - # Send notification if indexers failed - if failed_indexers and notification_manager.is_configured(): - notification_manager.send_notification( - title="Indexer Failure", - message=f"The following indexers failed for query '{query}': {', '.join(failed_indexers)}. Check indexer configuration and connectivity.", - ) - - # Send notification if no results found from any indexer - if not results and notification_manager.is_configured(): - notification_manager.send_notification( - title="No Search Results", - message=f"No torrents found for query '{query}' from any configured indexer. Consider checking the search terms or indexer availability.", - ) - + for result in results: + self.repository.save_result(result=result) + + return results + + def search_movie(self, movie: Movie): + query = f"{movie.title} {movie.year}" + query = remove_special_chars_and_parentheses(query) + + results = [] + for indexer in self.indexers: + try: + indexer_results = indexer.search_movie(query=query, movie=movie) + if indexer_results: + results.extend(indexer_results) + except Exception as e: + log.error( + f"Indexer {indexer.__class__.__name__} failed for movie search '{query}': {e}" + ) + + for result in results: + self.repository.save_result(result=result) + + return results + + def search_season(self, show: Show, season_number: int): + query = f"{show.title} S{season_number:02d}" + query = remove_special_chars_and_parentheses(query) + + results = [] + for indexer in self.indexers: + try: + indexer_results = indexer.search_season( + query=query, show=show, season_number=season_number + ) + if indexer_results: + results.extend(indexer_results) + except Exception as e: + log.error( + f"Indexer {indexer.__class__.__name__} failed for season search '{query}': {e}" + ) + for result in results: self.repository.save_result(result=result)