From 1c58a47073a11e7c29dfd1d3ea4e7582b071788a Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Tue, 20 Oct 2015 22:40:48 -0700 Subject: [PATCH] Update websocket and handler for timeline events --- plexpy/activity_handler.py | 49 ++++++++++++++++++++++++++++++++++++-- plexpy/web_socket.py | 12 +++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/plexpy/activity_handler.py b/plexpy/activity_handler.py index 675eb891..94962b15 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -1,4 +1,4 @@ -# This file is part of PlexPy. +# This file is part of PlexPy. # # PlexPy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -212,4 +212,49 @@ class ActivityHandler(object): else: # We don't have this session in our table yet, start a new one. if this_state != 'buffering': - self.on_start() \ No newline at end of file + self.on_start() + +class TimelineHandler(object): + + def __init__(self, timeline): + self.timeline = timeline + # print timeline + + def is_item(self): + if 'itemID' in self.timeline and 'metadataState' in self.timeline: + return True + + return False + + def get_rating_key(self): + if self.is_item(): + return int(self.timeline['itemID']) + + return None + + def get_metadata(self): + pms_connect = pmsconnect.PmsConnect() + metadata_list = pms_connect.get_metadata_details(self.get_rating_key()) + + if metadata_list: + return metadata_list['metadata'] + + return None + + def on_created(self): + if self.is_item(): + logger.debug(u"PlexPy TimelineHandler :: Library item %s has been added to Plex." % str(self.get_rating_key())) + + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(timeline_data=self.get_metadata(), notify_action='created')).start() + + # This function receives events from our websocket connection + def process(self): + if self.is_item(): + + this_state = self.timeline['state'] + this_metadataState = self.timeline['metadataState'] + + if this_state == 0 and this_metadataState == 'created': + self.on_created() \ No newline at end of file diff --git a/plexpy/web_socket.py b/plexpy/web_socket.py index c9740b9b..17603fe8 100644 --- a/plexpy/web_socket.py +++ b/plexpy/web_socket.py @@ -1,4 +1,4 @@ -# This file is part of PlexPy. +# This file is part of PlexPy. # # PlexPy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -139,4 +139,14 @@ def process(opcode, data): activity = activity_handler.ActivityHandler(timeline=time_line[0]) activity.process() + if type == 'timeline': + try: + time_line = info.get('_children') + except: + logger.debug(u"PlexPy WebSocket :: Timeline event found but unable to get timeline data.") + return False + + activity = activity_handler.TimelineHandler(timeline=time_line[0]) + activity.process() + return True