From 89e0894772494b2d380b0b943be473568c414474 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Sat, 3 Jul 2021 00:18:09 -0400 Subject: [PATCH] #290 added item_radarr_tag and item_sonarr_tag --- modules/builder.py | 43 ++++++++++++++++++++++++++++++++----------- modules/radarr.py | 30 +++++++++++++++++++++++++++--- modules/sonarr.py | 29 ++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index 97d7d3b1..0ac02851 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -611,26 +611,35 @@ class CollectionBuilder: if os.path.exists(method_data): self.backgrounds[method_name] = os.path.abspath(method_data) else: raise Failed(f"Collection Error: Background Path Does Not Exist: {os.path.abspath(method_data)}") elif method_name == "label": - if "label" in self.data and "label.sync" in self.data: - raise Failed(f"Collection Error: Cannot use label and label.sync together") - if "label.remove" in self.data and "label.sync" in self.data: - raise Failed(f"Collection Error: Cannot use label.remove and label.sync together") - if method_final == "label" and "label_sync_mode" in self.data and self.data["label_sync_mode"] == "sync": + if "label" in methods and "label.sync" in methods: + raise Failed("Collection Error: Cannot use label and label.sync together") + if "label.remove" in methods and "label.sync" in methods: + raise Failed("Collection Error: Cannot use label.remove and label.sync together") + if method_final == "label" and "label_sync_mode" in methods and self.data[methods["label_sync_mode"]] == "sync": self.details["label.sync"] = util.get_list(method_data) else: self.details[method_final] = util.get_list(method_data) elif method_name == "item_label": - if "item_label" in self.data and "item_label.sync" in self.data: + if "item_label" in methods and "item_label.sync" in methods: raise Failed(f"Collection Error: Cannot use item_label and item_label.sync together") - if "item_label.remove" in self.data and "item_label.sync" in self.data: + if "item_label.remove" in methods and "item_label.sync" in methods: raise Failed(f"Collection Error: Cannot use item_label.remove and item_label.sync together") self.item_details[method_final] = util.get_list(method_data) + elif method_name in ["item_radarr_tag", "item_sonarr_tag"]: + if method_name in methods and f"{method_name}.sync" in methods: + raise Failed(f"Collection Error: Cannot use {method_name} and {method_name}.sync together") + if f"{method_name}.remove" in methods and f"{method_name}.sync" in methods: + raise Failed(f"Collection Error: Cannot use {method_name}.remove and {method_name}.sync together") + if method_name in methods and f"{method_name}.remove" in methods: + raise Failed(f"Collection Error: Cannot use {method_name} and {method_name}.remove together") + self.item_details[method_name] = util.get_list(method_data) + self.item_details["apply_tags"] = method_mod[1:] if method_mod else "" elif method_name == "item_overlay": overlay = os.path.join(config.default_dir, "overlays", method_data, "overlay.png") if not os.path.exists(overlay): raise Failed(f"Collection Error: {method_data} overlay image not found at {overlay}") if method_data in self.library.overlays: - raise Failed(f"Each Overlay can only be used once per Library") + raise Failed("Each Overlay can only be used once per Library") self.library.overlays.append(method_data) self.item_details[method_name] = method_data elif method_name in plex.item_advance_keys: @@ -1357,10 +1366,10 @@ class CollectionBuilder: attribute = method_alias[attribute] if attribute in method_alias else attribute modifier = modifier_alias[modifier] if modifier in modifier_alias else modifier - if attribute.lower() == "add_to_arr": + if attribute == "add_to_arr": attribute = "radarr_add" if self.library.is_movie else "sonarr_add" - elif attribute.lower() in ["arr_tag", "arr_folder"]: - attribute = f"{'rad' if self.library.is_movie else 'son'}{attribute.lower()}" + elif attribute in ["arr_tag", "arr_folder"]: + attribute = f"{'rad' if self.library.is_movie else 'son'}{attribute}" elif attribute in plex.date_attributes and modifier in [".gt", ".gte"]: modifier = ".after" elif attribute in plex.date_attributes and modifier in [".lt", ".lte"]: @@ -1682,12 +1691,18 @@ class CollectionBuilder: temp_image = os.path.join(overlay_folder, f"temp.png") overlay = (overlay_name, overlay_folder, overlay_image, temp_image) + tmdb_ids = [] + tvdb_ids = [] for item in items: if int(item.ratingKey) in rating_keys: rating_keys.remove(int(item.ratingKey)) if self.details["item_assets"] or overlay is not None: self.library.update_item_from_assets(item, overlay=overlay) self.library.edit_tags("label", item, add_tags=add_tags, remove_tags=remove_tags, sync_tags=sync_tags) + if "item_radarr_tag" in self.item_details and item.ratingKey in self.library.movie_rating_key_map: + tmdb_ids.append(self.library.movie_rating_key_map[item.ratingKey]) + if "item_sonarr_tag" in self.item_details and item.ratingKey in self.library.show_rating_key_map: + tvdb_ids.append(self.library.show_rating_key_map[item.ratingKey]) advance_edits = {} for method_name, method_data in self.item_details.items(): if method_name in plex.item_advance_keys: @@ -1696,6 +1711,12 @@ class CollectionBuilder: advance_edits[key] = options[method_data] self.library.edit_item(item, item.title, "Movie" if self.library.is_movie else "Show", advance_edits, advanced=True) + if len(tmdb_ids) > 0: + self.library.Radarr.edit_tags(tmdb_ids, self.item_details["item_radarr_tag"], self.item_details["apply_tags"]) + + if len(tvdb_ids) > 0: + self.library.Sonarr.edit_tags(tvdb_ids, self.item_details["item_sonarr_tag"], self.item_details["apply_tags"]) + for rating_key in rating_keys: try: item = self.fetch_item(rating_key) diff --git a/modules/radarr.py b/modules/radarr.py index 347f1b75..3dd50dfc 100644 --- a/modules/radarr.py +++ b/modules/radarr.py @@ -12,6 +12,11 @@ availability_translation = { "released": "released", "db": "preDB" } +apply_tags_translation = { + "": "add", + "sync": "replace", + "remove": "remove" +} class Radarr: def __init__(self, params): @@ -31,7 +36,7 @@ class Radarr: def add_tmdb(self, tmdb_ids, **options): logger.info("") - util.separator(f"Adding to Radarr", space=False, border=False) + util.separator("Adding to Radarr", space=False, border=False) logger.debug("") logger.debug(f"TMDb IDs: {tmdb_ids}") folder = options["folder"] if "folder" in options else self.root_folder_path @@ -57,6 +62,25 @@ class Radarr: logger.info(f"Already in Radarr | {movie.tmdbId:<6} | {movie.title}") logger.info(f"{len(exists)} Movie{'s' if len(exists) > 1 else ''} already existing in Radarr") - for movie in invalid: + if len(invalid) > 0: logger.info("") - logger.info(f"Invalid TMDb ID | {movie}") + for tmdb_id in invalid: + logger.info(f"Invalid TMDb ID | {tmdb_id}") + + def edit_tags(self, tmdb_ids, tags, apply_tags): + logger.info("") + logger.info(f"{apply_tags_translation[apply_tags].capitalize()} Radarr Tags: {tags}") + + edited, not_exists = self.api.edit_multiple_movies(tmdb_ids, tags=tags, apply_tags=apply_tags) + + if len(edited) > 0: + logger.info("") + for movie in edited: + logger.info(f"Radarr Tags | {movie.title:<25} | {movie.tags}") + logger.info(f"{len(edited)} Movie{'s' if len(edited) > 1 else ''} edited in Radarr") + + if len(not_exists) > 0: + logger.info("") + for tmdb_id in not_exists: + logger.info(f"TMDb ID Not in Radarr | {tmdb_id}") + diff --git a/modules/sonarr.py b/modules/sonarr.py index 65ab0be8..80bbb65e 100644 --- a/modules/sonarr.py +++ b/modules/sonarr.py @@ -17,6 +17,11 @@ monitor_translation = { "latest": "latestSeason", "none": "none" } +apply_tags_translation = { + "": "add", + "sync": "replace", + "remove": "remove" +} class Sonarr: def __init__(self, params): @@ -40,7 +45,7 @@ class Sonarr: def add_tvdb(self, tvdb_ids, **options): logger.info("") - util.separator(f"Adding to Sonarr", space=False, border=False) + util.separator("Adding to Sonarr", space=False, border=False) logger.debug("") logger.debug(f"TVDb IDs: {tvdb_ids}") folder = options["folder"] if "folder" in options else self.root_folder_path @@ -70,6 +75,24 @@ class Sonarr: logger.info(f"Already in Sonarr | {series.tvdbId:<6} | {series.title}") logger.info(f"{len(exists)} Series already existing in Sonarr") - for series in invalid: + if len(invalid) > 0: + for tvdb_id in invalid: + logger.info("") + logger.info(f"Invalid TVDb ID | {tvdb_id}") + + def edit_tags(self, tvdb_ids, tags, apply_tags): + logger.info("") + logger.info(f"{apply_tags_translation[apply_tags].capitalize()} Sonarr Tags: {tags}") + + edited, not_exists = self.api.edit_multiple_series(tvdb_ids, tags=tags, apply_tags=apply_tags) + + if len(edited) > 0: + logger.info("") + for series in edited: + logger.info(f"Radarr Tags | {series.title:<25} | {series.tags}") + logger.info(f"{len(edited)} Series edited in Sonarr") + + if len(not_exists) > 0: logger.info("") - logger.info(f"Invalid TVDb ID | {series}") + for tvdb_id in not_exists: + logger.info(f"TVDb ID Not in Sonarr | {tvdb_id}")