Update notification handler for all notification triggers

This commit is contained in:
JonnyWong16 2016-10-02 12:52:52 -07:00 committed by JonnyWong16
parent 1206d13978
commit 7b2a7aff9f
7 changed files with 245 additions and 1034 deletions

View file

@ -10,7 +10,7 @@ DOCUMENTATION :: END
</%doc>
<ul class="stacked-configs list-unstyled">
% for notifier in notifiers_list:
% for notifier in sorted(notifiers_list, key=lambda k: (k['agent_label'], k['id'])):
<li>
<span>
<span class="toggle-left trigger-tooltip ${'active' if notifier['active'] else ''}" data-toggle="tooltip" data-placement="top" title="Triggers active"><i class="fa fa-lg fa-bell"></i></span>

View file

@ -14,6 +14,7 @@
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
import os
from Queue import Queue
import sqlite3
import sys
import subprocess
@ -35,6 +36,7 @@ import activity_pinger
import config
import database
import logger
import notification_handler
import plextv
import pmsconnect
import versioncheck
@ -58,6 +60,8 @@ PIDFILE = None
SCHED = BackgroundScheduler()
SCHED_LOCK = threading.Lock()
NOTIFY_QUEUE = Queue()
INIT_LOCK = threading.Lock()
_INITIALIZED = False
started = False
@ -345,7 +349,6 @@ def initialize_scheduler():
# Debug
#SCHED.print_jobs()
def schedule_job(function, name, hours=0, minutes=0, seconds=0, args=None):
"""
Start scheduled job if starting or restarting plexpy.
@ -374,6 +377,13 @@ def start():
if _INITIALIZED:
initialize_scheduler()
# Start background notification thread
if any([CONFIG.MOVIE_NOTIFY_ENABLE, CONFIG.TV_NOTIFY_ENABLE,
CONFIG.MUSIC_NOTIFY_ENABLE, CONFIG.NOTIFY_RECENTLY_ADDED]):
logger.info(u"Starting background notification handler.")
notification_handler.start_thread(num_threads=3)
started = True
@ -450,8 +460,8 @@ def dbcheck():
c_db.execute(
'CREATE TABLE IF NOT EXISTS notify_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, '
'session_key INTEGER, rating_key INTEGER, parent_rating_key INTEGER, grandparent_rating_key INTEGER, '
'user_id INTEGER, user TEXT, agent_id INTEGER, agent_name TEXT, notify_action TEXT, '
'subject_text TEXT, body_text TEXT, script_args TEXT, poster_url TEXT)'
'user_id INTEGER, user TEXT, notifier_id INTEGER, agent_id INTEGER, agent_name TEXT, notify_action TEXT, '
'subject_text TEXT, body_text TEXT, poster_url TEXT)'
)
# library_sections table :: This table keeps record of the servers library sections
@ -899,6 +909,15 @@ def dbcheck():
'ALTER TABLE notify_log_temp RENAME TO notify_log'
)
# Upgrade notify_log table from earlier versions
try:
c_db.execute('SELECT notifier_id FROM notify_log')
except sqlite3.OperationalError:
logger.debug(u"Altering database. Updating database table notify_log.")
c_db.execute(
'ALTER TABLE notify_log ADD COLUMN notifier_id INTEGER'
)
# Upgrade library_sections table from earlier versions (remove UNIQUE constraint on section_id)
try:
result = c_db.execute('SELECT SQL FROM sqlite_master WHERE type="table" AND name="library_sections"').fetchone()

View file

@ -74,35 +74,16 @@ class ActivityHandler(object):
session = self.get_live_session()
# 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=session, notify_action='play')).start()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=session, notify_action='on_play'))
# Write the new session to our temp session table
self.update_db_session(session=session)
# Check if any notification agents have notifications enabled
if any(d['on_concurrent'] for d in notifiers.available_notification_agents()):
# Check if any concurrent streams by the user
ip = True if plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP else None
ap = activity_processor.ActivityProcessor()
user_sessions = ap.get_session_by_user_id(user_id=session['user_id'], ip_address=ip)
if len(user_sessions) >= plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD:
# 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=session, notify_action='concurrent')).start()
# Check if any notification agents have notifications enabled
if any(d['on_newdevice'] for d in notifiers.available_notification_agents()):
# Check if any concurrent streams by the user
data_factory = datafactory.DataFactory()
user_devices = data_factory.get_user_devices(user_id=session['user_id'])
if session['machine_id'] not in user_devices:
# 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=session, notify_action='newdevice')).start()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=session, notify_action='on_concurrent'))
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=session, notify_action='on_newdevice'))
def on_stop(self, force_stop=False):
if self.is_valid_session():
@ -123,11 +104,8 @@ class ActivityHandler(object):
# Retrieve the session data from our temp table
db_session = ap.get_session_by_key(session_key=self.get_session_key())
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=db_session, notify_action='on_stop'))
# Write it to the history table
monitor_proc = activity_processor.ActivityProcessor()
@ -154,11 +132,8 @@ class ActivityHandler(object):
# Retrieve the session data from our temp table
db_session = ap.get_session_by_key(session_key=self.get_session_key())
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=db_session, notify_action='on_pause'))
def on_resume(self):
if self.is_valid_session():
@ -176,11 +151,8 @@ class ActivityHandler(object):
# Retrieve the session data from our temp table
db_session = ap.get_session_by_key(session_key=self.get_session_key())
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=db_session, notify_action='on_resume'))
def on_buffer(self):
if self.is_valid_session():
@ -209,10 +181,8 @@ class ActivityHandler(object):
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())
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=db_session, notify_action='on_buffer'))
# This function receives events from our websocket connection
def process(self):
@ -254,17 +224,9 @@ class ActivityHandler(object):
# Monitor if the stream has reached the watch percentage for notifications
# The only purpose of this is for notifications
# 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')
if this_state != 'buffering':
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=db_session, notify_action='on_watched'))
else:
# We don't have this session in our table yet, start a new one.

View file

@ -48,11 +48,9 @@ def check_active_sessions(ws_request=False):
if int_ping_count >= 3:
logger.info(u"PlexPy Monitor :: The Plex Media Server is back up.")
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
notify_action='on_intup'))
int_ping_count = 0
media_container = session_list['sessions']
@ -72,22 +70,14 @@ def check_active_sessions(ws_request=False):
if session['state'] == 'paused':
logger.debug(u"PlexPy Monitor :: Session %s has been paused." % stream['session_key'])
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_pause'))
if session['state'] == 'playing' and stream['state'] == 'paused':
logger.debug(u"PlexPy Monitor :: Session %s has been resumed." % stream['session_key'])
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_resume'))
if stream['state'] == 'paused' and not ws_request:
# The stream is still paused so we need to increment the paused_counter
@ -125,12 +115,9 @@ def check_active_sessions(ws_request=False):
'WHERE session_key = ? AND rating_key = ?',
[stream['session_key'], stream['rating_key']])
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_buffer'))
else:
# Subsequent buffer notifications after wait time
if int(time.time()) > buffer_values[0]['buffer_last_triggered'] + \
@ -143,12 +130,8 @@ def check_active_sessions(ws_request=False):
'WHERE session_key = ? AND rating_key = ?',
[stream['session_key'], stream['rating_key']])
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_buffer'))
logger.debug(u"PlexPy Monitor :: Session %s is buffering. Count is now %s. Last triggered %s."
% (stream['session_key'],
@ -158,15 +141,8 @@ def check_active_sessions(ws_request=False):
# Check if the user has reached the offset in the media we defined as the "watched" percent
# Don't trigger if state is buffer as some clients push the progress to the end when
# buffering on start.
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:
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_watched'))
else:
# The user has stopped playing a stream
@ -180,22 +156,11 @@ def check_active_sessions(ws_request=False):
'WHERE session_key = ? AND rating_key = ?',
[stream['stopped'], 'stopped', stream['session_key'], stream['rating_key']])
# Check if the user has reached the offset in the media we defined as the "watched" percent
if stream['view_offset'] and stream['duration']:
if helpers.get_percent(stream['view_offset'],
stream['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT:
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_watched'))
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=stream, notify_action='on_stop'))
# Write the item history on playback stop
success = monitor_process.write_session_history(session=stream)
@ -247,11 +212,8 @@ def check_active_sessions(ws_request=False):
% str(int_ping_count))
if int_ping_count == 3:
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
notify_action='on_intdown'))
def check_recently_added():
@ -304,11 +266,8 @@ 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']))
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
timeline_data=item, notify_action='on_created'))
else:
item = max(metadata, key=lambda x:x['added_at'])
@ -326,10 +285,9 @@ def check_recently_added():
logger.debug(u"PlexPy Monitor :: Library item %s has been added to Plex." % str(item['rating_key']))
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
timeline_data=item, notify_action='on_created'))
def check_server_response():
@ -360,19 +318,14 @@ def check_server_response():
if ext_ping_count >= 3:
logger.info(u"PlexPy Monitor :: Plex remote access is back up.")
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
notify_action='on_extup'))
ext_ping_count = 0
if ext_ping_count == 3:
# 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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
notify_action='on_extdown'))
def check_server_updates():
@ -389,10 +342,8 @@ def check_server_updates():
if download_info['update_available']:
logger.info(u"PlexPy Monitor :: PMS update available version: %s", download_info['version'])
# Check if any notification agents have notifications enabled
if any(d['on_pmsupdate'] for d in notifiers.available_notification_agents()):
# Fire off notifications
threading.Thread(target=notification_handler.notify_timeline,
kwargs=dict(notify_action='pmsupdate')).start()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
notify_action='on_pmsupdate'))
else:
logger.info(u"PlexPy Monitor :: No PMS update available.")

View file

@ -97,11 +97,10 @@ class ActivityProcessor(object):
if result == 'insert':
# Check if any notification agents have notifications enabled
if notify and any(d['on_play'] for d in notifiers.available_notification_agents()):
if notify:
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()
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=values, notify_action='on_play'))
# If it's our first write then time stamp it.
started = int(time.time())
@ -116,25 +115,11 @@ class ActivityProcessor(object):
ip_address = {'ip_address': ip_address}
self.db.upsert('sessions', ip_address, keys)
# Check if any notification agents have notifications enabled
if notify and any(d['on_concurrent'] for d in notifiers.available_notification_agents()):
# Check if any concurrent streams by the user
user_sessions = self.get_session_by_user_id(user_id=session['user_id'],
ip_address=plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP)
if len(user_sessions) >= plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD:
# 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='concurrent')).start()
# Check if any notification agents have notifications enabled
if notify and any(d['on_newdevice'] for d in notifiers.available_notification_agents()):
# Check if any concurrent streams by the user
data_factory = datafactory.DataFactory()
user_devices = data_factory.get_user_devices(user_id=session['user_id'])
if session['machine_id'] not in user_devices:
# 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='newdevice')).start()
if notify:
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=values, notify_action='on_concurrent'))
plexpy.NOTIFY_QUEUE.put(notification_handler.add_to_notify_queue(
stream_data=values, notify_action='on_newdevice'))
return True

File diff suppressed because it is too large Load diff

View file

@ -339,7 +339,7 @@ def get_notifiers(notifier_id=None, notify_action=None):
for item in result:
item['active'] = int(any([item.pop(k) for k in item.keys() if k in notify_actions]))
return sorted(result, key=lambda k: (k['agent_label'], k['id']))
return result
def delete_notifier(notifier_id=None):
@ -357,7 +357,7 @@ def get_notifier_config(notifier_id=None):
if str(notifier_id).isdigit():
notifier_id = int(notifier_id)
else:
logger.error(u"PlexPy Notifiers :: Unable to retrieve notifier config: invalid notifier_id.")
logger.error(u"PlexPy Notifiers :: Unable to retrieve notifier config: invalid notifier_id %s." % notifier_id)
return None
monitor_db = database.MonitorDatabase()
@ -381,7 +381,6 @@ def get_notifier_config(notifier_id=None):
notifier_text[k] = {'subject': result.pop(k + '_subject'),
'body': result.pop(k + '_body')}
result['agent'] = notifier_agent
result['config'] = notifier_config
result['actions'] = notifier_actions
result['notify_text'] = notifier_text
@ -393,13 +392,13 @@ def add_notifier_config(agent_id=None, **kwargs):
if str(agent_id).isdigit():
agent_id = int(agent_id)
else:
logger.error(u"PlexPy Notifiers :: Unable to add new notifier: invalid agent_id.")
logger.error(u"PlexPy Notifiers :: Unable to add new notifier: invalid agent_id %s." % agent_id)
return False
agent = next((a for a in available_notification_agents() if a['id'] == agent_id), None)
if not agent:
logger.error(u"PlexPy Notifiers :: Unable to retrieve new notification agent: invalid agent_id.")
logger.error(u"PlexPy Notifiers :: Unable to retrieve new notification agent: invalid agent_id %s." % agent_id)
return False
keys = {'id': None}
@ -433,13 +432,13 @@ def set_notifier_config(notifier_id=None, agent_id=None, **kwargs):
if str(agent_id).isdigit():
agent_id = int(agent_id)
else:
logger.error(u"PlexPy Notifiers :: Unable to set exisiting notifier: invalid agent_id.")
logger.error(u"PlexPy Notifiers :: Unable to set exisiting notifier: invalid agent_id %s." % agent_id)
return False
agent = next((a for a in available_notification_agents() if a['id'] == agent_id), None)
if not agent:
logger.error(u"PlexPy Notifiers :: Unable to retrieve existing notification agent: invalid agent_id.")
logger.error(u"PlexPy Notifiers :: Unable to retrieve existing notification agent: invalid agent_id %s." % agent_id)
return False
notify_actions = get_notify_actions()
@ -479,7 +478,7 @@ def set_notifier_config(notifier_id=None, agent_id=None, **kwargs):
def send_notification(notifier_id=None, subject='', body='', notify_action='', **kwargs):
notifier_config = get_notifier_config(notifier_id=notifier_id)
if notifier_config:
agent = notifier_config['agent']
agent = get_agent_class(notifier_config['agent_id'])
return agent.notify(subject=subject,
body=body,
action=notify_action,