diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 3cf7d2a9..ae6e2a02 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -285,13 +285,13 @@ def initialize_scheduler(): hours=12, minutes=0, seconds=0) schedule_job(pmsconnect.PmsConnect().get_server_friendly_name, 'Refresh Plex Server Name', hours=12, minutes=0, seconds=0) + schedule_job(activity_pinger.check_recently_added, 'Check for recently added items', + hours=0, minutes=0, seconds=seconds) # If we're not using websockets then fall back to polling if not CONFIG.MONITORING_USE_WEBSOCKET or POLLING_FAILOVER: schedule_job(activity_pinger.check_active_sessions, 'Check for active sessions', hours=0, minutes=0, seconds=seconds) - schedule_job(activity_pinger.check_recently_added, 'Check for recently added items', - hours=0, minutes=0, seconds=seconds) # Refresh the users list if CONFIG.REFRESH_USERS_INTERVAL: diff --git a/plexpy/activity_handler.py b/plexpy/activity_handler.py index f726b3fe..967a2e21 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -218,7 +218,7 @@ class TimelineHandler(object): def __init__(self, timeline): self.timeline = timeline - logger.debug(timeline) + #logger.debug(timeline) def is_item(self): if 'itemID' in self.timeline: diff --git a/plexpy/activity_pinger.py b/plexpy/activity_pinger.py index 28fd781d..baa280d2 100644 --- a/plexpy/activity_pinger.py +++ b/plexpy/activity_pinger.py @@ -167,7 +167,10 @@ def check_active_sessions(ws_request=False): def check_recently_added(): with monitor_lock: - current_time = int(time.time()) + # add delay to allow for metadata processing + delay = plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_DELAY + time_threshold = int(time.time()) - delay + time_interval = plexpy.CONFIG.MONITORING_INTERVAL pms_connect = pmsconnect.PmsConnect() recently_added_list = pms_connect.get_recently_added_details(count='10') @@ -176,36 +179,43 @@ def check_recently_added(): recently_added = recently_added_list['recently_added'] for item in recently_added: - if int(item['added_at']) >= current_time - plexpy.CONFIG.MONITORING_INTERVAL: - if item['media_type'] == 'movie': - metadata_list = pms_connect.get_metadata_details(item['rating_key']) - if metadata_list: - metadata = [metadata_list['metadata']] - else: - logger.error(u"PlexPy Monitor :: Unable to retrieve metadata for rating_key %s" % str(item['rating_key'])) - - elif plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT: - metadata_list = pms_connect.get_metadata_details(item['parent_rating_key']) - if metadata_list: - metadata = [metadata_list['metadata']] - else: - logger.error(u"PlexPy Monitor :: Unable to retrieve metadata for parent_rating_key %s" % str(item['parent_rating_key'])) - + if item['media_type'] == 'movie': + metadata_list = pms_connect.get_metadata_details(item['rating_key']) + if metadata_list: + metadata = [metadata_list['metadata']] else: - metadata_list = pms_connect.get_metadata_children_details(item['rating_key']) - if metadata_list: - metadata = metadata_list['metadata'] - else: - logger.error(u"PlexPy Monitor :: Unable to retrieve children metadata for rating_key" % str(item['rating_key'])) + logger.error(u"PlexPy Monitor :: Unable to retrieve metadata for rating_key %s" \ + % str(item['rating_key'])) - if metadata: + else: + metadata_list = pms_connect.get_metadata_children_details(item['rating_key']) + if metadata_list: + metadata = metadata_list['metadata'] + else: + logger.error(u"PlexPy Monitor :: Unable to retrieve children metadata for rating_key %s" \ + % str(item['rating_key'])) + + if metadata: + if not plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT: for item in metadata: - if (plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT \ - and int(item['updated_at']) >= current_time - plexpy.CONFIG.MONITORING_INTERVAL) \ - or (not plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT \ - and int(item['added_at']) >= current_time - plexpy.CONFIG.MONITORING_INTERVAL): - logger.debug(u"PlexPy Monitor :: Library item %s has been added to Plex." % str(item['rating_key'])) - + if 0 < int(item['added_at']) - time_threshold <= time_interval: # Fire off notifications threading.Thread(target=notification_handler.notify_timeline, kwargs=dict(timeline_data=item, notify_action='created')).start() + + else: + item = max(metadata, key=lambda x:x['added_at']) + + if 0 < int(item['added_at']) - time_threshold <= time_interval: + if item['media_type'] == 'episode' or item['media_type'] == 'track': + metadata_list = pms_connect.get_metadata_details(item['grandparent_rating_key']) + + if metadata_list: + item = metadata_list['metadata'] + else: + logger.error(u"PlexPy Monitor :: Unable to retrieve grandparent metadata for grandparent_rating_key %s" \ + % str(item['rating_key'])) + + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(timeline_data=item, notify_action='created')).start() diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 562f2d91..4318e923 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -172,17 +172,13 @@ def notify_timeline(timeline_data=None, notify_action=None): if timeline_data and notify_action: for agent in notifiers.available_notification_agents(): if agent['on_created'] and notify_action == 'created': - if (plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT \ - and (timeline_data['media_type'] == 'movie' or timeline_data['media_type'] == 'show' or timeline_data['media_type'] == 'artist')) \ - or (not plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT \ - and (timeline_data['media_type'] == 'movie' or timeline_data['media_type'] == 'episode' or timeline_data['media_type'] == 'track')): - # Build and send notification - notify_strings = build_notify_text(timeline=timeline_data, state=notify_action) - notifiers.send_notification(config_id=agent['id'], - subject=notify_strings[0], - body=notify_strings[1]) - # Set the notification state in the db - set_notify_state(session=timeline_data, state=notify_action, agent_info=agent) + # Build and send notification + notify_strings = build_notify_text(timeline=timeline_data, state=notify_action) + notifiers.send_notification(config_id=agent['id'], + subject=notify_strings[0], + body=notify_strings[1]) + # Set the notification state in the db + set_notify_state(session=timeline_data, state=notify_action, agent_info=agent) else: logger.debug(u"PlexPy Notifier :: Notify timeline called but incomplete data received.") diff --git a/plexpy/web_socket.py b/plexpy/web_socket.py index 17603fe8..dc2a3f29 100644 --- a/plexpy/web_socket.py +++ b/plexpy/web_socket.py @@ -139,14 +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 + #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() + # activity = activity_handler.TimelineHandler(timeline=time_line[0]) + # activity.process() return True