From 856b96f819800c1806a915e12dad036cf2d66a03 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Fri, 17 Dec 2021 18:18:34 -0500 Subject: [PATCH] fixed playlist removal and various other bugs --- modules/builder.py | 21 +++++++++++---------- modules/config.py | 27 ++++++++++++++------------- modules/util.py | 3 +++ modules/webhooks.py | 16 ++++++---------- plex_meta_manager.py | 2 +- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/modules/builder.py b/modules/builder.py index 662097aa..c8bcd656 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -524,7 +524,7 @@ class CollectionBuilder: raise Failed(f"{self.Type} Error: sort_by attribute is blank") else: logger.debug(f"Value: {self.data[methods['sort_by']]}") - if (self.library.is_movie and self.data[methods["sort_by"]] not in plex.movie_sorts) or (self.library.is_show and self.data[methods["sort_by"]] in plex.show_sorts): + if (self.library.is_movie and self.data[methods["sort_by"]] not in plex.movie_sorts) or (self.library.is_show and self.data[methods["sort_by"]] not in plex.show_sorts): raise Failed(f"{self.Type} Error: sort_by attribute {self.data[methods['sort_by']]} invalid") else: self.sort_by = self.data[methods["sort_by"]] @@ -1747,13 +1747,13 @@ class CollectionBuilder: self.library.alter_collection(item, name, smart_label_collection=self.smart_label_collection) amount_added += 1 if self.details["collection_changes_webhooks"]: - if self.library.is_movie and item.ratingKey in self.library.movie_rating_key_map: + if item.ratingKey in self.library.movie_rating_key_map: add_id = self.library.movie_rating_key_map[item.ratingKey] - elif self.library.is_show and item.ratingKey in self.library.show_rating_key_map: + elif item.ratingKey in self.library.show_rating_key_map: add_id = self.library.show_rating_key_map[item.ratingKey] else: add_id = None - self.notification_additions.append({"title": item.title, "id": add_id}) + self.notification_additions.append(util.item_set(item, add_id)) if self.playlist and playlist_adds and not self.obj: self.obj = self.library.create_playlist(self.name, playlist_adds) logger.info("") @@ -1782,14 +1782,15 @@ class CollectionBuilder: self.library.alter_collection(item, self.name, smart_label_collection=self.smart_label_collection, add=False) amount_removed += 1 if self.details["collection_changes_webhooks"]: - if self.library.is_movie and item.ratingKey in self.library.movie_rating_key_map: + if item.ratingKey in self.library.movie_rating_key_map: remove_id = self.library.movie_rating_key_map[item.ratingKey] - elif self.library.is_show and item.ratingKey in self.library.show_rating_key_map: + elif item.ratingKey in self.library.show_rating_key_map: remove_id = self.library.show_rating_key_map[item.ratingKey] else: remove_id = None - self.notification_removals.append({"title": item.title, "id": remove_id}) + self.notification_removals.append(util.item_set(item, remove_id)) if self.playlist and playlist_removes: + self.obj.reload() self.obj.removeItems(playlist_removes) if amount_removed > 0: logger.info("") @@ -2315,8 +2316,8 @@ class CollectionBuilder: self.library.Webhooks.collection_hooks( self.details["collection_changes_webhooks"], self.obj, - poster_url=self.collection_poster.location if self.collection_poster.is_url else None, - background_url=self.collection_background.location if self.collection_background.is_url else None, + poster_url=self.collection_poster.location if self.collection_poster and self.collection_poster.is_url else None, + background_url=self.collection_background.location if self.collection_background and self.collection_background.is_url else None, created=self.created, deleted=self.deleted, additions=self.notification_additions, @@ -2358,7 +2359,7 @@ class CollectionBuilder: add_id = self.library.show_rating_key_map[current.ratingKey] else: add_id = None - self.notification_additions.append({"title": current.title, "id": add_id}) + self.notification_additions.append(util.item_set(current, add_id)) self.send_notifications() logger.info(f"{len(rating_keys)} {self.collection_level.capitalize()}{'s' if len(rating_keys) > 1 else ''} Processed") diff --git a/modules/config.py b/modules/config.py index 45d848de..d0753084 100644 --- a/modules/config.py +++ b/modules/config.py @@ -99,16 +99,17 @@ class ConfigFile: if "settings" in new_config: new_config["settings"] = new_config.pop("settings") if "webhooks" in new_config: temp = new_config.pop("webhooks") - changes = [] - def hooks(attr): - if attr in temp: - items = util.get_list(temp.pop(attr), split=False) - if items: - changes.extend([w for w in items if w not in changes]) - hooks("collection_creation") - hooks("collection_addition") - hooks("collection_removal") - temp["collection_changes"] = changes if changes else None + if "collection_changes" not in temp: + changes = [] + def hooks(attr): + if attr in temp: + items = util.get_list(temp.pop(attr), split=False) + if items: + changes.extend([w for w in items if w not in changes]) + hooks("collection_creation") + hooks("collection_addition") + hooks("collection_removal") + temp["collection_changes"] = changes if changes else None new_config["webhooks"] = temp if "plex" in new_config: new_config["plex"] = new_config.pop("plex") if "tmdb" in new_config: new_config["tmdb"] = new_config.pop("tmdb") @@ -147,9 +148,9 @@ class ConfigFile: elif attribute not in loaded_config[parent]: loaded_config[parent][attribute] = default else: endline = "" yaml.round_trip_dump(loaded_config, open(self.config_path, "w"), indent=None, block_seq_indent=2) - if default_is_none and var_type in ["list", "int_list"]: return [] + if default_is_none and var_type in ["list", "int_list"]: return default if default else [] elif data[attribute] is None: - if default_is_none and var_type in ["list", "int_list"]: return [] + if default_is_none and var_type in ["list", "int_list"]: return default if default else [] elif default_is_none: return None else: message = f"{text} is blank" elif var_type == "url": @@ -512,7 +513,7 @@ class ConfigFile: params["ignore_imdb_ids"] = check_for_attribute(lib, "ignore_imdb_ids", parent="settings", var_type="list", default_is_none=True, do_print=False, save=False) params["ignore_imdb_ids"].extend([i for i in self.general["ignore_imdb_ids"] if i not in params["ignore_imdb_ids"]]) params["error_webhooks"] = check_for_attribute(lib, "error", parent="webhooks", var_type="list", default=self.webhooks["error"], do_print=False, save=False, default_is_none=True) - params["collection_changes_webhooks"] = check_for_attribute(lib, "collection_creation", parent="webhooks", var_type="list", default=self.webhooks["collection_changes"], do_print=False, save=False, default_is_none=True) + params["collection_changes_webhooks"] = check_for_attribute(lib, "collection_changes", parent="webhooks", var_type="list", default=self.webhooks["collection_changes"], do_print=False, save=False, default_is_none=True) params["assets_for_all"] = check_for_attribute(lib, "assets_for_all", parent="settings", var_type="bool", default=self.general["assets_for_all"], do_print=False, save=False) params["mass_genre_update"] = check_for_attribute(lib, "mass_genre_update", test_list=mass_update_options, default_is_none=True, save=False, do_print=False) params["mass_audience_rating_update"] = check_for_attribute(lib, "mass_audience_rating_update", test_list=mass_update_options, default_is_none=True, save=False, do_print=False) diff --git a/modules/util.py b/modules/util.py index 701e0c7e..67a30922 100644 --- a/modules/util.py +++ b/modules/util.py @@ -259,6 +259,9 @@ def item_title(item): else: return item.title +def item_set(item, item_id): + return {"title": item_title(item), "tmdb" if isinstance(item, Movie) else "tvdb": item_id} + def is_locked(filepath): locked = None file_object = None diff --git a/modules/webhooks.py b/modules/webhooks.py index f2996112..553a529a 100644 --- a/modules/webhooks.py +++ b/modules/webhooks.py @@ -79,22 +79,18 @@ class Webhooks: if not poster_url and collection.thumb and next((f for f in collection.fields if f.name == "thumb"), None): thumb = self.config.get_image_encoded(f"{self.library.url}{collection.thumb}?X-Plex-Token={self.library.token}") art = None - if not background_url and collection.art and next((f for f in collection.fields if f.name == "art"), None): + if not playlist and not background_url and collection.art and next((f for f in collection.fields if f.name == "art"), None): art = self.config.get_image_encoded(f"{self.library.url}{collection.art}?X-Plex-Token={self.library.token}") - json = { + self._request(webhooks, { "server_name": self.library.PlexServer.friendlyName, "library_name": self.library.name, - "type": "movie" if self.library.is_movie else "show", "playlist" if playlist else "collection": collection.title, "created": created, "deleted": deleted, "poster": thumb, "background": art, "poster_url": poster_url, - "background_url": background_url - } - if additions: - json["additions"] = additions - if removals: - json["removals"] = removals - self._request(webhooks, json) + "background_url": background_url, + "additions": additions if additions else [], + "removals": removals if removals else [], + }) diff --git a/plex_meta_manager.py b/plex_meta_manager.py index 7be8b0c9..6b62da7b 100644 --- a/plex_meta_manager.py +++ b/plex_meta_manager.py @@ -545,7 +545,7 @@ def update_libraries(config): if builder.custom_sort: builder.sort_collection() - builder.send_notifications() + builder.send_notifications(playlist=True)