mirror of
https://github.com/pkkid/python-plexapi
synced 2024-11-10 14:14:19 +00:00
68fc970d7a
* lets begin * skip plexpass tests if there is not plexpass on account * test new myplex attrubutes * bootstrap: proper photos organisation * fix rest of photos tests * fix myplex new attributes test * fix music bootstrap by setting agent to lastfm * fix sync tests * increase bootstrap timeout * remove timeout from .travis.yml * do not create playlist-style photoalbums in plex-bootstraptest.py * allow negative filtering in LibrarySection.search() * fix sync tests once again * use sendCrashReports in test_settings * fix test_settings * fix test_video * do not accept eula in bootstrap * fix PlexServer.isLatest() * add test against old version of PlexServer * fix MyPlexAccount.OutOut * add flag for one-time testing in Travis * fix test_library onDeck tests * fix more tests * use tqdm in plex-bootstraptest for media scanning progress * create sections one-by-one * update docs on AlertListener for timeline entries * fix plex-bootstraptest for server version 1.3.2 * display skip/xpass/xfail reasons * fix tests on 1.3 * wait for music to be fully processed in plex-bootstraptest * fix misplaced TEST_ACCOUNT_ONCE * fix test_myplex_users, not sure if in proper-way * add pytest-rerunfailures; mark test_myplex_optout as flaky * fix comment * Revert "add pytest-rerunfailures; mark test_myplex_optout as flaky" This reverts commit580e4c95a7
. * restart plex container on failure * add conftest.wait_until() and used where some retries are required * add more wait_until() usage in test_sync * fix managed user search * fix updating managed users in myplex * allow to add new servers to existent users * add new server to a shared user while bootstrapping * add some docs on testing process * perform few attemps when unable to get the claim token * unlock websocket-client in requirements_dev * fix docblock in tools/plex-teardowntest * do not hardcode mediapart size in test_video * remove cache:pip from travis * Revert "unlock websocket-client in requirements_dev" This reverts commit0d536bd06d
. * remove debug from server.py * improve webhook tests * fix type() check to isinstance() * remove excessive `else` branch due to Hellowlol advice * add `unknown` as allowed `myPlexMappingState` in test_server
70 lines
2.9 KiB
Python
70 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
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
|
|
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.
|
|
|
|
Known `state`-values for timeline entries, with identifier=`com.plexapp.plugins.library`:
|
|
|
|
:0: The item was created
|
|
:1: Reporting progress on item processing
|
|
:2: Matching the item
|
|
:3: Downloading the metadata
|
|
:4: Processing downloaded metadata
|
|
:5: The item processed
|
|
:9: The item deleted
|
|
|
|
When metadata agent is not set for the library processing ends with state=1.
|
|
|
|
Parameters:
|
|
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): ...`
|
|
"""
|
|
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, includeToken=True).replace('http', 'ws')
|
|
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 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 AlertListener.')
|
|
self._ws.close()
|
|
|
|
def _onMessage(self, ws, message):
|
|
""" Called when websocket message is recieved. """
|
|
try:
|
|
data = json.loads(message)['NotificationContainer']
|
|
log.debug('Alert: %s %s %s', *data)
|
|
if self._callback:
|
|
self._callback(data)
|
|
except Exception as err: # pragma: no cover
|
|
log.error('AlertListener Msg Error: %s', err)
|
|
|
|
def _onError(self, ws, err): # pragma: no cover
|
|
""" Called when websocket error is recieved. """
|
|
log.error('AlertListener Error: %s' % err)
|