From d3eb57902ba98c517c4a4793d1feab7265cac040 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Mon, 6 Dec 2021 04:16:43 -0500 Subject: [PATCH] check paths when adding to radarr/sonarr --- modules/radarr.py | 48 +++++++++++++++++++++++++++++++++------------- modules/sonarr.py | 49 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/modules/radarr.py b/modules/radarr.py index 396b34dd..38e8f082 100644 --- a/modules/radarr.py +++ b/modules/radarr.py @@ -53,11 +53,20 @@ class Radarr: tags = options["tag"] if "tag" in options else self.tag search = options["search"] if "search" in options else self.search + arr_paths = {} + arr_ids = {} + for movie in self.api.all_movies(): + if movie.path: + arr_paths[movie.path] = movie.tmdbId + arr_ids[movie.tmdbId] = movie + added = [] exists = [] skipped = [] invalid = [] movies = [] + path_lookup = {} + mismatched = {} for i, item in enumerate(tmdb_ids, 1): path = item[1] if isinstance(item, tuple) else None tmdb_id = item[0] if isinstance(item, tuple) else item @@ -68,8 +77,18 @@ class Radarr: skipped.append(item) continue try: + if tmdb_id in arr_ids: + exists.append(arr_ids[tmdb_id]) + continue + if path in arr_paths: + mismatched[path] = tmdb_id + continue movie = self.api.get_movie(tmdb_id=tmdb_id) - movies.append((movie, path) if path else movie) + if path: + movies.append((movie, path)) + path_lookup[path] = tmdb_id + else: + movies.append(movie) except ArrException: invalid.append(item) if len(movies) == 100 or len(tmdb_ids) == i: @@ -91,26 +110,29 @@ class Radarr: self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name) logger.info(f"{len(added)} Movie{'s' if len(added) > 1 else ''} added to Radarr") - if len(exists) > 0: + if len(exists) > 0 or len(skipped) > 0: logger.info("") - for movie in exists: - logger.info(f"Already in Radarr | {movie.tmdbId:<6} | {movie.title}") - if self.config.Cache: - self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name) - logger.info(f"{len(exists)} Movie{'s' if len(exists) > 1 else ''} already existing in Radarr") + if len(exists) > 0: + for movie in exists: + logger.info(f"Already in Radarr | {movie.tmdbId:<6} | {movie.title}") + if self.config.Cache: + self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name) + if len(skipped) > 0: + for movie in skipped: + logger.info(f"Skipped: In Cache | {movie}") + logger.info(f"{len(exists) + len(skipped)} Movie{'s' if len(skipped) > 1 else ''} already exist in Radarr") - if len(skipped) > 0: + if len(mismatched) > 0: logger.info("") - for movie in skipped: - logger.info(f"Skipped: In Cache | {movie}") - if self.config.Cache: - self.config.Cache.update_radarr_adds(movie[0] if isinstance(movie, tuple) else movie, self.library.original_mapping_name) - logger.info(f"{len(skipped)} Movie{'s' if len(skipped) > 1 else ''} already existing in Radarr") + for path, tmdb_id in mismatched.items(): + logger.info(f"Plex TMDb ID: {tmdb_id:<7} | Radarr TMDb ID: {arr_paths[path]:<7} | Path: {path}") + logger.info(f"{len(mismatched)} Movie{'s' if len(mismatched) > 1 else ''} with mismatched TMDb IDs") if len(invalid) > 0: logger.info("") for tmdb_id in invalid: logger.info(f"Invalid TMDb ID | {tmdb_id}") + logger.info(f"{len(invalid)} Movie{'s' if len(invalid) > 1 else ''} with Invalid IDs") return len(added) diff --git a/modules/sonarr.py b/modules/sonarr.py index 4303b103..dc13ecee 100644 --- a/modules/sonarr.py +++ b/modules/sonarr.py @@ -79,11 +79,20 @@ class Sonarr: search = options["search"] if "search" in options else self.search cutoff_search = options["cutoff_search"] if "cutoff_search" in options else self.cutoff_search + arr_paths = {} + arr_ids = {} + for series in self.api.all_series(): + if series.path: + arr_paths[series.path] = series.tvdbId + arr_paths[series.tvdbId] = series + added = [] exists = [] skipped = [] invalid = [] shows = [] + path_lookup = {} + mismatched = {} for i, item in enumerate(tvdb_ids, 1): path = item[1] if isinstance(item, tuple) else None tvdb_id = item[0] if isinstance(item, tuple) else item @@ -94,8 +103,18 @@ class Sonarr: skipped.append(item) continue try: + if tvdb_id in arr_ids: + exists.append(arr_ids[tvdb_id]) + continue + if path in arr_paths: + mismatched[path] = tvdb_id + continue show = self.api.get_series(tvdb_id=tvdb_id) - shows.append((show, path) if path else show) + if path: + shows.append((show, path)) + path_lookup[path] = tvdb_id + else: + shows.append(show) except ArrException: invalid.append(item) if len(shows) == 100 or len(tvdb_ids) == i: @@ -117,26 +136,30 @@ class Sonarr: self.config.Cache.update_sonarr_adds(series.tvdbId, self.library.original_mapping_name) logger.info(f"{len(added)} Series added to Sonarr") - if len(exists) > 0: + if len(exists) > 0 or len(skipped) > 0: logger.info("") - for series in exists: - logger.info(f"Already in Sonarr | {series.tvdbId:<6} | {series.title}") - if self.config.Cache: - self.config.Cache.update_sonarr_adds(series.tvdbId, self.library.original_mapping_name) - logger.info(f"{len(exists)} Series already existing in Sonarr") + if len(exists) > 0: + for series in exists: + logger.info(f"Already in Sonarr | {series.tvdbId:<6} | {series.title}") + if self.config.Cache: + self.config.Cache.update_sonarr_adds(series.tvdbId, self.library.original_mapping_name) + if len(skipped) > 0: + logger.info("") + for series in skipped: + logger.info(f"Skipped: In Cache | {series}") + logger.info(f"{len(exists) + len(skipped)} Series already exist in Sonarr") - if len(skipped) > 0: + if len(mismatched) > 0: logger.info("") - for series in skipped: - logger.info(f"Skipped: In Cache | {series}") - if self.config.Cache: - self.config.Cache.update_sonarr_adds(series[0] if isinstance(series, tuple) else series, self.library.original_mapping_name) - logger.info(f"{len(skipped)} Movie{'s' if len(skipped) > 1 else ''} already existing in Sonarr") + for path, tmdb_id in mismatched.items(): + logger.info(f"Plex TVDb ID: {tmdb_id:<7} | Sonarr TVDb ID: {arr_paths[path]:<7} | Path: {path}") + logger.info(f"{len(mismatched)} Series with mismatched TVDb IDs") if len(invalid) > 0: for tvdb_id in invalid: logger.info("") logger.info(f"Invalid TVDb ID | {tvdb_id}") + logger.info(f"{len(invalid)} Series with Invalid IDs") return len(added)