diff --git a/plexpy/activity_handler.py b/plexpy/activity_handler.py index 35eb405d..0661ad64 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -273,14 +273,25 @@ 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': - progress_percent = helpers.get_percent(db_session['view_offset'], db_session['duration']) - notify_states = notification_handler.get_notify_state(session=db_session) - if (db_session['media_type'] == 'movie' and progress_percent >= plexpy.CONFIG.MOVIE_WATCHED_PERCENT or - db_session['media_type'] == 'episode' and progress_percent >= plexpy.CONFIG.TV_WATCHED_PERCENT or - db_session['media_type'] == 'track' and progress_percent >= plexpy.CONFIG.MUSIC_WATCHED_PERCENT) \ - and not any(d['notify_action'] == 'on_watched' for d in notify_states): - logger.debug(u"Tautulli ActivityHandler :: Session %s watched." % str(self.get_session_key())) - plexpy.NOTIFY_QUEUE.put({'stream_data': db_session.copy(), 'notify_action': 'on_watched'}) + 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, + 'track': plexpy.CONFIG.MUSIC_WATCHED_PERCENT, + 'clip': plexpy.CONFIG.TV_WATCHED_PERCENT + } + + if progress_percent >= watched_percent.get(db_session['media_type'], 101): + 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'], + 'notify_action': 'on_watched'}) else: # We don't have this session in our table yet, start a new one. diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 5845ef19..1c109e3d 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -188,7 +188,7 @@ class DataFactory(object): 'episode': plexpy.CONFIG.TV_WATCHED_PERCENT, 'track': plexpy.CONFIG.MUSIC_WATCHED_PERCENT, 'photo': 0, - 'clip': plexpy.CONFIG.MOVIE_WATCHED_PERCENT + 'clip': plexpy.CONFIG.TV_WATCHED_PERCENT } rows = [] diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 9f65c415..288bc253 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -82,11 +82,6 @@ def add_notifier_each(notifier_id=None, notify_action=None, stream_data=None, ti # Check if any notification agents have notifications enabled for the action notifiers_enabled = notifiers.get_notifiers(notify_action=notify_action) - # Check if the watched notifications has already been sent - if stream_data and notify_action == 'on_watched': - watched_notifiers = [d['notifier_id'] for d in get_notify_state(session=stream_data)] - notifiers_enabled = [n for n in notifiers_enabled if n['id'] not in watched_notifiers] - if notifiers_enabled and not manual_trigger: # Check if notification conditions are satisfied conditions = notify_conditions(notify_action=notify_action, @@ -390,6 +385,28 @@ def get_notify_state(session): return notify_states +def get_notify_state_enabled(session, notify_action, notified=True): + if notified: + timestamp_where = 'AND timestamp IS NOT NULL' + else: + timestamp_where = 'AND timestamp IS NULL' + + monitor_db = database.MonitorDatabase() + result = monitor_db.select('SELECT id AS notifier_id, timestamp ' + 'FROM notifiers ' + 'LEFT OUTER JOIN (' + 'SELECT timestamp, notifier_id ' + 'FROM notify_log ' + 'WHERE session_key = ? ' + 'AND rating_key = ? ' + 'AND user_id = ? ' + 'AND notify_action = ?) AS t ON notifiers.id = t.notifier_id ' + 'WHERE %s = 1 %s' % (notify_action, timestamp_where), + args=[session['session_key'], session['rating_key'], session['user_id'], notify_action]) + + return result + + def set_notify_state(notifier, notify_action, subject='', body='', script_args='', session=None): if notifier and notify_action: