From e4ec2529035bcedbb6abdadcffee8d9451774e70 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Wed, 19 May 2021 21:59:10 -0400 Subject: [PATCH] added tag.remove --- modules/builder.py | 40 ++++++++++++++++++++++++++----------- modules/meta.py | 50 +++++++++++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index ee82cdef..59f81217 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -706,16 +706,20 @@ class CollectionBuilder: elif method_name == "sync_mode": if str(method_data).lower() in ["append", "sync"]: self.details[method_name] = method_data.lower() else: raise Failed("Collection Error: sync_mode attribute must be either 'append' or 'sync'") - elif method_name in ["label", "label.sync"]: + elif method_name in ["label", "label.remove", "label.sync"]: 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_name == "label" and "label_sync_mode" in self.data and self.data["label_sync_mode"] == "sync": self.details["label.sync"] = util.get_list(method_data) else: self.details[method_name] = util.get_list(method_data) - elif method_name in ["item_label", "item_label.sync"]: + elif method_name in ["item_label", "item_label.remove", "item_label.sync"]: if "item_label" in self.data and "item_label.sync" in self.data: 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: + raise Failed(f"Collection Error: Cannot use item_label.remove and item_label.sync together") self.item_details[method_name] = util.get_list(method_data) elif method_name in plex.item_advance_keys: key, options = plex.item_advance_keys[method_name] @@ -1597,21 +1601,27 @@ class CollectionBuilder: self.library.collection_order_query(self.obj, self.details["collection_order"]) logger.info(f"Detail: collection_order updated Collection Order to {self.details['collection_order']}") - if "label" in self.details or "label.sync" in self.details: + if "label" in self.details or "label.remove" in self.details or "label.sync" in self.details: item_labels = [label.tag for label in self.obj.labels] - labels = util.get_list(self.details["label" if "label" in self.details else "label.sync"]) + labels = self.details["label" if "label" in self.details else "label.sync"] if "label.sync" in self.details: for label in (la for la in item_labels if la not in labels): self.library.query_data(self.obj.removeLabel, label) logger.info(f"Detail: Label {label} removed") - for label in (la for la in labels if la not in item_labels): - self.library.query_data(self.obj.addLabel, label) - logger.info(f"Detail: Label {label} added") + if "label" in self.details or "label.sync" in self.details: + for label in (la for la in labels if la not in item_labels): + self.library.query_data(self.obj.addLabel, label) + logger.info(f"Detail: Label {label} added") + if "label.remove" in self.details: + for label in self.details["label.remove"]: + if label in item_labels: + self.library.query_data(self.obj.removeLabel, label) + logger.info(f"Detail: Label {label} removed") if len(self.item_details) > 0: labels = None - if "item_label" in self.item_details or "item_label.sync" in self.item_details: - labels = util.get_list(self.item_details["item_label" if "item_label" in self.item_details else "item_label.sync"]) + if "item_label" in self.item_details or "item_label.remove" in self.item_details or "item_label.sync" in self.item_details: + labels = self.item_details["item_label" if "item_label" in self.item_details else "item_label.sync"] for item in self.library.get_collection_items(self.obj, self.smart_label_collection): if labels is not None: item_labels = [label.tag for label in item.labels] @@ -1619,9 +1629,15 @@ class CollectionBuilder: for label in (la for la in item_labels if la not in labels): self.library.query_data(item.removeLabel, label) logger.info(f"Detail: Label {label} removed from {item.title}") - for label in (la for la in labels if la not in item_labels): - self.library.query_data(item.addLabel, label) - logger.info(f"Detail: Label {label} added to {item.title}") + if "item_label" in self.item_details or "item_label.sync" in self.item_details: + for label in (la for la in labels if la not in item_labels): + self.library.query_data(item.addLabel, label) + logger.info(f"Detail: Label {label} added to {item.title}") + if "item_label.remove" in self.item_details: + for label in self.item_details["item_label.remove"]: + if label in item_labels: + self.library.query_data(self.obj.removeLabel, label) + logger.info(f"Detail: Label {label} removed from {item.title}") advance_edits = {} for method_name, method_data in self.item_details.items(): if method_name in plex.item_advance_keys: diff --git a/modules/meta.py b/modules/meta.py index 65a4970c..9efc9a09 100644 --- a/modules/meta.py +++ b/modules/meta.py @@ -121,30 +121,44 @@ class Metadata: def edit_tags(attr, obj, group, alias, key=None, extra=None, movie_library=False): if key is None: key = f"{attr}s" - if attr in alias and f"{attr}.sync" in alias: + if movie_library and not self.library.is_movie: + logger.error(f"Metadata Error: {attr} attribute only works for movie libraries") + elif attr in alias and f"{attr}.sync" in alias: logger.error(f"Metadata Error: Cannot use {attr} and {attr}.sync together") - elif attr in alias or f"{attr}.sync" in alias: + elif f"{attr}.remove" in alias and f"{attr}.sync" in alias: + logger.error(f"Metadata Error: Cannot use {attr}.remove and {attr}.sync together") + elif attr in alias and group[alias[attr]] is None: + logger.error(f"Metadata Error: {attr} attribute is blank") + elif f"{attr}.remove" in alias and group[alias[f"{attr}.remove"]] is None: + logger.error(f"Metadata Error: {attr}.remove attribute is blank") + elif f"{attr}.sync" in alias and group[alias[f"{attr}.sync"]] is None: + logger.error(f"Metadata Error: {attr}.sync attribute is blank") + elif attr in alias or f"{attr}.remove" in alias or f"{attr}.sync" in alias: attr_key = attr if attr in alias else f"{attr}.sync" - if movie_library and not self.library.is_movie: - logger.error(f"Metadata Error: {attr_key} attribute only works for movie libraries") - elif group[alias[attr_key]] or extra: - item_tags = [item_tag.tag for item_tag in getattr(obj, key)] - input_tags = [] - if group[alias[attr_key]]: - input_tags.extend(util.get_list(group[alias[attr_key]])) - if extra: - input_tags.extend(extra) - if f"{attr}.sync" in alias: - remove_method = getattr(obj, f"remove{attr.capitalize()}") - for tag in (t for t in item_tags if t not in input_tags): - updated = True - remove_method(tag) - logger.info(f"Detail: {attr.capitalize()} {tag} removed") + item_tags = [item_tag.tag for item_tag in getattr(obj, key)] + input_tags = [] + if group[alias[attr_key]]: + input_tags.extend(util.get_list(group[alias[attr_key]])) + if extra: + input_tags.extend(extra) + if f"{attr}.sync" in alias: + remove_method = getattr(obj, f"remove{attr.capitalize()}") + for tag in (t for t in item_tags if t not in input_tags): + updated = True + self.library.query_data(remove_method, tag) + logger.info(f"Detail: {attr.capitalize()} {tag} removed") + if attr in alias or f"{attr}.sync" in alias: add_method = getattr(obj, f"add{attr.capitalize()}") for tag in (t for t in input_tags if t not in item_tags): updated = True - add_method(tag) + self.library.query_data(add_method, tag) logger.info(f"Detail: {attr.capitalize()} {tag} added") + if f"{attr}.remove" in alias: + remove_method = getattr(obj, f"remove{attr.capitalize()}") + for tag in util.get_list(group[alias[f"{attr}.remove"]]): + if tag in item_tags: + self.library.query_data(remove_method, tag) + logger.info(f"Detail: {attr.capitalize()} {tag} removed") else: logger.error(f"Metadata Error: {attr} attribute is blank")