[30] Merge branch 'refs/heads/benbou8231/small_playlist_refactor' into nightly

pull/1894/head
meisnate12 10 months ago
commit 0f905d4b62

@ -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):
playlist_getters = [self.library.get_playlist, self.library.get_playlist_from_users]
for getter in playlist_getters:
try: try:
self.obj = self.library.get_playlist(self.name) self.obj = getter(self.name)
break
except Failed as e: except Failed as e:
logger.error(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:
if user == self.library.account.username:
try:
_ = self.library.get_playlist(title) # Verify if this playlist exists in Admin to avoid log confusion
self.library.delete(self.obj)
output += f"\nPlaylist deleted on {user}"
except Failed:
output += f"\nPlaylist not found on {user}"
else:
try: try:
self.library.delete_user_playlist(title, user) self.library.delete_user_playlist(title, user)
output += f"\nPlaylist deleted on User {user}" output += f"\nPlaylist deleted on User {user}"
except Failed: except Failed:
output += f"\nPlaylist not found on User {user}" 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,9 +3571,22 @@ 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:
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}") 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 \
(self.created or len(self.notification_additions) > 0 or len(self.notification_removals) > 0): (self.created or len(self.notification_additions) > 0 or len(self.notification_removals) > 0):

@ -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