Small playlist refactor. Fix for #1877, #1880 and #1882

pull/1885/head
Benoit Boulanger 10 months ago
parent 6c6da10c9d
commit 5eb5acb226

@ -530,7 +530,10 @@ class CollectionBuilder:
self.exclude_users = config.general["playlist_exclude_users"] self.exclude_users = config.general["playlist_exclude_users"]
logger.warning(f"Playlist Warning: exclude_users attribute not found defaulting to playlist_exclude_users: {self.exclude_users}") logger.warning(f"Playlist Warning: exclude_users attribute not found defaulting to playlist_exclude_users: {self.exclude_users}")
plex_users = self.library.users plex_admin = self.library.account.username
_plex_users = self.library.users
plex_users = _plex_users.copy()
plex_users.append(plex_admin)
self.exclude_users = util.get_list(self.exclude_users) if self.exclude_users else [] self.exclude_users = util.get_list(self.exclude_users) if self.exclude_users else []
for user in self.exclude_users: for user in self.exclude_users:
@ -553,10 +556,15 @@ class CollectionBuilder:
logger.debug("Validating Method: delete_playlist") logger.debug("Validating Method: delete_playlist")
logger.debug(f"Value: {data[methods['delete_playlist']]}") logger.debug(f"Value: {data[methods['delete_playlist']]}")
if util.parse(self.Type, "delete_playlist", self.data, datatype="bool", methods=methods, default=False): if util.parse(self.Type, "delete_playlist", self.data, datatype="bool", methods=methods, default=False):
try: playlist_getters = [self.library.get_playlist, self.library.get_playlist_from_users]
self.obj = self.library.get_playlist(self.name) for getter in playlist_getters:
except Failed as e: try:
logger.error(e) self.obj = getter(self.name)
break
except Failed as e:
error = e
else:
logger.error(error)
raise Deleted(self.delete()) raise Deleted(self.delete())
else: else:
self.libraries.append(self.library) self.libraries.append(self.library)
@ -3526,7 +3534,6 @@ class CollectionBuilder:
title = self.obj.title if self.obj else self.name title = self.obj.title if self.obj else self.name
if self.playlist: if self.playlist:
output = f"Deleting {self.Type} {title}" output = f"Deleting {self.Type} {title}"
output += f"\n{self.Type} {'deleted' if self.obj else 'not found'} on {self.library.account.username}"
elif self.obj: elif self.obj:
output = f"{self.Type} {self.obj.title} deleted" output = f"{self.Type} {self.obj.title} deleted"
if self.smart_label_collection: if self.smart_label_collection:
@ -3534,16 +3541,24 @@ class CollectionBuilder:
self.library.edit_tags("label", item, remove_tags=self.name) self.library.edit_tags("label", item, remove_tags=self.name)
else: else:
output = "" output = ""
if self.obj:
self.library.delete(self.obj)
if self.playlist and self.valid_users: if self.playlist:
for user in self.valid_users: for user in self.valid_users:
try: if user == self.library.account.username:
self.library.delete_user_playlist(title, user) try:
output += f"\nPlaylist deleted on User {user}" _ = self.library.get_playlist(title) # Verify if this playlist exists in Admin to avoid log confusion
except Failed: self.library.delete(self.obj)
output += f"\nPlaylist not found on User {user}" output += f"\nPlaylist deleted on {user}"
except Failed:
output += f"\nPlaylist not found on {user}"
else:
try:
self.library.delete_user_playlist(title, user)
output += f"\nPlaylist deleted on User {user}"
except Failed:
output += f"\nPlaylist not found on User {user}"
elif self.obj:
self.library.delete(self.obj)
return output return output
def sync_playlist(self): def sync_playlist(self):
@ -3556,8 +3571,21 @@ class CollectionBuilder:
self.library.delete_user_playlist(self.obj.title, user) self.library.delete_user_playlist(self.obj.title, user)
except Failed: except Failed:
pass pass
self.obj.copyToUser(user) if user != self.library.account.username:
logger.info(f"Playlist: {self.name} synced to {user}") new_playlist = self.obj.copyToUser(user)
new_playlist = new_playlist.editSummary(summary=self.summaries["summary"]).reload()
logger.info(f"Playlist: {self.name} synced to {user}")
def exclude_admin_from_playlist(self):
if self.obj and (self.exclude_users is not None and self.library.account.username in self.exclude_users):
logger.info("")
logger.separator(f"Excluding Admin from Playlist", space=False, border=False)
logger.info("")
try:
self.library.delete(self.obj)
logger.info(f"Playlist: {self.name} deleted on {self.library.account.username}")
except Failed:
logger.info(f"Playlist: {self.name} not found on {self.library.account.username}")
def send_notifications(self, playlist=False): def send_notifications(self, playlist=False):
if self.obj and self.details["changes_webhooks"] and \ if self.obj and self.details["changes_webhooks"] and \

@ -1004,6 +1004,21 @@ class Plex(Library):
except NotFound: except NotFound:
raise Failed(f"Plex Error: Playlist {title} not found") raise Failed(f"Plex Error: Playlist {title} not found")
def get_playlist_from_users(self, title):
def scan_user(server, title):
try:
for playlist in server.playlists():
if isinstance(playlist, Playlist):
if playlist.title == title:
return playlist
except requests.exceptions.ConnectionError:
pass
for user in self.users:
playlist_obj = scan_user(self.PlexServer.switchUser(user), title)
if playlist_obj:
return playlist_obj
raise Failed(f"Plex Error: Playlist {title} not found")
def get_collection(self, data, force_search=False, debug=True): def get_collection(self, data, force_search=False, debug=True):
if isinstance(data, Collection): if isinstance(data, Collection):
return data return data

@ -1046,6 +1046,9 @@ def run_playlists(config):
builder.send_notifications(playlist=True) builder.send_notifications(playlist=True)
if valid:
builder.exclude_admin_from_playlist()
except Deleted as e: except Deleted as e:
logger.info(e) logger.info(e)
status[mapping_name]["status"] = "Deleted" status[mapping_name]["status"] = "Deleted"

Loading…
Cancel
Save