Rework notify_log table to save each notification separately

This commit is contained in:
JonnyWong16 2016-02-21 15:44:21 -08:00
parent 3c6a6cdc5b
commit c93b65b299
3 changed files with 231 additions and 147 deletions

View file

@ -443,10 +443,10 @@ def dbcheck():
# notify_log table :: This is a table which logs notifications sent # notify_log table :: This is a table which logs notifications sent
c_db.execute( c_db.execute(
'CREATE TABLE IF NOT EXISTS notify_log (id INTEGER PRIMARY KEY AUTOINCREMENT, ' 'CREATE TABLE IF NOT EXISTS notify_log (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, '
'session_key INTEGER, rating_key INTEGER, user_id INTEGER, user TEXT, ' 'session_key INTEGER, rating_key INTEGER, parent_rating_key INTEGER, grandparent_rating_key INTEGER, '
'agent_id INTEGER, agent_name TEXT, on_play INTEGER, on_stop INTEGER, on_watched INTEGER, ' 'user_id INTEGER, user TEXT, agent_id INTEGER, agent_name TEXT, notify_action TEXT, '
'on_pause INTEGER, on_resume INTEGER, on_buffer INTEGER, on_created INTEGER, poster_url TEXT)' 'subject_text TEXT, body_text TEXT, script_args TEXT, poster_url TEXT)'
) )
# library_sections table :: This table keeps record of the servers library sections # library_sections table :: This table keeps record of the servers library sections
@ -724,7 +724,7 @@ def dbcheck():
'ALTER TABLE notify_log ADD COLUMN on_created INTEGER' 'ALTER TABLE notify_log ADD COLUMN on_created INTEGER'
) )
# Upgrade session_history_metadata table from earlier versions # Upgrade notify_log table from earlier versions
try: try:
c_db.execute('SELECT poster_url FROM notify_log') c_db.execute('SELECT poster_url FROM notify_log')
except sqlite3.OperationalError: except sqlite3.OperationalError:
@ -733,6 +733,44 @@ def dbcheck():
'ALTER TABLE notify_log ADD COLUMN poster_url TEXT' 'ALTER TABLE notify_log ADD COLUMN poster_url TEXT'
) )
# Upgrade notify_log table from earlier versions (populate table with data from notify_log)
try:
c_db.execute('SELECT timestamp FROM notify_log')
except sqlite3.OperationalError:
logger.debug(u"Altering database. Updating database table notify_log.")
c_db.execute(
'CREATE TABLE IF NOT EXISTS notify_log_temp (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)'
)
c_db.execute(
'INSERT INTO notify_log_temp (session_key, rating_key, user_id, user, agent_id, agent_name, '
'poster_url, timestamp, notify_action) '
'SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, timestamp, '
'notify_action FROM notify_log_temp '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_play, "play" FROM notify_log WHERE on_play '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_stop, "stop" FROM notify_log WHERE on_stop '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_watched, "watched" FROM notify_log WHERE on_watched '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_pause, "pause" FROM notify_log WHERE on_pause '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_resume, "resume" FROM notify_log WHERE on_resume '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_buffer, "buffer" FROM notify_log WHERE on_buffer '
'UNION ALL SELECT session_key, rating_key, user_id, user, agent_id, agent_name, poster_url, '
'on_created, "created" FROM notify_log WHERE on_created '
'ORDER BY timestamp ')
c_db.execute(
'DROP TABLE notify_log'
)
c_db.execute(
'ALTER TABLE notify_log_temp RENAME TO notify_log'
)
# Upgrade library_sections table from earlier versions (remove UNIQUE constraint on section_id) # Upgrade library_sections table from earlier versions (remove UNIQUE constraint on section_id)
try: try:
result = c_db.execute('SELECT SQL FROM sqlite_master WHERE type="table" AND name="library_sections"').fetchone() result = c_db.execute('SELECT SQL FROM sqlite_master WHERE type="table" AND name="library_sections"').fetchone()

View file

@ -860,8 +860,9 @@ class DataFactory(object):
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
if rating_key: if rating_key:
query = 'SELECT id, poster_url FROM notify_log WHERE rating_key = %d ' \ query = 'SELECT id, poster_url FROM notify_log ' \
'ORDER BY id DESC LIMIT 1' % int(rating_key) 'WHERE rating_key = %d OR parent_rating_key = %d OR grandparent_rating_key = %d ' \
'ORDER BY id DESC LIMIT 1' % (int(rating_key), int(rating_key), int(rating_key))
result = monitor_db.select(query) result = monitor_db.select(query)
else: else:
return None return None

View file

@ -50,168 +50,201 @@ def notify(stream_data=None, notify_action=None):
for agent in notifiers.available_notification_agents(): for agent in notifiers.available_notification_agents():
if agent['on_play'] and notify_action == 'play': if agent['on_play'] and notify_action == 'play':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_stop'] and notify_action == 'stop' \ elif agent['on_stop'] and notify_action == 'stop' \
and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < plexpy.CONFIG.NOTIFY_WATCHED_PERCENT): and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < plexpy.CONFIG.NOTIFY_WATCHED_PERCENT):
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) # Set the notification state in the db
set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_pause'] and notify_action == 'pause' \ elif agent['on_pause'] and notify_action == 'pause' \
and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99): and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99):
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) # Set the notification state in the db
set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_resume'] and notify_action == 'resume' \ elif agent['on_resume'] and notify_action == 'resume' \
and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99): and (plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99):
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) # Set the notification state in the db
set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_buffer'] and notify_action == 'buffer': elif agent['on_buffer'] and notify_action == 'buffer':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
script_args=notify_strings[2],
notify_action=notify_action, notify_action=notify_action,
script_args=metadata) metadata=metadata)
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) # Set the notification state in the db
set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_watched'] and notify_action == 'watched': elif agent['on_watched'] and notify_action == 'watched':
# Get the current states for notifications from our db # Get the current states for notifications from our db
notify_states = get_notify_state(session=stream_data) notify_states = get_notify_state(session=stream_data)
# If there is nothing in the notify_log for our agent id but it is enabled we should notify # If there is nothing in the notify_log for our agent id but it is enabled we should notify
if not any(d['agent_id'] == agent['id'] for d in notify_states): if not any(d['agent_id'] == agent['id'] and d['notify_action'] == notify_action for d in notify_states):
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
else:
# Check in our notify log if the notification has already been sent
for notify_state in notify_states:
if not notify_state['on_watched'] and (notify_state['agent_id'] == agent['id']):
# Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action)
notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0],
body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2], agent_info=agent,
notify_strings=notify_strings,
metadata=metadata) metadata=metadata)
# Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata)
elif (stream_data['media_type'] == 'track' and plexpy.CONFIG.MUSIC_NOTIFY_ENABLE): elif (stream_data['media_type'] == 'track' and plexpy.CONFIG.MUSIC_NOTIFY_ENABLE):
for agent in notifiers.available_notification_agents(): for agent in notifiers.available_notification_agents():
if agent['on_play'] and notify_action == 'play': if agent['on_play'] and notify_action == 'play':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_stop'] and notify_action == 'stop': elif agent['on_stop'] and notify_action == 'stop':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_pause'] and notify_action == 'pause': elif agent['on_pause'] and notify_action == 'pause':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_resume'] and notify_action == 'resume': elif agent['on_resume'] and notify_action == 'resume':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif agent['on_buffer'] and notify_action == 'buffer': elif agent['on_buffer'] and notify_action == 'buffer':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(session=stream_data, state=notify_action) notify_strings, metadata = build_notify_text(session=stream_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=stream_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif stream_data['media_type'] == 'clip': elif stream_data['media_type'] == 'clip':
pass pass
@ -227,128 +260,134 @@ def notify_timeline(timeline_data=None, notify_action=None):
for agent in notifiers.available_notification_agents(): for agent in notifiers.available_notification_agents():
if agent['on_created'] and notify_action == 'created': if agent['on_created'] and notify_action == 'created':
# Build and send notification # Build and send notification
notify_strings, metadata = build_notify_text(timeline=timeline_data, state=notify_action) notify_strings, metadata = build_notify_text(timeline=timeline_data, notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action,
script_args=notify_strings[2], script_args=notify_strings[2],
notify_action=notify_action,
metadata=metadata) metadata=metadata)
# Set the notification state in the db # Set the notification state in the db
set_notify_state(session=timeline_data, state=notify_action, agent_info=agent, metadata=metadata) set_notify_state(session=stream_data,
notify_action=notify_action,
agent_info=agent,
notify_strings=notify_strings,
metadata=metadata)
elif not timeline_data and notify_action: elif not timeline_data and notify_action:
for agent in notifiers.available_notification_agents(): for agent in notifiers.available_notification_agents():
if agent['on_extdown'] and notify_action == 'extdown': if agent['on_extdown'] and notify_action == 'extdown':
# Build and send notification # Build and send notification
notify_strings = build_server_notify_text(state=notify_action) notify_strings = build_server_notify_text(notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
script_args=notify_strings[2],
notify_action=notify_action)
# Set the notification state in the db
set_notify_state(session=None,
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) agent_info=agent,
notify_strings=notify_strings,
metadata=None)
if agent['on_intdown'] and notify_action == 'intdown': if agent['on_intdown'] and notify_action == 'intdown':
# Build and send notification # Build and send notification
notify_strings = build_server_notify_text(state=notify_action) notify_strings = build_server_notify_text(notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
script_args=notify_strings[2],
notify_action=notify_action)
# Set the notification state in the db
set_notify_state(session=None,
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) agent_info=agent,
notify_strings=notify_strings,
metadata=None)
if agent['on_extup'] and notify_action == 'extup': if agent['on_extup'] and notify_action == 'extup':
# Build and send notification # Build and send notification
notify_strings = build_server_notify_text(state=notify_action) notify_strings = build_server_notify_text(notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
script_args=notify_strings[2],
notify_action=notify_action)
# Set the notification state in the db
set_notify_state(session=None,
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) agent_info=agent,
notify_strings=notify_strings,
metadata=None)
if agent['on_intup'] and notify_action == 'intup': if agent['on_intup'] and notify_action == 'intup':
# Build and send notification # Build and send notification
notify_strings = build_server_notify_text(state=notify_action) notify_strings = build_server_notify_text(notify_action=notify_action)
notifiers.send_notification(agent_id=agent['id'], notifiers.send_notification(agent_id=agent['id'],
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
script_args=notify_strings[2],
notify_action=notify_action)
# Set the notification state in the db
set_notify_state(session=None,
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) agent_info=agent,
notify_strings=notify_strings,
metadata=None)
else: else:
logger.debug(u"PlexPy NotificationHandler :: Notify timeline called but incomplete data received.") logger.debug(u"PlexPy NotificationHandler :: Notify timeline called but incomplete data received.")
def get_notify_state(session): def get_notify_state(session):
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
result = monitor_db.select('SELECT on_play, on_stop, on_pause, on_resume, on_buffer, on_watched, agent_id ' result = monitor_db.select('SELECT timestamp, notify_action, agent_id '
'FROM notify_log ' 'FROM notify_log '
'WHERE session_key = ? ' 'WHERE session_key = ? '
'AND rating_key = ? ' 'AND rating_key = ? '
'AND user = ? ' 'AND user_id = ? '
'ORDER BY id DESC', 'ORDER BY id DESC',
args=[session['session_key'], session['rating_key'], session['user']]) args=[session['session_key'], session['rating_key'], session['user_id']])
notify_states = [] notify_states = []
for item in result: for item in result:
notify_state = {'on_play': item['on_play'], notify_state = {'timestamp': item['timestamp'],
'on_stop': item['on_stop'], 'notify_action': item['notify_action'],
'on_pause': item['on_pause'],
'on_resume': item['on_resume'],
'on_buffer': item['on_buffer'],
'on_watched': item['on_watched'],
'agent_id': item['agent_id']} 'agent_id': item['agent_id']}
notify_states.append(notify_state) notify_states.append(notify_state)
return notify_states return notify_states
def get_notify_state_timeline(timeline): def set_notify_state(session, notify_action, agent_info, notify_strings, metadata):
if notify_action and agent_info:
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
result = monitor_db.select('SELECT on_created, agent_id '
'FROM notify_log '
'WHERE rating_key = ? '
'ORDER BY id DESC',
args=[timeline['rating_key']])
notify_states = []
for item in result:
notify_state = {'on_created': item['on_created'],
'agent_id': item['agent_id']}
notify_states.append(notify_state)
return notify_states if notify_strings[2]:
script_args = '[' + ', '.join(notify_strings[2]) + ']'
def set_notify_state(session, state, agent_info, metadata):
if session and state and agent_info:
monitor_db = database.MonitorDatabase()
notify_time = int(time.time())
if state == 'play':
values = {'on_play': notify_time}
elif state == 'stop':
values = {'on_stop': notify_time}
elif state == 'pause':
values = {'on_pause': notify_time}
elif state == 'resume':
values = {'on_resume': notify_time}
elif state == 'buffer':
values = {'on_buffer': notify_time}
elif state == 'watched':
values = {'on_watched': notify_time}
elif state == 'created':
values = {'on_created': notify_time}
else: else:
return script_args = None
if state == 'created': keys = {'timestamp': int(time.time()),
keys = {'rating_key': session['rating_key'], 'session_key': session.get('session_key', None),
'agent_id': agent_info['id'], 'rating_key': session.get('rating_key', None),
'agent_name': agent_info['name'], 'user_id': session.get('user_id', None),
'poster_url': metadata.get('poster_url', None)}
else:
keys = {'session_key': session['session_key'],
'rating_key': session['rating_key'],
'user_id': session['user_id'],
'user': session['user'],
'agent_id': agent_info['id'], 'agent_id': agent_info['id'],
'notify_action': notify_action}
values = {'parent_rating_key': session.get('parent_rating_key', None),
'grandparent_rating_key': session.get('grandparent_rating_key', None),
'user': session.get('user', None),
'agent_name': agent_info['name'], 'agent_name': agent_info['name'],
'subject_text': notify_strings[0],
'body_text': notify_strings[1],
'script_args': script_args,
'poster_url': metadata.get('poster_url', None)} 'poster_url': metadata.get('poster_url', None)}
monitor_db.upsert(table_name='notify_log', key_dict=keys, value_dict=values) monitor_db.upsert(table_name='notify_log', key_dict=keys, value_dict=values)
@ -356,7 +395,7 @@ def set_notify_state(session, state, agent_info, metadata):
logger.error(u"PlexPy NotificationHandler :: Unable to set notify state.") logger.error(u"PlexPy NotificationHandler :: Unable to set notify state.")
def build_notify_text(session=None, timeline=None, state=None): def build_notify_text(session=None, timeline=None, notify_action=None):
# Get time formats # Get time formats
date_format = plexpy.CONFIG.DATE_FORMAT.replace('Do','').replace('zz','') date_format = plexpy.CONFIG.DATE_FORMAT.replace('Do','').replace('zz','')
time_format = plexpy.CONFIG.TIME_FORMAT.replace('Do','').replace('zz','') time_format = plexpy.CONFIG.TIME_FORMAT.replace('Do','').replace('zz','')
@ -462,7 +501,7 @@ def build_notify_text(session=None, timeline=None, state=None):
else: else:
transcode_decision = 'Direct Play' transcode_decision = 'Direct Play'
if state != 'play': if notify_action != 'play':
stream_duration = int((time.time() - stream_duration = int((time.time() -
helpers.cast_to_int(session.get('started', 0)) - helpers.cast_to_int(session.get('started', 0)) -
helpers.cast_to_int(session.get('paused_counter', 0))) / 60) helpers.cast_to_int(session.get('paused_counter', 0))) / 60)
@ -507,17 +546,23 @@ def build_notify_text(session=None, timeline=None, state=None):
if metadata['media_type'] == 'movie' or metadata['media_type'] == 'show' or metadata['media_type'] == 'artist': if metadata['media_type'] == 'movie' or metadata['media_type'] == 'show' or metadata['media_type'] == 'artist':
thumb = metadata['thumb'] thumb = metadata['thumb']
poster_key = metadata['rating_key']
poster_title = metadata['title']
elif metadata['media_type'] == 'episode': elif metadata['media_type'] == 'episode':
thumb = metadata['grandparent_thumb'] thumb = metadata['grandparent_thumb']
poster_key = metadata['grandparent_rating_key']
poster_title = metadata['grandparent_title']
elif metadata['media_type'] == 'track': elif metadata['media_type'] == 'track':
thumb = metadata['parent_thumb'] thumb = metadata['parent_thumb']
poster_key = metadata['parent_rating_key']
poster_title = metadata['parent_title']
else: else:
thumb = None thumb = None
if thumb: if thumb:
# Try to retrieve a poster_url from the database # Try to retrieve a poster_url from the database
data_factory = datafactory.DataFactory() data_factory = datafactory.DataFactory()
poster_url = data_factory.get_poster_url(rating_key=rating_key) poster_url = data_factory.get_poster_url(rating_key=poster_key)
# If no previous poster_url # If no previous poster_url
if not poster_url: if not poster_url:
@ -525,12 +570,12 @@ def build_notify_text(session=None, timeline=None, state=None):
urllib.urlretrieve(plexpy.CONFIG.PMS_URL + thumb + '?X-Plex-Token=' + plexpy.CONFIG.PMS_TOKEN, urllib.urlretrieve(plexpy.CONFIG.PMS_URL + thumb + '?X-Plex-Token=' + plexpy.CONFIG.PMS_TOKEN,
os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster.jpg')) os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster.jpg'))
# Upload thumb to Imgur and get link # Upload thumb to Imgur and get link
poster_url = helpers.uploadToImgur(os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster.jpg'), full_title) poster_url = helpers.uploadToImgur(os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster.jpg'), poster_title)
metadata['poster_url'] = poster_url metadata['poster_url'] = poster_url
# Fix metadata params for notify recently added grandparent # Fix metadata params for notify recently added grandparent
if state == 'created' and plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT: if notify_action == 'created' and plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT:
show_name = metadata['title'] show_name = metadata['title']
episode_name = '' episode_name = ''
artist_name = metadata['title'] artist_name = metadata['title']
@ -546,7 +591,7 @@ def build_notify_text(session=None, timeline=None, state=None):
available_params = {# Global paramaters available_params = {# Global paramaters
'server_name': server_name, 'server_name': server_name,
'server_uptime': server_uptime, 'server_uptime': server_uptime,
'action': state.title(), 'action': notify_action.title(),
'datestamp': arrow.now().format(date_format), 'datestamp': arrow.now().format(date_format),
'timestamp': arrow.now().format(time_format), 'timestamp': arrow.now().format(time_format),
# Stream parameters # Stream parameters
@ -639,7 +684,7 @@ def build_notify_text(session=None, timeline=None, state=None):
except Exception as e: except Exception as e:
logger.error(u"PlexPy Notifier :: Unable to parse custom script arguments %s. Using fallback." % e) logger.error(u"PlexPy Notifier :: Unable to parse custom script arguments %s. Using fallback." % e)
if state == 'play': if notify_action == 'play':
# Default body text # Default body text
body_text = '%s (%s) started playing %s' % (session['friendly_name'], body_text = '%s (%s) started playing %s' % (session['friendly_name'],
session['player'], session['player'],
@ -663,7 +708,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
else: else:
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
elif state == 'stop': elif notify_action == 'stop':
# Default body text # Default body text
body_text = '%s (%s) has stopped %s' % (session['friendly_name'], body_text = '%s (%s) has stopped %s' % (session['friendly_name'],
session['player'], session['player'],
@ -687,7 +732,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
else: else:
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
elif state == 'pause': elif notify_action == 'pause':
# Default body text # Default body text
body_text = '%s (%s) has paused %s' % (session['friendly_name'], body_text = '%s (%s) has paused %s' % (session['friendly_name'],
session['player'], session['player'],
@ -711,7 +756,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
else: else:
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
elif state == 'resume': elif notify_action == 'resume':
# Default body text # Default body text
body_text = '%s (%s) has resumed %s' % (session['friendly_name'], body_text = '%s (%s) has resumed %s' % (session['friendly_name'],
session['player'], session['player'],
@ -735,7 +780,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
else: else:
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
elif state == 'buffer': elif notify_action == 'buffer':
# Default body text # Default body text
body_text = '%s (%s) is buffering %s' % (session['friendly_name'], body_text = '%s (%s) is buffering %s' % (session['friendly_name'],
session['player'], session['player'],
@ -759,7 +804,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
else: else:
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
elif state == 'watched': elif notify_action == 'watched':
# Default body text # Default body text
body_text = '%s (%s) has watched %s' % (session['friendly_name'], body_text = '%s (%s) has watched %s' % (session['friendly_name'],
session['player'], session['player'],
@ -783,7 +828,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
else: else:
return [subject_text, body_text, script_args], metadata return [subject_text, body_text, script_args], metadata
elif state == 'created': elif notify_action == 'created':
# Default body text # Default body text
body_text = '%s was recently added to Plex.' % full_title body_text = '%s was recently added to Plex.' % full_title
@ -809,7 +854,7 @@ def build_notify_text(session=None, timeline=None, state=None):
return None return None
def build_server_notify_text(state=None): def build_server_notify_text(notify_action=None):
# Get time formats # Get time formats
date_format = plexpy.CONFIG.DATE_FORMAT.replace('Do','').replace('zz','') date_format = plexpy.CONFIG.DATE_FORMAT.replace('Do','').replace('zz','')
time_format = plexpy.CONFIG.TIME_FORMAT.replace('Do','').replace('zz','') time_format = plexpy.CONFIG.TIME_FORMAT.replace('Do','').replace('zz','')
@ -841,7 +886,7 @@ def build_server_notify_text(state=None):
available_params = {# Global paramaters available_params = {# Global paramaters
'server_name': server_name, 'server_name': server_name,
'server_uptime': server_uptime, 'server_uptime': server_uptime,
'action': state.title(), 'action': notify_action.title(),
'datestamp': arrow.now().format(date_format), 'datestamp': arrow.now().format(date_format),
'timestamp': arrow.now().format(time_format)} 'timestamp': arrow.now().format(time_format)}
@ -859,7 +904,7 @@ def build_server_notify_text(state=None):
except Exception as e: except Exception as e:
logger.error(u"PlexPy Notifier :: Unable to parse custom script arguments %s. Using fallback." % e) logger.error(u"PlexPy Notifier :: Unable to parse custom script arguments %s. Using fallback." % e)
if state == 'extdown': if notify_action == 'extdown':
# Default body text # Default body text
body_text = 'The Plex Media Server remote access is down.' body_text = 'The Plex Media Server remote access is down.'
@ -882,7 +927,7 @@ def build_server_notify_text(state=None):
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args]
elif state == 'intdown': elif notify_action == 'intdown':
# Default body text # Default body text
body_text = 'The Plex Media Server is down.' body_text = 'The Plex Media Server is down.'
@ -904,7 +949,7 @@ def build_server_notify_text(state=None):
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args]
if state == 'extup': if notify_action == 'extup':
# Default body text # Default body text
body_text = 'The Plex Media Server remote access is back up.' body_text = 'The Plex Media Server remote access is back up.'
@ -926,7 +971,7 @@ def build_server_notify_text(state=None):
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args]
elif state == 'intup': elif notify_action == 'intup':
# Default body text # Default body text
body_text = 'The Plex Media Server is back up.' body_text = 'The Plex Media Server is back up.'