From e64c56f4f21363a0576fa774045b1a79bc5d9a2d Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Sat, 28 May 2022 12:01:51 -0400 Subject: [PATCH] [3] fix deleting playlists --- VERSION | 2 +- modules/builder.py | 29 +++++++++++++++++------------ modules/plex.py | 11 +++++++++-- modules/util.py | 3 +++ plex_meta_manager.py | 4 +++- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/VERSION b/VERSION index 24b1d315..9917c521 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.17.0-develop2 +1.17.0-develop3 diff --git a/modules/builder.py b/modules/builder.py index dc1b676d..b8385df5 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -1,7 +1,7 @@ import os, re, time from datetime import datetime from modules import anidb, anilist, flixpatrol, icheckmovies, imdb, letterboxd, mal, plex, radarr, reciperr, sonarr, tautulli, tmdb, trakt, tvdb, mdblist, util -from modules.util import Failed, NotScheduled, NotScheduledRange, Overlay +from modules.util import Failed, NotScheduled, NotScheduledRange, Overlay, Deleted from plexapi.audio import Artist, Album, Track from plexapi.exceptions import BadRequest, NotFound from plexapi.video import Movie, Show, Season, Episode @@ -293,7 +293,7 @@ class CollectionBuilder: logger.debug(f"Value: {data[methods['delete_playlist']]}") if util.parse(self.Type, "delete_playlist", self.data, datatype="bool", methods=methods, default=False): self.obj = self.library.get_playlist(self.name) - logger.info(self.delete()) + raise Deleted(self.delete()) else: self.libraries.append(self.library) @@ -2576,18 +2576,23 @@ class CollectionBuilder: self.config.Trakt.sync_list(self.sync_to_trakt_list, current_ids) def delete(self): - output = "" + if self.playlist: + output = f"Deleting {self.Type} {self.obj.title}" + output += f"\n{self.Type} {'deleted' if self.obj else 'not found'} on {self.library.account.username}" + elif self.obj: + output = f"{self.Type} {self.obj.title} deleted" + else: + output = "" if self.obj: self.library.query(self.obj.delete) - output = f"{self.Type} {self.obj.title} deleted" - if self.playlist: - if self.valid_users: - for user in self.valid_users: - try: - self.library.delete_user_playlist(self.obj.title, user) - output += f"\nPlaylist {self.obj.title} deleted on User {user}" - except NotFound: - output += f"\nPlaylist {self.obj.title} not found on User {user}" + + if self.playlist and self.valid_users: + for user in self.valid_users: + try: + self.library.delete_user_playlist(self.obj.title, user) + output += f"\nPlaylist deleted on User {user}" + except NotFound: + output += f"\nPlaylist not found on User {user}" return output def sync_playlist(self): diff --git a/modules/plex.py b/modules/plex.py index eab02353..34190aa6 100644 --- a/modules/plex.py +++ b/modules/plex.py @@ -417,6 +417,7 @@ class Plex(Library): self.type = self.Plex.type.capitalize() self._users = [] self._all_items = [] + self._account = None self.agent = self.Plex.agent self.is_movie = self.type == "Movie" self.is_show = self.type == "Show" @@ -615,7 +616,7 @@ class Plex(Library): def users(self): if not self._users: users = [] - for user in self.PlexServer.myPlexAccount().users(): + for user in self.account.users(): if self.PlexServer.machineIdentifier in [s.machineIdentifier for s in user.servers]: users.append(user.title) self._users = users @@ -624,6 +625,12 @@ class Plex(Library): def delete_user_playlist(self, title, user): self.PlexServer.switchUser(user).playlist(title).delete() + @property + def account(self): + if self._account is None: + self._account = self.PlexServer.myPlexAccount() + return self._account + def playlist_report(self): playlists = {} def scan_user(server, username): @@ -634,7 +641,7 @@ class Plex(Library): playlists[playlist.title].append(username) except requests.exceptions.ConnectionError: pass - scan_user(self.PlexServer, self.PlexServer.myPlexAccount().title) + scan_user(self.PlexServer, self.account.title) for user in self.users: scan_user(self.PlexServer.switchUser(user), user) return playlists diff --git a/modules/util.py b/modules/util.py index 6f80de11..d85d114a 100644 --- a/modules/util.py +++ b/modules/util.py @@ -21,6 +21,9 @@ class TimeoutExpired(Exception): class Failed(Exception): pass +class Deleted(Exception): + pass + class NotScheduled(Exception): pass diff --git a/plex_meta_manager.py b/plex_meta_manager.py index 829ca5ac..7b7dbc1c 100644 --- a/plex_meta_manager.py +++ b/plex_meta_manager.py @@ -112,7 +112,7 @@ from modules import util util.logger = logger from modules.builder import CollectionBuilder from modules.config import ConfigFile -from modules.util import Failed, NotScheduled +from modules.util import Failed, NotScheduled, Deleted def my_except_hook(exctype, value, tb): for _line in traceback.format_exception(etype=exctype, value=value, tb=tb): @@ -816,6 +816,8 @@ def run_playlists(config): builder.send_notifications(playlist=True) + except Deleted as e: + logger.info(e) except NotScheduled as e: logger.info(e) if str(e).endswith("and was deleted"):