From fcd759376413d1f571367cf3237636d637780e26 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Wed, 4 Apr 2018 22:05:30 -0700 Subject: [PATCH] Add temporary watched state for sessions --- plexpy/__init__.py | 13 +++++++++++-- plexpy/activity_handler.py | 10 +++++----- plexpy/activity_processor.py | 5 +++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index b6e4d886..1cb636de 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -529,8 +529,8 @@ def dbcheck(): 'transcode_hw_decoding INTEGER, transcode_hw_encoding INTEGER, ' 'optimized_version INTEGER, optimized_version_profile TEXT, optimized_version_title TEXT, ' 'synced_version INTEGER, synced_version_profile TEXT, ' - 'buffer_count INTEGER DEFAULT 0, buffer_last_triggered INTEGER, last_paused INTEGER, write_attempts INTEGER DEFAULT 0, ' - 'raw_stream_info TEXT)' + '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)' ) # session_history table :: This is a history table which logs essential stream details @@ -1038,6 +1038,15 @@ def dbcheck(): 'ALTER TABLE sessions ADD COLUMN transcode_hw_encoding INTEGER' ) + # Upgrade sessions table from earlier versions + try: + c_db.execute('SELECT watched FROM sessions') + except sqlite3.OperationalError: + logger.debug(u"Altering database. Updating database table sessions.") + c_db.execute( + 'ALTER TABLE sessions ADD COLUMN watched 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 2188ab4b..d04cb4f2 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -271,7 +271,7 @@ class ActivityHandler(object): # Monitor if the stream has reached the watch percentage for notifications # The only purpose of this is for notifications - if this_state != 'buffering': + if not db_session['watched'] and this_state != 'buffering': progress_percent = helpers.get_percent(self.timeline['viewOffset'], db_session['duration']) watched_percent = {'movie': plexpy.CONFIG.MOVIE_WATCHED_PERCENT, 'episode': plexpy.CONFIG.TV_WATCHED_PERCENT, @@ -280,13 +280,13 @@ class ActivityHandler(object): } if progress_percent >= watched_percent.get(db_session['media_type'], 101): + logger.debug(u"Tautulli ActivityHandler :: Session %s watched." + % str(self.get_session_key())) + ap.set_watched(session_key=self.get_session_key()) + watched_notifiers = notification_handler.get_notify_state_enabled( session=db_session, notify_action='on_watched', notified=False) - if watched_notifiers: - logger.debug(u"Tautulli ActivityHandler :: Session %s watched." - % str(self.get_session_key())) - for d in watched_notifiers: plexpy.NOTIFY_QUEUE.put({'stream_data': db_session.copy(), 'notifier_id': d['notifier_id'], diff --git a/plexpy/activity_processor.py b/plexpy/activity_processor.py index 41c29169..36961e1a 100644 --- a/plexpy/activity_processor.py +++ b/plexpy/activity_processor.py @@ -540,3 +540,8 @@ class ActivityProcessor(object): session = self.get_session_by_key(session_key=session_key) self.db.action('UPDATE sessions SET write_attempts = ? WHERE session_key = ?', [session['write_attempts'] + 1, session_key]) + + def set_watched(self, session_key=None): + self.db.action('UPDATE sessions SET watched = ?' + 'WHERE session_key = ?', + [1, session_key])