Skip to content

Commit

Permalink
Listen for Play events and print them
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Apr 6, 2021
1 parent 0c04399 commit 725eb72
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions plex_trakt_sync/commands/watch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import click
from plexapi.server import PlexServer
from plex_trakt_sync.listener import WebSocketListener, PLAYING
from plex_trakt_sync.config import CONFIG


def onPlay(message):
"""
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 0, 'playQueueItemID': 17679, 'state': 'playing'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 10000, 'playQueueItemID': 17679, 'state': 'playing', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 20000, 'playQueueItemID': 17679, 'state': 'playing', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 30000, 'playQueueItemID': 17679, 'state': 'playing', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 30000, 'playQueueItemID': 17679, 'state': 'paused', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
{'sessionKey': '23', 'guid': '', 'ratingKey': '9725', 'url': '', 'key': '/library/metadata/9725', 'viewOffset': 30000, 'playQueueItemID': 17679, 'state': 'paused', 'transcodeSession': '18nyclub53k1ey37jjbg8ok3'}
"""
if message["size"] != 1:
raise ValueError("Unexpected size: %r" % message)

for item in message["PlaySessionStateNotification"]:
print(item)
state = item["state"]
if state == 'buffering':
continue


@click.command()
Expand All @@ -7,4 +29,10 @@ def watch():
Listen to events from Plex
"""

url = CONFIG["PLEX_BASEURL"]
token = CONFIG["PLEX_TOKEN"]
server = PlexServer(url, token)
ws = WebSocketListener(server)
ws.on(PLAYING, onPlay)
print("Listening for events")
ws.listen()

0 comments on commit 725eb72

Please sign in to comment.