From f5fc9a8509831d3b751d5becec29c16297093223 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Fri, 2 Apr 2021 14:15:43 -0400 Subject: [PATCH] less radarr/sonarr calls --- modules/builder.py | 4 ++-- modules/radarr.py | 31 +++++++++++++++++++------------ modules/sonarr.py | 31 +++++++++++++++++++------------ 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index 3b44eccf..0c38bd76 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -1077,7 +1077,7 @@ class CollectionBuilder: if self.details["save_missing"] is True: self.library.add_missing(collection_name, missing_movies_with_names, True) if self.do_arr and self.library.Radarr: - self.library.Radarr.add_tmdb([missing_id for title, missing_id in missing_movies_with_names], tag=self.details["arr_tag"], folder=self.details["arr_folder"]) + self.library.Radarr.add_tmdb([missing_id for title, missing_id in missing_movies_with_names], tags=self.details["arr_tag"], folder=self.details["arr_folder"]) if self.run_again: self.missing_movies.extend([missing_id for title, missing_id in missing_movies_with_names]) if len(missing_shows) > 0 and self.library.is_show: @@ -1106,7 +1106,7 @@ class CollectionBuilder: if self.details["save_missing"] is True: self.library.add_missing(collection_name, missing_shows_with_names, False) if self.do_arr and self.library.Sonarr: - self.library.Sonarr.add_tvdb([missing_id for title, missing_id in missing_shows_with_names], tag=self.details["arr_tag"], folder=self.details["arr_folder"]) + self.library.Sonarr.add_tvdb([missing_id for title, missing_id in missing_shows_with_names], tags=self.details["arr_tag"], folder=self.details["arr_folder"]) if self.run_again: self.missing_shows.extend([missing_id for title, missing_id in missing_shows_with_names]) diff --git a/modules/radarr.py b/modules/radarr.py index 602d8fb4..0ae3fcb9 100644 --- a/modules/radarr.py +++ b/modules/radarr.py @@ -28,6 +28,7 @@ class RadarrAPI: self.quality_profile_id = profile["id"] if not self.quality_profile_id: raise Failed(f"Radarr Error: quality_profile: {params['quality_profile']} does not exist in radarr. Profiles available: {profiles}") + self.tags = self.get_tags() self.tmdb = tmdb self.url = params["url"] self.version = params["version"] @@ -37,22 +38,28 @@ class RadarrAPI: self.search = params["search"] self.tag = params["tag"] - def add_tmdb(self, tmdb_ids, tag=None, folder=None): + def get_tags(self): + return {tag["label"]: tag["id"] for tag in self.send_get("tag")} + + def add_tags(self, tags): + added = False + for label in tags: + if label not in self.tags: + added = True + self.send_post("tag", {"label": str(label)}) + if added: + self.tags = self.get_tags() + + def add_tmdb(self, tmdb_ids, tags=None, folder=None): logger.info("") logger.debug(f"TMDb IDs: {tmdb_ids}") tag_nums = [] add_count = 0 - if tag is None: - tag = self.tag - if tag: - tag_cache = {} - for label in tag: - self.send_post("tag", {"label": str(label)}) - for t in self.send_get("tag"): - tag_cache[t["label"]] = t["id"] - for label in tag: - if label in tag_cache: - tag_nums.append(tag_cache[label]) + if tags is None: + tags = self.tag + if tags: + self.add_tags(tags) + tag_nums = [self.tags[label] for label in tags if label in self.tags] for tmdb_id in tmdb_ids: try: movie = self.tmdb.get_movie(tmdb_id) diff --git a/modules/sonarr.py b/modules/sonarr.py index fd79d589..d1c92934 100644 --- a/modules/sonarr.py +++ b/modules/sonarr.py @@ -44,6 +44,7 @@ class SonarrAPI: if self.language_profile_id is None: self.language_profile_id = 1 + self.tags = self.get_tags() self.tvdb = tvdb self.language = language self.url = params["url"] @@ -55,22 +56,28 @@ class SonarrAPI: self.season_folder = params["season_folder"] self.tag = params["tag"] - def add_tvdb(self, tvdb_ids, tag=None, folder=None): + def get_tags(self): + return {tag["label"]: tag["id"] for tag in self.send_get("tag")} + + def add_tags(self, tags): + added = False + for label in tags: + if label not in self.tags: + added = True + self.send_post("tag", {"label": str(label)}) + if added: + self.tags = self.get_tags() + + def add_tvdb(self, tvdb_ids, tags=None, folder=None): logger.info("") logger.debug(f"TVDb IDs: {tvdb_ids}") tag_nums = [] add_count = 0 - if tag is None: - tag = self.tag - if tag: - tag_cache = {} - for label in tag: - self.send_post("tag", {"label": str(label)}) - for t in self.send_get("tag"): - tag_cache[t["label"]] = t["id"] - for label in tag: - if label in tag_cache: - tag_nums.append(tag_cache[label]) + if tags is None: + tags = self.tag + if tags: + self.add_tags(tags) + tag_nums = [self.tags[label] for label in tags if label in self.tags] for tvdb_id in tvdb_ids: try: show = self.tvdb.get_series(self.language, tvdb_id)