diff --git a/plexpy/activity_handler.py b/plexpy/activity_handler.py index 3a66925d..c5bca198 100644 --- a/plexpy/activity_handler.py +++ b/plexpy/activity_handler.py @@ -16,7 +16,7 @@ import time import plexpy -from plexpy import logger, pmsconnect, activity_processor, threading, notification_handler, helpers +from plexpy import logger, pmsconnect, activity_processor, threading, notification_handler, helpers, notifiers class ActivityHandler(object): @@ -57,9 +57,11 @@ class ActivityHandler(object): if self.is_valid_session() and self.get_live_session(): logger.debug(u"PlexPy ActivityHandler :: Session %s has started." % str(self.get_session_key())) - # Fire off notifications - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=self.get_live_session(), notify_action='play')).start() + # Check if any notification agents have notifications enabled + if any(d['on_play'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=self.get_live_session(), notify_action='play')).start() # Write the new session to our temp session table self.update_db_session() @@ -83,9 +85,11 @@ class ActivityHandler(object): # Retrieve the session data from our temp table db_session = ap.get_session_by_key(session_key=self.get_session_key()) - # Fire off notifications - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=db_session, notify_action='stop')).start() + # Check if any notification agents have notifications enabled + if any(d['on_stop'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=db_session, notify_action='stop')).start() # Write it to the history table monitor_proc = activity_processor.ActivityProcessor() @@ -111,9 +115,11 @@ class ActivityHandler(object): # Retrieve the session data from our temp table db_session = ap.get_session_by_key(session_key=self.get_session_key()) - # Fire off notifications - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=db_session, notify_action='pause')).start() + # Check if any notification agents have notifications enabled + if any(d['on_pause'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=db_session, notify_action='pause')).start() def on_resume(self): if self.is_valid_session(): @@ -131,9 +137,11 @@ class ActivityHandler(object): # Retrieve the session data from our temp table db_session = ap.get_session_by_key(session_key=self.get_session_key()) - # Fire off notifications - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=db_session, notify_action='resume')).start() + # Check if any notification agents have notifications enabled + if any(d['on_resume'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=db_session, notify_action='resume')).start() def on_buffer(self): if self.is_valid_session(): @@ -161,8 +169,11 @@ class ActivityHandler(object): if plexpy.CONFIG.BUFFER_THRESHOLD > 0 and (current_buffer_count >= plexpy.CONFIG.BUFFER_THRESHOLD and \ time_since_last_trigger == 0 or time_since_last_trigger >= plexpy.CONFIG.BUFFER_WAIT): ap.set_session_buffer_trigger_time(session_key=self.get_session_key()) - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=db_stream, notify_action='buffer')).start() + + # Check if any notification agents have notifications enabled + if any(d['on_buffer'] for d in notifiers.available_notification_agents()): + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=db_stream, notify_action='buffer')).start() # This function receives events from our websocket connection def process(self): @@ -204,10 +215,17 @@ class ActivityHandler(object): # Monitor if the stream has reached the watch percentage for notifications # The only purpose of this is for notifications - progress_percent = helpers.get_percent(self.timeline['viewOffset'], db_session['duration']) - if progress_percent >= plexpy.CONFIG.NOTIFY_WATCHED_PERCENT and this_state != 'buffering': - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=db_session, notify_action='watched')).start() + # Check if any notification agents have notifications enabled + notify_agents = [d['id'] for d in notifiers.available_notification_agents() if d['on_watched']] + # Get the current states for notifications from our db + notified_agents = [d['agent_id'] for d in notification_handler.get_notify_state(session=db_session) + if d['notify_action'] == 'watched'] if notify_agents else [] + + if any(a not in notified_agents for a in notify_agents): + progress_percent = helpers.get_percent(self.timeline['viewOffset'], db_session['duration']) + if progress_percent >= plexpy.CONFIG.NOTIFY_WATCHED_PERCENT and this_state != 'buffering': + # Rather not put this on it's own thread so we know it completes before our next event. + notification_handler.notify(stream_data=db_session, notify_action='watched') else: # We don't have this session in our table yet, start a new one. diff --git a/plexpy/activity_pinger.py b/plexpy/activity_pinger.py index 2e7498ca..b5bfc05a 100644 --- a/plexpy/activity_pinger.py +++ b/plexpy/activity_pinger.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU General Public License # along with PlexPy. If not, see . -from plexpy import logger, pmsconnect, plextv, notification_handler, database, helpers, activity_processor, libraries +from plexpy import logger, pmsconnect, plextv, notification_handler, database, helpers, activity_processor, libraries, notifiers import threading import plexpy @@ -38,9 +38,12 @@ def check_active_sessions(ws_request=False): if session_list: if int_ping_count >= 3: logger.info(u"PlexPy Monitor :: The Plex Media Server is back up.") - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(notify_action='intup')).start() + + # Check if any notification agents have notifications enabled + if any(d['on_intup'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='intup')).start() int_ping_count = 0 media_container = session_list['sessions'] @@ -60,18 +63,22 @@ def check_active_sessions(ws_request=False): if session['state'] == 'paused': logger.debug(u"PlexPy Monitor :: Session %s has been paused." % stream['session_key']) - # Push any notifications - - # Push it on it's own thread so we don't hold up our db actions - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='pause')).start() + # Check if any notification agents have notifications enabled + if any(d['on_pause'] for d in notifiers.available_notification_agents()): + # Push any notifications - + # Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='pause')).start() if session['state'] == 'playing' and stream['state'] == 'paused': logger.debug(u"PlexPy Monitor :: Session %s has been resumed." % stream['session_key']) - # Push any notifications - - # Push it on it's own thread so we don't hold up our db actions - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='resume')).start() + # Check if any notification agents have notifications enabled + if any(d['on_resume'] for d in notifiers.available_notification_agents()): + # Push any notifications - + # Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='resume')).start() if stream['state'] == 'paused' and not ws_request: # The stream is still paused so we need to increment the paused_counter @@ -109,8 +116,12 @@ def check_active_sessions(ws_request=False): 'WHERE session_key = ? AND rating_key = ?', [stream['session_key'], stream['rating_key']]) - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='buffer')).start() + # Check if any notification agents have notifications enabled + if any(d['on_buffer'] for d in notifiers.available_notification_agents()): + # Push any notifications - + # Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='buffer')).start() else: # Subsequent buffer notifications after wait time if int(time.time()) > buffer_values[0]['buffer_last_triggered'] + \ @@ -123,8 +134,12 @@ def check_active_sessions(ws_request=False): 'WHERE session_key = ? AND rating_key = ?', [stream['session_key'], stream['rating_key']]) - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='buffer')).start() + # Check if any notification agents have notifications enabled + if any(d['on_buffer'] for d in notifiers.available_notification_agents()): + # Push any notifications - + # Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='buffer')).start() logger.debug(u"PlexPy Monitor :: Session %s is buffering. Count is now %s. Last triggered %s." % (stream['session_key'], @@ -137,10 +152,12 @@ def check_active_sessions(ws_request=False): if session['view_offset'] and session['duration'] and session['state'] != 'buffering': if helpers.get_percent(session['view_offset'], session['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT: - # Push any notifications - - # Push it on it's own thread so we don't hold up our db actions - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='watched')).start() + # Check if any notification agents have notifications enabled + if any(d['on_watched'] for d in notifiers.available_notification_agents()): + # Push any notifications - + # Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='watched')).start() else: # The user has stopped playing a stream @@ -157,14 +174,18 @@ def check_active_sessions(ws_request=False): if stream['view_offset'] and stream['duration']: if helpers.get_percent(stream['view_offset'], stream['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT: - # Push any notifications - - # Push it on it's own thread so we don't hold up our db actions - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='watched')).start() + # Check if any notification agents have notifications enabled + if any(d['on_watched'] for d in notifiers.available_notification_agents()): + # Push any notifications - + # Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='watched')).start() - # Push any notifications - Push it on it's own thread so we don't hold up our db actions - threading.Thread(target=notification_handler.notify, - kwargs=dict(stream_data=stream, notify_action='stop')).start() + # Check if any notification agents have notifications enabled + if any(d['on_stop'] for d in notifiers.available_notification_agents()): + # Push any notifications - Push it on it's own thread so we don't hold up our db actions + threading.Thread(target=notification_handler.notify, + kwargs=dict(stream_data=stream, notify_action='stop')).start() # Write the item history on playback stop success = monitor_process.write_session_history(session=stream) @@ -194,9 +215,11 @@ def check_active_sessions(ws_request=False): % str(int_ping_count)) if int_ping_count == 3: - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(notify_action='intdown')).start() + # Check if any notification agents have notifications enabled + if any(d['on_intdown'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='intdown')).start() def check_recently_added(): @@ -248,9 +271,12 @@ def check_recently_added(): if 0 < time_threshold - int(item['added_at']) <= time_interval: logger.debug(u"PlexPy Monitor :: Library item %s has been added to Plex." % str(item['rating_key'])) - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(timeline_data=item, notify_action='created')).start() + + # Check if any notification agents have notifications enabled + if any(d['on_created'] for d in notifiers.available_notification_agents()): + # 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']) @@ -266,9 +292,12 @@ def check_recently_added(): % str(item['rating_key'])) logger.debug(u"PlexPy Monitor :: Library item %s has been added to Plex." % str(item['rating_key'])) - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(timeline_data=item, notify_action='created')).start() + + # Check if any notification agents have notifications enabled + if any(d['on_created'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(timeline_data=item, notify_action='created')).start() def check_server_response(): @@ -298,15 +327,20 @@ def check_server_response(): else: if ext_ping_count >= 3: logger.info(u"PlexPy Monitor :: Plex remote access is back up.") - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(notify_action='extup')).start() + + # Check if any notification agents have notifications enabled + if any(d['on_extup'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='extup')).start() ext_ping_count = 0 if ext_ping_count == 3: - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(notify_action='extdown')).start() + # Check if any notification agents have notifications enabled + if any(d['on_extdown'] for d in notifiers.available_notification_agents()): + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='extdown')).start() def check_server_updates(): diff --git a/plexpy/activity_processor.py b/plexpy/activity_processor.py index a1fa736b..b2ea9968 100644 --- a/plexpy/activity_processor.py +++ b/plexpy/activity_processor.py @@ -78,9 +78,10 @@ class ActivityProcessor(object): result = self.db.upsert('sessions', values, keys) if result == 'insert': - # Push any notifications - Push it on it's own thread so we don't hold up our db actions - if notify: + # Check if any notification agents have notifications enabled + if notify and any(d['on_play'] for d in notifiers.available_notification_agents()): values.update({'ip_address': session['ip_address']}) + # Push any notifications - Push it on it's own thread so we don't hold up our db actions threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=values, notify_action='play')).start() diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 7ac89fd9..a855f305 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -1105,7 +1105,7 @@ def build_server_notify_text(notify_action=None, agent_id=None): return None -def strip_tag(data, agent_id): +def strip_tag(data, agent_id=None): # Allow tags b, i, u, a[href], font[color] for Pushover if agent_id == 7: p = re.compile(r'<(?!/?(b>|i>|u>)|(a\shref=\"[^\"\'\s]+\"|/a>|font\scolor=\"[^\"\'\s]+\"|/font>)).*?>',