Skip to content

Commit

Permalink
Retrieve media info in watch status
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Apr 6, 2021
1 parent 725eb72 commit b98a6c4
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions plex_trakt_sync/commands/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,39 @@
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)
class WatchStateUpdater:
def __init__(self, plex):
self.plex = plex

def __call__(self, 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
for item in message["PlaySessionStateNotification"]:
print(item)
state = item["state"]
# "playing", 'buffering', 'stopped'
if state == 'buffering':
continue

viewOffset = item["viewOffset"]
ratingKey = item["ratingKey"]
print("Find movie for %s @ %s" % (ratingKey, viewOffset))
movie = self.plex.library.fetchItem("/library/metadata/%s" % ratingKey)
print("Found movie: %r" % movie)
percent = viewOffset/movie.duration * 100

print("%r: %.6F%% Duration: %s, Watched: %s, LastViewed: %s" % (
movie, percent, movie.duration, movie.isWatched, movie.lastViewedAt
))


@click.command()
Expand All @@ -32,7 +48,8 @@ def watch():
url = CONFIG["PLEX_BASEURL"]
token = CONFIG["PLEX_TOKEN"]
server = PlexServer(url, token)

ws = WebSocketListener(server)
ws.on(PLAYING, onPlay)
print("Listening for events")
ws.on(PLAYING, WatchStateUpdater(server))
print("Listening for events!")
ws.listen()

0 comments on commit b98a6c4

Please sign in to comment.