From d6b31dc542dc83596199fd3c5eaac8abd8ae5890 Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Sun, 25 Oct 2015 12:20:00 -0700 Subject: [PATCH] Set on_created notification in database --- plexpy/__init__.py | 11 +++++- plexpy/activity_handler.py | 14 +++++--- plexpy/notification_handler.py | 62 +++++++++++++++++++++++----------- 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 038cf14d..5d2216c4 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -552,7 +552,7 @@ def dbcheck(): 'CREATE TABLE IF NOT EXISTS notify_log (id INTEGER PRIMARY KEY AUTOINCREMENT, ' 'session_key INTEGER, rating_key INTEGER, user_id INTEGER, user TEXT, ' 'agent_id INTEGER, agent_name TEXT, on_play INTEGER, on_stop INTEGER, on_watched INTEGER, ' - 'on_pause INTEGER, on_resume INTEGER, on_buffer INTEGER)' + 'on_pause INTEGER, on_resume INTEGER, on_buffer INTEGER, on_created INTEGER)' ) # Upgrade users table from earlier versions @@ -588,6 +588,15 @@ def dbcheck(): 'ALTER TABLE notify_log ADD COLUMN on_buffer INTEGER' ) + # Upgrade notify_log table from earlier versions + try: + c_db.execute('SELECT on_created from notify_log') + except sqlite3.OperationalError: + logger.debug(u"Altering database. Updating database table notify_log.") + c_db.execute( + 'ALTER TABLE notify_log ADD COLUMN on_created INTEGER' + ) + # Upgrade sessions table from earlier versions try: c_db.execute('SELECT buffer_count from sessions') diff --git a/plexpy/activity_handler.py b/plexpy/activity_handler.py index 94962b15..a6dc8d17 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -221,7 +221,7 @@ class TimelineHandler(object): # print timeline def is_item(self): - if 'itemID' in self.timeline and 'metadataState' in self.timeline: + if 'itemID' in self.timeline: return True return False @@ -254,7 +254,13 @@ class TimelineHandler(object): if self.is_item(): this_state = self.timeline['state'] - this_metadataState = self.timeline['metadataState'] + this_type = self.timeline['type'] + this_metadataState = self.timeline.get('metadataState', None) + this_section_id = self.timeline['sectionID'] + this_rating_key = self.timeline['itemID'] - if this_state == 0 and this_metadataState == 'created': - self.on_created() \ No newline at end of file + # state: 5: done processing metadata + # type: 1: movie, 2: tv show, 4: episode, 8: artist, 10: track + types = [1, 2, 4, 8, 10] + if this_state == 5 and this_type in types and not this_metadataState: + self.on_created() diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index d2d38295..57085b09 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -43,7 +43,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='play', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_stop'] and notify_action == 'stop' \ and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < plexpy.CONFIG.NOTIFY_WATCHED_PERCENT): @@ -53,7 +53,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) - set_notify_state(session=stream_data, state='stop', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_pause'] and notify_action == 'pause' \ and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99): @@ -63,7 +63,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) - set_notify_state(session=stream_data, state='pause', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_resume'] and notify_action == 'resume' \ and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99): @@ -73,7 +73,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) - set_notify_state(session=stream_data, state='resume', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_buffer'] and notify_action == 'buffer': # Build and send notification @@ -82,7 +82,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) - set_notify_state(session=stream_data, state='buffer', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_watched'] and notify_action == 'watched': # Get the current states for notifications from our db @@ -96,7 +96,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='watched', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) else: # Check in our notify log if the notification has already been sent @@ -108,7 +108,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='watched', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif stream_data['media_type'] == 'track': if plexpy.CONFIG.MUSIC_NOTIFY_ENABLE: @@ -121,7 +121,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='play', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_stop'] and notify_action == 'stop': # Build and send notification @@ -130,7 +130,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='stop', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_pause'] and notify_action == 'pause': # Build and send notification @@ -139,7 +139,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='pause', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_resume'] and notify_action == 'resume': # Build and send notification @@ -148,7 +148,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='resume', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif agent['on_buffer'] and notify_action == 'buffer': # Build and send notification @@ -157,7 +157,7 @@ def notify(stream_data=None, notify_action=None): subject=notify_strings[0], body=notify_strings[1]) # Set the notification state in the db - set_notify_state(session=stream_data, state='buffer', agent_info=agent) + set_notify_state(session=stream_data, state=notify_action, agent_info=agent) elif stream_data['media_type'] == 'clip': pass @@ -177,6 +177,8 @@ def notify_timeline(timeline_data=None, notify_action=None): 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.") @@ -203,6 +205,21 @@ def get_notify_state(session): return notify_states +def get_notify_state_timeline(timeline): + monitor_db = database.MonitorDatabase() + result = monitor_db.select('SELECT on_created, agent_id ' + 'FROM notify_log ' + 'WHERE rating_key = ? ' + 'ORDER BY id DESC', + args=[timeline['rating_key']]) + notify_states = [] + for item in result: + notify_state = {'on_created': item[0], + 'agent_id': item[1]} + notify_states.append(notify_state) + + return notify_states + def set_notify_state(session, state, agent_info): @@ -221,15 +238,22 @@ def set_notify_state(session, state, agent_info): values = {'on_buffer': int(time.time())} elif state == 'watched': values = {'on_watched': int(time.time())} + elif state == 'created': + values = {'on_created': int(time.time())} else: return - keys = {'session_key': session['session_key'], - 'rating_key': session['rating_key'], - 'user_id': session['user_id'], - 'user': session['user'], - 'agent_id': agent_info['id'], - 'agent_name': agent_info['name']} + if state == 'created': + keys = {'rating_key': session['rating_key'], + 'agent_id': agent_info['id'], + 'agent_name': agent_info['name']} + else: + keys = {'session_key': session['session_key'], + 'rating_key': session['rating_key'], + 'user_id': session['user_id'], + 'user': session['user'], + 'agent_id': agent_info['id'], + 'agent_name': agent_info['name']} monitor_db.upsert(table_name='notify_log', key_dict=keys, value_dict=values) else: @@ -326,7 +350,7 @@ def build_notify_text(session, state): duration = helpers.convert_milliseconds_to_minutes(metadata['duration']) - view_offset = '' + view_offset = 0 if 'view_offset' in session: view_offset = helpers.convert_milliseconds_to_minutes(session['view_offset'])