python-plexapi icon indicating copy to clipboard operation
python-plexapi copied to clipboard

Playback event listening

Open KodeMunkie opened this issue 2 years ago • 2 comments

What is your feature request?

To display on an e-paper display, I'd like to listen for media playback start events rather than polling sessions every X minutes. I noticed the API supports events/notifications but the documentation appears to be light on detail and there are no examples. Please can you update the documentation and/or point me in the right area for the API to listen for playback events?

Are there any workarounds?

No response

Code Snippets

No response

Additional Context

No response

KodeMunkie avatar May 27 '22 13:05 KodeMunkie

This is possible using the alerts module: https://github.com/pkkid/python-plexapi/blob/master/plexapi/alert.py. It sets up a websocket connection where you provide a callback method, and that method is called with a dict of data received from the PMS instance when interesting events occur. It's partially documented, but you may need to do some experimentation to determine the important payloads for your use case.

I needed an async implementation of this, so I created https://github.com/jjlawren/python-plexwebsocket. It has some special handling for client sessions, including some throttling so the consuming app doesn't need to ignore unnecessary updates.

jjlawren avatar May 27 '22 14:05 jjlawren

That is exactly what I'm doing:

def listener(alert):
    global log, server, broker
    if "playing" in alert["type"]:
        n = alert["PlaySessionStateNotification"][0]
        key = int(n["key"].split("/")[-1])
        state = n["state"]
        alert = {**alert, **fetchItem(key), "state": state}
        c = findClient(n["clientIdentifier"])
        if c: # Plexamp provides information, but Alexa doesn't
            alert.update(c)
        log.debug(alert)
        broker.publish("plex/playback", dumps(alert))
    else:
        log.debug(alert)

 server.startAlertListener(listener)

rcarmo avatar Jul 21 '22 12:07 rcarmo