diff --git a/couchpotato/core/notifications/plex/main.py b/couchpotato/core/notifications/plex/main.py index 3c127fe..d3644d5 100755 --- a/couchpotato/core/notifications/plex/main.py +++ b/couchpotato/core/notifications/plex/main.py @@ -29,31 +29,40 @@ class Plex(Notification): return self.server.refresh() - def notifyClients(self, message, clients): + def getClientNames(self): + return [ + x.strip().lower() + for x in self.conf('clients').split(',') + ] + + def notifyClients(self, message, client_names): success = True - while len(clients): - client = clients[0] + while len(client_names): + client_name = client_names[0] + client_success = False + client = self.server.clients.get(client_name) + + if client: + client_success = fireEvent('notify.plex.notifyClient', client, message, single=True) - success = fireEvent('notify.plex.notifyClient', client, message, single=True) + if client_success: + client_names.pop(0) - if success: - clients.pop(0) - else: + if not client_success: if self.server.staleClients(): log.info('Failed to send notification to client "%s". ' - 'Client list is stale, updating the client list and retrying.', client['name']) - self.server.updateClients() + 'Client list is stale, updating the client list and retrying.', client_name) + self.server.updateClients(self.getClientNames()) else: - log.warning('Failed to send notification to client %s, skipping this time', client['name']) - clients.pop(0) + log.warning('Failed to send notification to client %s, skipping this time', client_name) + client_names.pop(0) success = False - break return success def notify(self, message = '', data = {}, listener = None): - return self.notifyClients(message, self.server.clients.values()) + return self.notifyClients(message, self.getClientNames()) def test(self, **kwargs): diff --git a/couchpotato/core/notifications/plex/server.py b/couchpotato/core/notifications/plex/server.py index 67e4937..4df6e9b 100644 --- a/couchpotato/core/notifications/plex/server.py +++ b/couchpotato/core/notifications/plex/server.py @@ -44,7 +44,7 @@ class PlexServer(object): else: return data - def updateClients(self): + def updateClients(self, client_names): log.info('Searching for clients on Plex Media Server') self.clients = {} @@ -53,14 +53,9 @@ class PlexServer(object): if not result: return - notify_clients = [ - x.strip().lower() - for x in self.plex.conf('clients').split(',') - ] - found_clients = [ c for c in result.findall('Server') - if c.get('name') and c.get('name').lower() in notify_clients + if c.get('name') and c.get('name').lower() in client_names ] for client in found_clients: @@ -73,10 +68,10 @@ class PlexServer(object): 'protocol': client.get('protocol', 'xbmchttp') } - notify_clients.remove(name) + client_names.remove(name) - if len(notify_clients) > 0: - log.debug('Unable to find clients: %s', ', '.join(notify_clients)) + if len(client_names) > 0: + log.debug('Unable to find clients: %s', ', '.join(client_names)) self.last_clients_update = datetime.now()