Switch recently added to only use activity pinger

* Fixed activity pinger logic for grouping notifications
This commit is contained in:
Jonathan Wong 2015-11-16 21:19:04 -08:00
parent 53044c75dd
commit 69a3b5134f
5 changed files with 56 additions and 50 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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()

View file

@ -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.")

View file

@ -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