2017-02-11 04:08:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-02-20 05:37:00 +00:00
|
|
|
import json
|
|
|
|
import threading
|
2017-02-26 23:17:03 +00:00
|
|
|
import websocket
|
2017-02-11 04:08:36 +00:00
|
|
|
from plexapi import log
|
|
|
|
|
|
|
|
|
2017-02-25 04:50:58 +00:00
|
|
|
class AlertListener(threading.Thread):
|
|
|
|
""" Creates a websocket connection to the PlexServer to optionally recieve alert notifications.
|
|
|
|
These often include messages from Plex about media scans as well as updates to currently running
|
2017-02-24 16:18:54 +00:00
|
|
|
Transcode Sessions. This class implements threading.Thread, therfore to start monitoring
|
2017-02-25 04:50:58 +00:00
|
|
|
alerts you must call .start() on the object once it's created. When calling
|
|
|
|
`PlexServer.startAlertListener()`, the thread will be started for you.
|
2017-02-24 16:18:54 +00:00
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
Parameters:
|
2017-02-25 04:50:58 +00:00
|
|
|
server (:class:`~plexapi.server.PlexServer`): PlexServer this listener is connected to.
|
2017-02-24 16:18:54 +00:00
|
|
|
callback (func): Callback function to call on recieved messages. The callback function
|
|
|
|
will be sent a single argument 'data' which will contain a dictionary of data
|
|
|
|
recieved from the server. :samp:`def my_callback(data): ...`
|
2017-02-11 04:08:36 +00:00
|
|
|
"""
|
|
|
|
key = '/:/websockets/notifications'
|
|
|
|
|
|
|
|
def __init__(self, server, callback=None):
|
2017-04-30 01:19:59 +00:00
|
|
|
super(AlertListener, self).__init__()
|
|
|
|
self.daemon = True
|
2017-02-11 04:08:36 +00:00
|
|
|
self._server = server
|
|
|
|
self._callback = callback
|
|
|
|
self._ws = None
|
|
|
|
|
|
|
|
def run(self):
|
2017-02-11 04:26:09 +00:00
|
|
|
# create the websocket connection
|
2018-01-05 02:44:35 +00:00
|
|
|
url = self._server.url(self.key, includeToken=True).replace('http', 'ws')
|
2017-02-25 04:50:58 +00:00
|
|
|
log.info('Starting AlertListener: %s', url)
|
2017-02-27 02:09:29 +00:00
|
|
|
self._ws = websocket.WebSocketApp(url, on_message=self._onMessage,
|
2017-10-26 18:22:35 +00:00
|
|
|
on_error=self._onError)
|
2017-02-11 04:08:36 +00:00
|
|
|
self._ws.run_forever()
|
|
|
|
|
|
|
|
def stop(self):
|
2017-02-25 04:50:58 +00:00
|
|
|
""" Stop the AlertListener thread. Once the notifier is stopped, it cannot be diractly
|
|
|
|
started again. You must call :func:`plexapi.server.PlexServer.startAlertListener()`
|
2017-02-24 16:18:54 +00:00
|
|
|
from a PlexServer instance.
|
|
|
|
"""
|
2017-02-25 04:50:58 +00:00
|
|
|
log.info('Stopping AlertListener.')
|
2017-02-11 04:08:36 +00:00
|
|
|
self._ws.close()
|
|
|
|
|
|
|
|
def _onMessage(self, ws, message):
|
2017-02-13 19:38:40 +00:00
|
|
|
""" Called when websocket message is recieved. """
|
2017-02-11 04:08:36 +00:00
|
|
|
try:
|
|
|
|
data = json.loads(message)['NotificationContainer']
|
2017-10-25 22:51:25 +00:00
|
|
|
log.debug('Alert: %s %s %s', *data)
|
2017-02-11 04:08:36 +00:00
|
|
|
if self._callback:
|
|
|
|
self._callback(data)
|
2017-10-26 18:22:35 +00:00
|
|
|
except Exception as err: # pragma: no cover
|
2017-02-25 04:50:58 +00:00
|
|
|
log.error('AlertListener Msg Error: %s', err)
|
2017-02-11 04:08:36 +00:00
|
|
|
|
2017-10-26 18:22:35 +00:00
|
|
|
def _onError(self, ws, err): # pragma: no cover
|
2017-02-13 19:38:40 +00:00
|
|
|
""" Called when websocket error is recieved. """
|
2017-02-25 04:50:58 +00:00
|
|
|
log.error('AlertListener Error: %s' % err)
|