From 3ff0b4a256399cc0aa46eac38541db73cdc75710 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Thu, 23 Apr 2020 22:34:30 -0700 Subject: [PATCH] Add method to check if a stream is a continued session --- plexpy/__init__.py | 17 ++++++++++++++++- plexpy/activity_handler.py | 9 +++++---- plexpy/activity_processor.py | 34 +++++++++++++++++++++++++++++----- plexpy/config.py | 1 + 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index a640163e..fa802da8 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -610,7 +610,13 @@ def dbcheck(): 'live INTEGER, live_uuid TEXT, channel_call_sign TEXT, channel_identifier TEXT, channel_thumb TEXT, ' 'secure INTEGER, relayed INTEGER, ' 'buffer_count INTEGER DEFAULT 0, buffer_last_triggered INTEGER, last_paused INTEGER, watched INTEGER DEFAULT 0, ' - 'write_attempts INTEGER DEFAULT 0, raw_stream_info TEXT)' + 'continued_session INTEGER DEFAULT 0, write_attempts INTEGER DEFAULT 0, raw_stream_info TEXT)' + ) + + # sessions_continued table :: This is a temp table that keeps track of continued streaming sessions + c_db.execute( + 'CREATE TABLE IF NOT EXISTS sessions_continued (id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 'user_id INTEGER, machine_id TEXT, media_type TEXT, stopped INTEGER)' ) # session_history table :: This is a history table which logs essential stream details @@ -1276,6 +1282,15 @@ def dbcheck(): 'ALTER TABLE sessions ADD COLUMN location TEXT' ) + # Upgrade sessions table from earlier versions + try: + c_db.execute('SELECT continued_session FROM sessions') + except sqlite3.OperationalError: + logger.debug(u"Altering database. Updating database table sessions.") + c_db.execute( + 'ALTER TABLE sessions ADD COLUMN continued_session INTEGER DEFAULT 0' + ) + # Upgrade session_history table from earlier versions try: c_db.execute('SELECT reference_id FROM session_history') diff --git a/plexpy/activity_handler.py b/plexpy/activity_handler.py index 40c30489..0a4a980f 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -84,14 +84,14 @@ class ActivityHandler(object): return None - def update_db_session(self, session=None): + def update_db_session(self, session=None, notify=False): if session is None: session = self.get_live_session() if session: # Update our session temp table values ap = activity_processor.ActivityProcessor() - ap.write_session(session=session, notify=False) + ap.write_session(session=session, notify=notify) self.set_session_state() @@ -121,10 +121,11 @@ class ActivityHandler(object): % (str(session['session_key']), str(session['user_id']), session['username'], str(session['rating_key']), session['full_title'], '[Live TV]' if session['live'] else '')) - plexpy.NOTIFY_QUEUE.put({'stream_data': session.copy(), 'notify_action': 'on_play'}) + # Send notification after updating db + #plexpy.NOTIFY_QUEUE.put({'stream_data': session.copy(), 'notify_action': 'on_play'}) # Write the new session to our temp session table - self.update_db_session(session=session) + self.update_db_session(session=session, notify=True) # Schedule a callback to force stop a stale stream 5 minutes later schedule_callback('session_key-{}'.format(self.get_session_key()), diff --git a/plexpy/activity_processor.py b/plexpy/activity_processor.py index 14ba9e82..bfd860ed 100644 --- a/plexpy/activity_processor.py +++ b/plexpy/activity_processor.py @@ -142,15 +142,20 @@ class ActivityProcessor(object): result = self.db.upsert('sessions', values, keys) if result == 'insert': - # Check if any notification agents have notifications enabled - if notify: - plexpy.NOTIFY_QUEUE.put({'stream_data': values.copy(), 'notify_action': 'on_play'}) - # If it's our first write then time stamp it. started = int(time.time()) - timestamp = {'started': started} + continued_session = self.is_continued_session(user_id=values['user_id'], + machine_id=values['machine_id'], + media_type=values['media_type'], + started=started) + timestamp = {'started': started, 'continued_session': continued_session} self.db.upsert('sessions', timestamp, keys) + # Check if any notification agents have notifications enabled + if notify: + session.update(timestamp) + plexpy.NOTIFY_QUEUE.put({'stream_data': session.copy(), 'notify_action': 'on_play'}) + # Add Live TV library if it hasn't been added if values['live']: libraries.add_live_tv_library() @@ -199,6 +204,12 @@ class ActivityProcessor(object): state='stopped', stopped=stopped) + if not is_import: + self.write_continued_session(user_id=session['user_id'], + machine_id=session['machine_id'], + media_type=session['media_type'], + stopped=stopped) + if str(session['rating_key']).isdigit() and session['media_type'] in ('movie', 'episode', 'track'): logging_enabled = True else: @@ -630,3 +641,16 @@ class ActivityProcessor(object): self.db.action('UPDATE sessions SET watched = ?' 'WHERE session_key = ?', [1, session_key]) + + def write_continued_session(self, user_id=None, machine_id=None, media_type=None, stopped=None): + keys = {'user_id': user_id, 'machine_id': machine_id, 'media_type': media_type} + values = {'stopped': stopped} + self.db.upsert(table_name='sessions_continued', key_dict=keys, value_dict=values) + + def is_continued_session(self, user_id=None, machine_id=None, media_type=None, started=None): + last_session = self.db.select_single('SELECT stopped ' + 'FROM sessions_continued ' + 'WHERE user_id = ? AND machine_id = ? AND media_type = ? ' + 'ORDER BY stopped DESC', + [user_id, machine_id, media_type]) + return int(started - last_session.get('stopped', 0) < plexpy.CONFIG.NOTIFY_CONTINUED_SESSION_THRESHOLD) diff --git a/plexpy/config.py b/plexpy/config.py index 796cc1f2..d95f4b34 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -338,6 +338,7 @@ _CONFIG_DEFINITIONS = { 'NMA_ON_NEWDEVICE': (int, 'NMA', 0), 'NOTIFICATION_THREADS': (int, 'Advanced', 2), 'NOTIFY_CONSECUTIVE': (int, 'Monitoring', 1), + 'NOTIFY_CONTINUED_SESSION_THRESHOLD': (int, 'Monitoring', 15), 'NOTIFY_GROUP_RECENTLY_ADDED_GRANDPARENT': (int, 'Monitoring', 1), 'NOTIFY_GROUP_RECENTLY_ADDED_PARENT': (int, 'Monitoring', 1), 'NOTIFY_GROUP_RECENTLY_ADDED': (int, 'Monitoring', 1),