python-plexapi/plexapi/alert.py

59 lines
2.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2017-02-20 05:37:00 +00:00
import json
import threading
import websocket
from plexapi import log
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
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
Parameters:
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): ...`
"""
key = '/:/websockets/notifications'
def __init__(self, server, callback=None):
super(AlertListener, self).__init__()
self.daemon = True
self._server = server
self._callback = callback
self._ws = None
def run(self):
# create the websocket connection
url = self._server.url(self.key).replace('http', 'ws')
log.info('Starting AlertListener: %s', url)
2017-02-27 02:09:29 +00:00
self._ws = websocket.WebSocketApp(url, on_message=self._onMessage,
on_error=self._onError)
self._ws.run_forever()
def stop(self):
""" 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.
"""
log.info('Stopping AlertListener.')
self._ws.close()
def _onMessage(self, ws, message):
2017-02-13 19:38:40 +00:00
""" Called when websocket message is recieved. """
try:
data = json.loads(message)['NotificationContainer']
log.debug('Alert: %s', data)
if self._callback:
self._callback(data)
except Exception as err:
log.error('AlertListener Msg Error: %s', err)
def _onError(self, ws, err):
2017-02-13 19:38:40 +00:00
""" Called when websocket error is recieved. """
log.error('AlertListener Error: %s' % err)