mirror of
https://github.com/pkkid/python-plexapi
synced 2024-11-10 06:04:15 +00:00
I was never happy with notifier being the name of the event listener. AlertListener is more boring, but also more clear what its doing.
This commit is contained in:
parent
b35b265776
commit
e66cc0a9c0
3 changed files with 22 additions and 22 deletions
|
@ -5,18 +5,18 @@ from plexapi import log
|
|||
from plexapi.exceptions import Unsupported
|
||||
|
||||
|
||||
class PlexNotifier(threading.Thread):
|
||||
""" Creates a websocket connection to the Plex Server to optionally recieve notifications. These
|
||||
often include messages from Plex about media scans as well as updates to currently running
|
||||
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
|
||||
Transcode Sessions. This class implements threading.Thread, therfore to start monitoring
|
||||
notifications you must call .start() on the object once it's created. When calling
|
||||
`PlexServer.startNotifier()`, the thread will be started for you.
|
||||
alerts you must call .start() on the object once it's created. When calling
|
||||
`PlexServer.startAlertListener()`, the thread will be started for you.
|
||||
|
||||
In order to use this feature, you must have websocket-client installed in your Python path.
|
||||
This can be installed vis pip `pip install websocket-client`.
|
||||
|
||||
Parameters:
|
||||
server (:class:`~plexapi.server.PlexServer`): PlexServer this notifier is connected to.
|
||||
server (:class:`~plexapi.server.PlexServer`): PlexServer this listener is connected to.
|
||||
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): ...`
|
||||
|
@ -27,7 +27,7 @@ class PlexNotifier(threading.Thread):
|
|||
self._server = server
|
||||
self._callback = callback
|
||||
self._ws = None
|
||||
super(PlexNotifier, self).__init__()
|
||||
super(AlertListener, self).__init__()
|
||||
|
||||
def run(self):
|
||||
# try importing websocket-client package
|
||||
|
@ -37,30 +37,30 @@ class PlexNotifier(threading.Thread):
|
|||
raise Unsupported('Websocket-client package is required to use this feature.')
|
||||
# create the websocket connection
|
||||
url = self._server.url(self.key).replace('http', 'ws')
|
||||
log.info('Starting PlexNotifier: %s', url)
|
||||
log.info('Starting AlertListener: %s', url)
|
||||
self._ws = websocket.WebSocketApp(url,
|
||||
on_message=self._onMessage,
|
||||
on_error=self._onError)
|
||||
self._ws.run_forever()
|
||||
|
||||
def stop(self):
|
||||
""" Stop the PlexNotifier thread. Once the notifier is stopped, it cannot be diractly
|
||||
started again. You must call :func:`plexapi.server.PlexServer.startNotifier()`
|
||||
""" Stop the AlertListener thread. Once the notifier is stopped, it cannot be diractly
|
||||
started again. You must call :func:`plexapi.server.PlexServer.startAlertListener()`
|
||||
from a PlexServer instance.
|
||||
"""
|
||||
log.info('Stopping PlexNotifier.')
|
||||
log.info('Stopping AlertListener.')
|
||||
self._ws.close()
|
||||
|
||||
def _onMessage(self, ws, message):
|
||||
""" Called when websocket message is recieved. """
|
||||
try:
|
||||
data = json.loads(message)['NotificationContainer']
|
||||
log.debug('Notify: %s', data)
|
||||
log.debug('Alert: %s', data)
|
||||
if self._callback:
|
||||
self._callback(data)
|
||||
except Exception as err:
|
||||
log.error('PlexNotifier Msg Error: %s', err)
|
||||
log.error('AlertListener Msg Error: %s', err)
|
||||
|
||||
def _onError(self, ws, err):
|
||||
""" Called when websocket error is recieved. """
|
||||
log.error('PlexNotifier Error: %s' % err)
|
||||
log.error('AlertListener Error: %s' % err)
|
|
@ -3,20 +3,20 @@ import requests
|
|||
from requests.status_codes import _codes as codes
|
||||
from plexapi import BASE_HEADERS, CONFIG, TIMEOUT
|
||||
from plexapi import log, logfilter, utils
|
||||
from plexapi.alert import AlertListener
|
||||
from plexapi.base import PlexObject
|
||||
from plexapi.client import PlexClient
|
||||
from plexapi.compat import ElementTree, urlencode
|
||||
from plexapi.exceptions import BadRequest, NotFound
|
||||
from plexapi.library import Library, Hub
|
||||
from plexapi.settings import Settings
|
||||
from plexapi.notify import PlexNotifier
|
||||
from plexapi.playlist import Playlist
|
||||
from plexapi.playqueue import PlayQueue
|
||||
from plexapi.utils import cast
|
||||
|
||||
# Need these imports to populate utils.PLEXOBJECTS
|
||||
from plexapi import (audio as _audio, video as _video, # noqa: F401
|
||||
photo as _photo, media as _media, playlist as _playlist) # noqa: F401
|
||||
from plexapi import (audio as _audio, video as _video,
|
||||
photo as _photo, media as _media, playlist as _playlist)
|
||||
|
||||
|
||||
class PlexServer(PlexObject):
|
||||
|
@ -285,7 +285,7 @@ class PlexServer(PlexObject):
|
|||
""" Returns a list of all active session (currently playing) media objects. """
|
||||
return self.fetchItems('/status/sessions')
|
||||
|
||||
def startNotifier(self, callback=None):
|
||||
def startAlertListener(self, callback=None):
|
||||
""" Creates a websocket connection to the Plex Server to optionally recieve
|
||||
notifications. These often include messages from Plex about media scans
|
||||
as well as updates to currently running Transcode Sessions.
|
||||
|
@ -299,7 +299,7 @@ class PlexServer(PlexObject):
|
|||
raises:
|
||||
:class:`~plexapi.exception.Unsupported`: Websocket-client not installed.
|
||||
"""
|
||||
notifier = PlexNotifier(self, callback)
|
||||
notifier = AlertListener(self, callback)
|
||||
notifier.start()
|
||||
return notifier
|
||||
|
||||
|
|
|
@ -23,13 +23,13 @@ def test_server_attr(pms):
|
|||
assert pms.version == '1.3.3.3148-b38628e'
|
||||
|
||||
|
||||
def test_server_notifier(pms, a_movie_section):
|
||||
def test_server_alert_listener(pms, a_movie_section):
|
||||
import time
|
||||
messages = []
|
||||
notifier = pms.startNotifier(messages.append)
|
||||
listener = pms.startAlertListener(messages.append)
|
||||
a_movie_section.refresh()
|
||||
time.sleep(5)
|
||||
notifier.stop()
|
||||
listener.stop()
|
||||
assert len(messages) >= 3
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue