Check if notification agents enabled before sending notifications

This commit is contained in:
JonnyWong16 2016-02-28 22:17:03 -08:00
parent d73e379dcf
commit cc1e888227
4 changed files with 117 additions and 64 deletions

View file

@ -16,7 +16,7 @@
import time import time
import plexpy 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): class ActivityHandler(object):
@ -57,9 +57,11 @@ class ActivityHandler(object):
if self.is_valid_session() and self.get_live_session(): if self.is_valid_session() and self.get_live_session():
logger.debug(u"PlexPy ActivityHandler :: Session %s has started." % str(self.get_session_key())) logger.debug(u"PlexPy ActivityHandler :: Session %s has started." % str(self.get_session_key()))
# Fire off notifications # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify, if any(d['on_play'] for d in notifiers.available_notification_agents()):
kwargs=dict(stream_data=self.get_live_session(), notify_action='play')).start() # 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 # Write the new session to our temp session table
self.update_db_session() self.update_db_session()
@ -83,9 +85,11 @@ class ActivityHandler(object):
# Retrieve the session data from our temp table # Retrieve the session data from our temp table
db_session = ap.get_session_by_key(session_key=self.get_session_key()) db_session = ap.get_session_by_key(session_key=self.get_session_key())
# Fire off notifications # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify, if any(d['on_stop'] for d in notifiers.available_notification_agents()):
kwargs=dict(stream_data=db_session, notify_action='stop')).start() # 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 # Write it to the history table
monitor_proc = activity_processor.ActivityProcessor() monitor_proc = activity_processor.ActivityProcessor()
@ -111,9 +115,11 @@ class ActivityHandler(object):
# Retrieve the session data from our temp table # Retrieve the session data from our temp table
db_session = ap.get_session_by_key(session_key=self.get_session_key()) db_session = ap.get_session_by_key(session_key=self.get_session_key())
# Fire off notifications # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify, if any(d['on_pause'] for d in notifiers.available_notification_agents()):
kwargs=dict(stream_data=db_session, notify_action='pause')).start() # Fire off notifications
threading.Thread(target=notification_handler.notify,
kwargs=dict(stream_data=db_session, notify_action='pause')).start()
def on_resume(self): def on_resume(self):
if self.is_valid_session(): if self.is_valid_session():
@ -131,9 +137,11 @@ class ActivityHandler(object):
# Retrieve the session data from our temp table # Retrieve the session data from our temp table
db_session = ap.get_session_by_key(session_key=self.get_session_key()) db_session = ap.get_session_by_key(session_key=self.get_session_key())
# Fire off notifications # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify, if any(d['on_resume'] for d in notifiers.available_notification_agents()):
kwargs=dict(stream_data=db_session, notify_action='resume')).start() # Fire off notifications
threading.Thread(target=notification_handler.notify,
kwargs=dict(stream_data=db_session, notify_action='resume')).start()
def on_buffer(self): def on_buffer(self):
if self.is_valid_session(): 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 \ 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): 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()) 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 # This function receives events from our websocket connection
def process(self): def process(self):
@ -204,10 +215,17 @@ class ActivityHandler(object):
# Monitor if the stream has reached the watch percentage for notifications # Monitor if the stream has reached the watch percentage for notifications
# The only purpose of this is for notifications # The only purpose of this is for notifications
progress_percent = helpers.get_percent(self.timeline['viewOffset'], db_session['duration']) # Check if any notification agents have notifications enabled
if progress_percent >= plexpy.CONFIG.NOTIFY_WATCHED_PERCENT and this_state != 'buffering': notify_agents = [d['id'] for d in notifiers.available_notification_agents() if d['on_watched']]
threading.Thread(target=notification_handler.notify, # Get the current states for notifications from our db
kwargs=dict(stream_data=db_session, notify_action='watched')).start() 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: else:
# We don't have this session in our table yet, start a new one. # We don't have this session in our table yet, start a new one.

View file

@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>. # along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
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 threading
import plexpy import plexpy
@ -38,9 +38,12 @@ def check_active_sessions(ws_request=False):
if session_list: if session_list:
if int_ping_count >= 3: if int_ping_count >= 3:
logger.info(u"PlexPy Monitor :: The Plex Media Server is back up.") logger.info(u"PlexPy Monitor :: The Plex Media Server is back up.")
# Fire off notifications
threading.Thread(target=notification_handler.notify_timeline, # Check if any notification agents have notifications enabled
kwargs=dict(notify_action='intup')).start() 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 int_ping_count = 0
media_container = session_list['sessions'] media_container = session_list['sessions']
@ -60,18 +63,22 @@ def check_active_sessions(ws_request=False):
if session['state'] == 'paused': if session['state'] == 'paused':
logger.debug(u"PlexPy Monitor :: Session %s has been paused." % stream['session_key']) logger.debug(u"PlexPy Monitor :: Session %s has been paused." % stream['session_key'])
# Push any notifications - # Check if any notification agents have notifications enabled
# Push it on it's own thread so we don't hold up our db actions if any(d['on_pause'] for d in notifiers.available_notification_agents()):
threading.Thread(target=notification_handler.notify, # Push any notifications -
kwargs=dict(stream_data=stream, notify_action='pause')).start() # 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': if session['state'] == 'playing' and stream['state'] == 'paused':
logger.debug(u"PlexPy Monitor :: Session %s has been resumed." % stream['session_key']) logger.debug(u"PlexPy Monitor :: Session %s has been resumed." % stream['session_key'])
# Push any notifications - # Check if any notification agents have notifications enabled
# Push it on it's own thread so we don't hold up our db actions if any(d['on_resume'] for d in notifiers.available_notification_agents()):
threading.Thread(target=notification_handler.notify, # Push any notifications -
kwargs=dict(stream_data=stream, notify_action='resume')).start() # 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: if stream['state'] == 'paused' and not ws_request:
# The stream is still paused so we need to increment the paused_counter # 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 = ?', 'WHERE session_key = ? AND rating_key = ?',
[stream['session_key'], stream['rating_key']]) [stream['session_key'], stream['rating_key']])
threading.Thread(target=notification_handler.notify, # Check if any notification agents have notifications enabled
kwargs=dict(stream_data=stream, notify_action='buffer')).start() 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: else:
# Subsequent buffer notifications after wait time # Subsequent buffer notifications after wait time
if int(time.time()) > buffer_values[0]['buffer_last_triggered'] + \ 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 = ?', 'WHERE session_key = ? AND rating_key = ?',
[stream['session_key'], stream['rating_key']]) [stream['session_key'], stream['rating_key']])
threading.Thread(target=notification_handler.notify, # Check if any notification agents have notifications enabled
kwargs=dict(stream_data=stream, notify_action='buffer')).start() 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." logger.debug(u"PlexPy Monitor :: Session %s is buffering. Count is now %s. Last triggered %s."
% (stream['session_key'], % (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 session['view_offset'] and session['duration'] and session['state'] != 'buffering':
if helpers.get_percent(session['view_offset'], if helpers.get_percent(session['view_offset'],
session['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT: session['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT:
# Push any notifications - # Check if any notification agents have notifications enabled
# Push it on it's own thread so we don't hold up our db actions if any(d['on_watched'] for d in notifiers.available_notification_agents()):
threading.Thread(target=notification_handler.notify, # Push any notifications -
kwargs=dict(stream_data=stream, notify_action='watched')).start() # 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: else:
# The user has stopped playing a stream # 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 stream['view_offset'] and stream['duration']:
if helpers.get_percent(stream['view_offset'], if helpers.get_percent(stream['view_offset'],
stream['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT: stream['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT:
# Push any notifications - # Check if any notification agents have notifications enabled
# Push it on it's own thread so we don't hold up our db actions if any(d['on_watched'] for d in notifiers.available_notification_agents()):
threading.Thread(target=notification_handler.notify, # Push any notifications -
kwargs=dict(stream_data=stream, notify_action='watched')).start() # 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 # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify, if any(d['on_stop'] for d in notifiers.available_notification_agents()):
kwargs=dict(stream_data=stream, notify_action='stop')).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()
# Write the item history on playback stop # Write the item history on playback stop
success = monitor_process.write_session_history(session=stream) success = monitor_process.write_session_history(session=stream)
@ -194,9 +215,11 @@ def check_active_sessions(ws_request=False):
% str(int_ping_count)) % str(int_ping_count))
if int_ping_count == 3: if int_ping_count == 3:
# Fire off notifications # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify_timeline, if any(d['on_intdown'] for d in notifiers.available_notification_agents()):
kwargs=dict(notify_action='intdown')).start() # Fire off notifications
threading.Thread(target=notification_handler.notify_timeline,
kwargs=dict(notify_action='intdown')).start()
def check_recently_added(): def check_recently_added():
@ -248,9 +271,12 @@ def check_recently_added():
if 0 < time_threshold - int(item['added_at']) <= time_interval: 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'])) 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, # Check if any notification agents have notifications enabled
kwargs=dict(timeline_data=item, notify_action='created')).start() 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: else:
item = max(metadata, key=lambda x:x['added_at']) item = max(metadata, key=lambda x:x['added_at'])
@ -266,9 +292,12 @@ def check_recently_added():
% str(item['rating_key'])) % str(item['rating_key']))
logger.debug(u"PlexPy Monitor :: Library item %s has been added to Plex." % 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, # Check if any notification agents have notifications enabled
kwargs=dict(timeline_data=item, notify_action='created')).start() 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(): def check_server_response():
@ -298,15 +327,20 @@ def check_server_response():
else: else:
if ext_ping_count >= 3: if ext_ping_count >= 3:
logger.info(u"PlexPy Monitor :: Plex remote access is back up.") logger.info(u"PlexPy Monitor :: Plex remote access is back up.")
# Fire off notifications
threading.Thread(target=notification_handler.notify_timeline, # Check if any notification agents have notifications enabled
kwargs=dict(notify_action='extup')).start() 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 ext_ping_count = 0
if ext_ping_count == 3: if ext_ping_count == 3:
# Fire off notifications # Check if any notification agents have notifications enabled
threading.Thread(target=notification_handler.notify_timeline, if any(d['on_extdown'] for d in notifiers.available_notification_agents()):
kwargs=dict(notify_action='extdown')).start() # Fire off notifications
threading.Thread(target=notification_handler.notify_timeline,
kwargs=dict(notify_action='extdown')).start()
def check_server_updates(): def check_server_updates():

View file

@ -78,9 +78,10 @@ class ActivityProcessor(object):
result = self.db.upsert('sessions', values, keys) result = self.db.upsert('sessions', values, keys)
if result == 'insert': if result == 'insert':
# Push any notifications - Push it on it's own thread so we don't hold up our db actions # Check if any notification agents have notifications enabled
if notify: if notify and any(d['on_play'] for d in notifiers.available_notification_agents()):
values.update({'ip_address': session['ip_address']}) 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, threading.Thread(target=notification_handler.notify,
kwargs=dict(stream_data=values, notify_action='play')).start() kwargs=dict(stream_data=values, notify_action='play')).start()

View file

@ -1105,7 +1105,7 @@ def build_server_notify_text(notify_action=None, agent_id=None):
return 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 # Allow tags b, i, u, a[href], font[color] for Pushover
if agent_id == 7: if agent_id == 7:
p = re.compile(r'<(?!/?(b>|i>|u>)|(a\shref=\"[^\"\'\s]+\"|/a>|font\scolor=\"[^\"\'\s]+\"|/font>)).*?>', p = re.compile(r'<(?!/?(b>|i>|u>)|(a\shref=\"[^\"\'\s]+\"|/a>|font\scolor=\"[^\"\'\s]+\"|/font>)).*?>',