mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-12 00:06:07 -07:00
Rework notification logic to only build parameters once per action
* Instead of rebuilding for each notification agent * Change season/episode to use season poster * Change album/track to use album art
This commit is contained in:
parent
f45bd49421
commit
ffdd9c9cbf
7 changed files with 199 additions and 181 deletions
|
@ -88,9 +88,6 @@ class ActivityHandler(object):
|
||||||
# Write the new session to our temp session table
|
# Write the new session to our temp session table
|
||||||
self.update_db_session(session=session)
|
self.update_db_session(session=session)
|
||||||
|
|
||||||
plexpy.NOTIFY_QUEUE.put({'stream_data': session, 'notify_action': 'on_concurrent'})
|
|
||||||
plexpy.NOTIFY_QUEUE.put({'stream_data': session, 'notify_action': 'on_newdevice'})
|
|
||||||
|
|
||||||
def on_stop(self, force_stop=False):
|
def on_stop(self, force_stop=False):
|
||||||
if self.is_valid_session():
|
if self.is_valid_session():
|
||||||
logger.debug(u"PlexPy ActivityHandler :: Session %s stopped." % str(self.get_session_key()))
|
logger.debug(u"PlexPy ActivityHandler :: Session %s stopped." % str(self.get_session_key()))
|
||||||
|
|
|
@ -47,7 +47,7 @@ def check_active_sessions(ws_request=False):
|
||||||
media_container = session_list['sessions']
|
media_container = session_list['sessions']
|
||||||
|
|
||||||
# Check our temp table for what we must do with the new streams
|
# Check our temp table for what we must do with the new streams
|
||||||
db_streams = monitor_db.select('SELECT * FROM sessions')
|
db_streams = monitor_process.get_sessions()
|
||||||
for stream in db_streams:
|
for stream in db_streams:
|
||||||
if any(d['session_key'] == str(stream['session_key']) and d['rating_key'] == str(stream['rating_key'])
|
if any(d['session_key'] == str(stream['session_key']) and d['rating_key'] == str(stream['rating_key'])
|
||||||
for d in media_container):
|
for d in media_container):
|
||||||
|
|
|
@ -106,10 +106,6 @@ class ActivityProcessor(object):
|
||||||
timestamp = {'started': started}
|
timestamp = {'started': started}
|
||||||
self.db.upsert('sessions', timestamp, keys)
|
self.db.upsert('sessions', timestamp, keys)
|
||||||
|
|
||||||
if notify:
|
|
||||||
plexpy.NOTIFY_QUEUE.put({'stream_data': values, 'notify_action': 'on_concurrent'})
|
|
||||||
plexpy.NOTIFY_QUEUE.put({'stream_data': values, 'notify_action': 'on_newdevice'})
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def write_session_history(self, session=None, import_metadata=None, is_import=False, import_ignore_interval=0):
|
def write_session_history(self, session=None, import_metadata=None, is_import=False, import_ignore_interval=0):
|
||||||
|
@ -456,14 +452,20 @@ class ActivityProcessor(object):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_session_by_user_id(self, user_id=None, ip_address=None):
|
def get_sessions(self):
|
||||||
sessions = []
|
sessions = self.db.select('SELECT * FROM sessions')
|
||||||
|
return sessions
|
||||||
|
|
||||||
|
def get_sessions(self, user_id=None, ip_address=None):
|
||||||
|
query = 'SELECT * FROM sessions'
|
||||||
|
args = []
|
||||||
|
|
||||||
if str(user_id).isdigit():
|
if str(user_id).isdigit():
|
||||||
ip = ' GROUP BY ip_address' if ip_address else ''
|
ip = ' GROUP BY ip_address' if ip_address else ''
|
||||||
sessions = self.db.select('SELECT * '
|
query += ' WHERE user_id = ?' + ip
|
||||||
'FROM sessions '
|
args.append(user_id)
|
||||||
'WHERE user_id = ? %s' % ip,
|
|
||||||
[user_id])
|
sessions = self.db.select(query, args)
|
||||||
return sessions
|
return sessions
|
||||||
|
|
||||||
def set_temp_stopped(self):
|
def set_temp_stopped(self):
|
||||||
|
|
|
@ -1018,25 +1018,27 @@ class DataFactory(object):
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
poster_url = ''
|
poster_url = ''
|
||||||
poster_key = ''
|
poster_key = []
|
||||||
|
|
||||||
if rating_key:
|
if rating_key:
|
||||||
poster_key = rating_key
|
where_key = 'WHERE rating_key = ? OR parent_rating_key = ? OR grandparent_rating_key = ?'
|
||||||
|
poster_key = [rating_key, rating_key, rating_key]
|
||||||
elif metadata:
|
elif metadata:
|
||||||
if metadata['media_type'] == 'movie' or metadata['media_type'] == 'show' or \
|
if metadata['media_type'] in ('movie', 'show', 'artist'):
|
||||||
metadata['media_type'] == 'artist' or metadata['media_type'] == 'album':
|
where_key = 'WHERE rating_key = ?'
|
||||||
poster_key = metadata['rating_key']
|
poster_key = [metadata['rating_key']]
|
||||||
elif metadata['media_type'] == 'episode':
|
elif metadata['media_type'] in ('season', 'album'):
|
||||||
poster_key = metadata['grandparent_rating_key']
|
where_key = 'WHERE parent_rating_key = ?'
|
||||||
elif metadata['media_type'] == 'season' or metadata['media_type'] == 'track':
|
poster_key = [metadata['rating_key']]
|
||||||
poster_key = metadata['parent_rating_key']
|
elif metadata['media_type'] in ('episode', 'track'):
|
||||||
|
where_key = 'WHERE parent_rating_key = ?'
|
||||||
|
poster_key = [metadata['parent_rating_key']]
|
||||||
|
|
||||||
if poster_key:
|
if poster_key:
|
||||||
try:
|
try:
|
||||||
query = 'SELECT id, poster_url FROM notify_log ' \
|
query = 'SELECT id, poster_url FROM notify_log %s ' \
|
||||||
'WHERE rating_key = %d OR parent_rating_key = %d OR grandparent_rating_key = %d ' \
|
'ORDER BY id DESC LIMIT 1' % where_key
|
||||||
'ORDER BY id DESC LIMIT 1' % (int(poster_key), int(poster_key), int(poster_key))
|
result = monitor_db.select(query, args=poster_key)
|
||||||
result = monitor_db.select(query)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn(u"PlexPy DataFactory :: Unable to execute database query for get_poster_url: %s." % e)
|
logger.warn(u"PlexPy DataFactory :: Unable to execute database query for get_poster_url: %s." % e)
|
||||||
return poster_url
|
return poster_url
|
||||||
|
|
|
@ -36,6 +36,7 @@ import plextv
|
||||||
import pmsconnect
|
import pmsconnect
|
||||||
import users
|
import users
|
||||||
|
|
||||||
|
|
||||||
def process_queue():
|
def process_queue():
|
||||||
queue = plexpy.NOTIFY_QUEUE
|
queue = plexpy.NOTIFY_QUEUE
|
||||||
while True:
|
while True:
|
||||||
|
@ -64,21 +65,52 @@ def add_notifier_each(notify_action=None, stream_data=None, timeline_data=None,
|
||||||
# Check if any notification agents have notifications enabled for the action
|
# Check if any notification agents have notifications enabled for the action
|
||||||
notifiers_enabled = notifiers.get_notifiers(notify_action=notify_action)
|
notifiers_enabled = notifiers.get_notifiers(notify_action=notify_action)
|
||||||
|
|
||||||
|
# Check on_watched for each notifier
|
||||||
|
if notifiers_enabled and notify_action == 'on_watched':
|
||||||
|
for n, notifier in enumerate(notifiers_enabled):
|
||||||
|
if any(d['agent_id'] == notifier['agent_id'] and d['notify_action'] == notify_action
|
||||||
|
for d in get_notify_state(session=stream_data)):
|
||||||
|
# Already notified on_watched, remove from notifier
|
||||||
|
notifiers_enabled.pop(n)
|
||||||
|
|
||||||
if notifiers_enabled:
|
if notifiers_enabled:
|
||||||
for notifier in notifiers_enabled:
|
|
||||||
# Check if notification conditions are satisfied
|
# Check if notification conditions are satisfied
|
||||||
conditions = notify_conditions(notifier=notifier,
|
conditions = notify_conditions(notify_action=notify_action,
|
||||||
notify_action=notify_action,
|
|
||||||
stream_data=stream_data,
|
stream_data=stream_data,
|
||||||
timeline_data=timeline_data)
|
timeline_data=timeline_data)
|
||||||
if conditions:
|
|
||||||
|
if notifiers_enabled and conditions:
|
||||||
|
if stream_data or timeline_data:
|
||||||
|
# Build the notification parameters
|
||||||
|
parameters = build_media_notify_params(notify_action=notify_action,
|
||||||
|
session=stream_data,
|
||||||
|
timeline=timeline_data,
|
||||||
|
**kwargs)
|
||||||
|
else:
|
||||||
|
# Build the notification parameters
|
||||||
|
parameters = build_server_notify_params(notify_action=notify_action,
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
|
if not parameters:
|
||||||
|
logger.error(u"PlexPy NotificationHandler :: Failed to build notification parameters.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Add each notifier to the queue
|
||||||
|
for notifier in notifiers_enabled:
|
||||||
data = {'notifier_id': notifier['id'],
|
data = {'notifier_id': notifier['id'],
|
||||||
'notify_action': notify_action,
|
'notify_action': notify_action,
|
||||||
'stream_data': stream_data,
|
'stream_data': stream_data,
|
||||||
'timeline_data': timeline_data}
|
'timeline_data': timeline_data,
|
||||||
|
'parameters': parameters}
|
||||||
data.update(kwargs)
|
data.update(kwargs)
|
||||||
plexpy.NOTIFY_QUEUE.put(data)
|
plexpy.NOTIFY_QUEUE.put(data)
|
||||||
|
|
||||||
|
# Add on_concurrent and on_newdevice to queue if action is on_play
|
||||||
|
if notify_action == 'on_play':
|
||||||
|
plexpy.NOTIFY_QUEUE.put({'stream_data': stream_data, 'notify_action': 'on_concurrent'})
|
||||||
|
plexpy.NOTIFY_QUEUE.put({'stream_data': stream_data, 'notify_action': 'on_newdevice'})
|
||||||
|
|
||||||
|
|
||||||
def notify_conditions(notifier=None, notify_action=None, stream_data=None, timeline_data=None):
|
def notify_conditions(notifier=None, notify_action=None, stream_data=None, timeline_data=None):
|
||||||
if stream_data:
|
if stream_data:
|
||||||
# Check if notifications enabled for user and library
|
# Check if notifications enabled for user and library
|
||||||
|
@ -100,13 +132,9 @@ def notify_conditions(notifier=None, notify_action=None, stream_data=None, timel
|
||||||
|
|
||||||
progress_percent = helpers.get_percent(stream_data['view_offset'], stream_data['duration'])
|
progress_percent = helpers.get_percent(stream_data['view_offset'], stream_data['duration'])
|
||||||
|
|
||||||
|
ap = activity_processor.ActivityProcessor()
|
||||||
pms_connect = pmsconnect.PmsConnect()
|
user_sessions = ap.get_sessions(user_id=stream_data['user_id'],
|
||||||
current_activity = pms_connect.get_current_activity()
|
ip_address=plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP)
|
||||||
sessions = current_activity.get('sessions', [])
|
|
||||||
user_stream_count = [d for d in sessions if d['user_id'] == stream_data['user_id']]
|
|
||||||
if plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP:
|
|
||||||
user_stream_count = set(d['ip_address'] for d in user_stream_count)
|
|
||||||
|
|
||||||
data_factory = datafactory.DataFactory()
|
data_factory = datafactory.DataFactory()
|
||||||
user_devices = data_factory.get_user_devices(user_id=stream_data['user_id'])
|
user_devices = data_factory.get_user_devices(user_id=stream_data['user_id'])
|
||||||
|
@ -114,9 +142,7 @@ def notify_conditions(notifier=None, notify_action=None, stream_data=None, timel
|
||||||
conditions = \
|
conditions = \
|
||||||
{'on_stop': plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < plexpy.CONFIG.NOTIFY_WATCHED_PERCENT,
|
{'on_stop': plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < plexpy.CONFIG.NOTIFY_WATCHED_PERCENT,
|
||||||
'on_resume': plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99,
|
'on_resume': plexpy.CONFIG.NOTIFY_CONSECUTIVE or progress_percent < 99,
|
||||||
'on_watched': not any(d['agent_id'] == notifier['agent_id'] and d['notify_action'] == notify_action
|
'on_concurrent': len(user_sessions) >= plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD,
|
||||||
for d in get_notify_state(session=stream_data)),
|
|
||||||
'on_concurrent': len(user_stream_count) >= plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD,
|
|
||||||
'on_newdevice': stream_data['machine_id'] not in user_devices
|
'on_newdevice': stream_data['machine_id'] not in user_devices
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,36 +153,17 @@ def notify_conditions(notifier=None, notify_action=None, stream_data=None, timel
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
elif timeline_data:
|
elif timeline_data:
|
||||||
|
return True
|
||||||
conditions = \
|
|
||||||
{'on_created': True}
|
|
||||||
|
|
||||||
return conditions.get(notify_action, True)
|
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data=None, **kwargs):
|
def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data=None, parameters=None, **kwargs):
|
||||||
notifier_config = notifiers.get_notifier_config(notifier_id=notifier_id)
|
notifier_config = notifiers.get_notifier_config(notifier_id=notifier_id)
|
||||||
|
|
||||||
if not notifier_config:
|
if not notifier_config:
|
||||||
return
|
return
|
||||||
|
|
||||||
if stream_data or timeline_data:
|
|
||||||
# Build the notification parameters
|
|
||||||
parameters, metadata = build_media_notify_params(notify_action=notify_action,
|
|
||||||
session=stream_data,
|
|
||||||
timeline=timeline_data,
|
|
||||||
**kwargs)
|
|
||||||
else:
|
|
||||||
# Build the notification parameters
|
|
||||||
parameters, metadata = build_server_notify_params(notify_action=notify_action,
|
|
||||||
**kwargs)
|
|
||||||
|
|
||||||
if not parameters:
|
|
||||||
logger.error(u"PlexPy NotificationHandler :: Failed to build notification parameters.")
|
|
||||||
return
|
|
||||||
|
|
||||||
# Get the subject and body strings
|
# Get the subject and body strings
|
||||||
subject_string = notifier_config['notify_text'][notify_action]['subject']
|
subject_string = notifier_config['notify_text'][notify_action]['subject']
|
||||||
body_string = notifier_config['notify_text'][notify_action]['body']
|
body_string = notifier_config['notify_text'][notify_action]['body']
|
||||||
|
@ -173,7 +180,7 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data
|
||||||
subject=subject,
|
subject=subject,
|
||||||
body=body,
|
body=body,
|
||||||
notify_action=notify_action,
|
notify_action=notify_action,
|
||||||
metadata=metadata)
|
parameters=parameters)
|
||||||
|
|
||||||
# Set the notification state in the db
|
# Set the notification state in the db
|
||||||
set_notify_state(session=stream_data or timeline_data,
|
set_notify_state(session=stream_data or timeline_data,
|
||||||
|
@ -181,7 +188,7 @@ def notify(notifier_id=None, notify_action=None, stream_data=None, timeline_data
|
||||||
notifier=notifier_config,
|
notifier=notifier_config,
|
||||||
subject=subject,
|
subject=subject,
|
||||||
body=body,
|
body=body,
|
||||||
metadata=metadata)
|
parameters=parameters)
|
||||||
|
|
||||||
|
|
||||||
def get_notify_state(session):
|
def get_notify_state(session):
|
||||||
|
@ -203,13 +210,13 @@ def get_notify_state(session):
|
||||||
return notify_states
|
return notify_states
|
||||||
|
|
||||||
|
|
||||||
def set_notify_state(notify_action, notifier, subject, body, session=None, metadata=None):
|
def set_notify_state(notify_action, notifier, subject, body, session=None, parameters=None):
|
||||||
|
|
||||||
if notify_action and notifier:
|
if notify_action and notifier:
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
session = session or {}
|
session = session or {}
|
||||||
metadata = metadata or {}
|
parameters = parameters or {}
|
||||||
|
|
||||||
keys = {'timestamp': int(time.time()),
|
keys = {'timestamp': int(time.time()),
|
||||||
'session_key': session.get('session_key', None),
|
'session_key': session.get('session_key', None),
|
||||||
|
@ -225,7 +232,7 @@ def set_notify_state(notify_action, notifier, subject, body, session=None, metad
|
||||||
'agent_name': notifier['agent_name'],
|
'agent_name': notifier['agent_name'],
|
||||||
'subject_text': subject,
|
'subject_text': subject,
|
||||||
'body_text': body,
|
'body_text': body,
|
||||||
'poster_url': metadata.get('poster_url', None)}
|
'poster_url': parameters.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)
|
||||||
else:
|
else:
|
||||||
|
@ -252,7 +259,7 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, *
|
||||||
logger.error(u"PlexPy NotificationHandler :: Unable to retrieve server uptime.")
|
logger.error(u"PlexPy NotificationHandler :: Unable to retrieve server uptime.")
|
||||||
server_uptime = 'N/A'
|
server_uptime = 'N/A'
|
||||||
|
|
||||||
# Get metadata feed for item
|
# Get metadata for the item
|
||||||
if session:
|
if session:
|
||||||
rating_key = session['rating_key']
|
rating_key = session['rating_key']
|
||||||
elif timeline:
|
elif timeline:
|
||||||
|
@ -263,7 +270,7 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, *
|
||||||
|
|
||||||
if not metadata:
|
if not metadata:
|
||||||
logger.error(u"PlexPy NotificationHandler :: Unable to retrieve metadata for rating_key %s" % str(rating_key))
|
logger.error(u"PlexPy NotificationHandler :: Unable to retrieve metadata for rating_key %s" % str(rating_key))
|
||||||
return None, None
|
return None
|
||||||
|
|
||||||
child_metadata = grandchild_metadata = []
|
child_metadata = grandchild_metadata = []
|
||||||
for key in kwargs.pop('child_keys', []):
|
for key in kwargs.pop('child_keys', []):
|
||||||
|
@ -271,24 +278,14 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, *
|
||||||
for key in kwargs.pop('grandchild_keys', []):
|
for key in kwargs.pop('grandchild_keys', []):
|
||||||
grandchild_metadata.append(pms_connect.get_metadata_details(rating_key=key))
|
grandchild_metadata.append(pms_connect.get_metadata_details(rating_key=key))
|
||||||
|
|
||||||
current_activity = pms_connect.get_current_activity()
|
ap = activity_processor.ActivityProcessor()
|
||||||
sessions = current_activity.get('sessions', [])
|
sessions = ap.get_sessions()
|
||||||
stream_count = current_activity.get('stream_count', '')
|
stream_count = len(sessions)
|
||||||
user_stream_count = sum(1 for d in sessions if d['user_id'] == session['user_id']) if session else ''
|
user_sessions = ap.get_sessions(user_id=session['user_id'])
|
||||||
|
user_stream_count = len(user_sessions)
|
||||||
# Create a title
|
|
||||||
if metadata['media_type'] == 'episode' or metadata['media_type'] == 'track':
|
|
||||||
full_title = '%s - %s' % (metadata['grandparent_title'],
|
|
||||||
metadata['title'])
|
|
||||||
elif metadata['media_type'] == 'season' or metadata['media_type'] == 'album':
|
|
||||||
full_title = '%s - %s' % (metadata['parent_title'],
|
|
||||||
metadata['title'])
|
|
||||||
else:
|
|
||||||
full_title = metadata['title']
|
|
||||||
|
|
||||||
# Session values
|
# Session values
|
||||||
if session is None:
|
session = session or {}
|
||||||
session = {}
|
|
||||||
|
|
||||||
# Generate a combined transcode decision value
|
# Generate a combined transcode decision value
|
||||||
if session.get('video_decision','') == 'transcode' or session.get('audio_decision','') == 'transcode':
|
if session.get('video_decision','') == 'transcode' or session.get('audio_decision','') == 'transcode':
|
||||||
|
@ -345,49 +342,18 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, *
|
||||||
metadata['lastfm_id'] = metadata['guid'].split('lastfm://')[1].rsplit('/', 1)[0]
|
metadata['lastfm_id'] = metadata['guid'].split('lastfm://')[1].rsplit('/', 1)[0]
|
||||||
metadata['lastfm_url'] = 'https://www.last.fm/music/' + metadata['lastfm_id']
|
metadata['lastfm_url'] = 'https://www.last.fm/music/' + metadata['lastfm_id']
|
||||||
|
|
||||||
if metadata['media_type'] == 'movie' or metadata['media_type'] == 'show' or metadata['media_type'] == 'artist':
|
if plexpy.CONFIG.NOTIFY_UPLOAD_POSTERS:
|
||||||
thumb = metadata['thumb']
|
metadata['poster_url'] = upload_poster(metadata=metadata)
|
||||||
poster_key = metadata['rating_key']
|
|
||||||
poster_title = metadata['title']
|
# Create a title
|
||||||
elif metadata['media_type'] == 'episode':
|
if metadata['media_type'] == 'episode' or metadata['media_type'] == 'track':
|
||||||
thumb = metadata['grandparent_thumb']
|
full_title = '%s - %s' % (metadata['grandparent_title'],
|
||||||
poster_key = metadata['grandparent_rating_key']
|
metadata['title'])
|
||||||
poster_title = metadata['grandparent_title']
|
elif metadata['media_type'] == 'season' or metadata['media_type'] == 'album':
|
||||||
elif metadata['media_type'] == 'track':
|
full_title = '%s - %s' % (metadata['parent_title'],
|
||||||
thumb = metadata['parent_thumb']
|
metadata['title'])
|
||||||
poster_key = metadata['parent_rating_key']
|
|
||||||
poster_title = metadata['parent_title']
|
|
||||||
else:
|
else:
|
||||||
thumb = None
|
full_title = metadata['title']
|
||||||
|
|
||||||
if plexpy.CONFIG.NOTIFY_UPLOAD_POSTERS and thumb:
|
|
||||||
# Try to retrieve a poster_url from the database
|
|
||||||
data_factory = datafactory.DataFactory()
|
|
||||||
poster_url = data_factory.get_poster_url(rating_key=poster_key)
|
|
||||||
|
|
||||||
# If no previous poster_url
|
|
||||||
if not poster_url and plexpy.CONFIG.NOTIFY_UPLOAD_POSTERS:
|
|
||||||
try:
|
|
||||||
thread_name = str(threading.current_thread().ident)
|
|
||||||
poster_file = os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster-%s' % thread_name)
|
|
||||||
|
|
||||||
# Retrieve the poster from Plex and cache to file
|
|
||||||
result = pms_connect.get_image(img=thumb)
|
|
||||||
if result and result[0]:
|
|
||||||
with open(poster_file, 'wb') as f:
|
|
||||||
f.write(result[0])
|
|
||||||
else:
|
|
||||||
raise Exception(u'PMS image request failed')
|
|
||||||
|
|
||||||
# Upload thumb to Imgur and get link
|
|
||||||
poster_url = helpers.uploadToImgur(poster_file, poster_title)
|
|
||||||
|
|
||||||
# Delete the cached poster
|
|
||||||
os.remove(poster_file)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(u"PlexPy Notifier :: Unable to retrieve poster for rating_key %s: %s." % (str(rating_key), e))
|
|
||||||
|
|
||||||
metadata['poster_url'] = poster_url
|
|
||||||
|
|
||||||
# Fix metadata params for grouped recently added
|
# Fix metadata params for grouped recently added
|
||||||
show_name = metadata['grandparent_title']
|
show_name = metadata['grandparent_title']
|
||||||
|
@ -551,7 +517,7 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, *
|
||||||
'grandparent_rating_key': metadata['grandparent_rating_key']
|
'grandparent_rating_key': metadata['grandparent_rating_key']
|
||||||
}
|
}
|
||||||
|
|
||||||
return available_params, metadata
|
return available_params
|
||||||
|
|
||||||
|
|
||||||
def build_server_notify_params(notify_action=None, **kwargs):
|
def build_server_notify_params(notify_action=None, **kwargs):
|
||||||
|
@ -608,7 +574,7 @@ def build_server_notify_params(notify_action=None, **kwargs):
|
||||||
'plexpy_update_changelog': plexpy_download_info.get('body', '')
|
'plexpy_update_changelog': plexpy_download_info.get('body', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
return available_params, None
|
return available_params
|
||||||
|
|
||||||
|
|
||||||
def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None):
|
def build_notify_text(subject='', body='', notify_action=None, parameters=None, agent_id=None):
|
||||||
|
@ -714,3 +680,54 @@ def format_group_index(group_keys):
|
||||||
num00.append(str(group[0]).zfill(2))
|
num00.append(str(group[0]).zfill(2))
|
||||||
|
|
||||||
return ','.join(sorted(num)) or '0', ','.join(sorted(num00)) or '00'
|
return ','.join(sorted(num)) or '0', ','.join(sorted(num00)) or '00'
|
||||||
|
|
||||||
|
|
||||||
|
def upload_poster(metadata):
|
||||||
|
if metadata['media_type'] in ('movie', 'show', 'season', 'artist', 'album'):
|
||||||
|
thumb = metadata['thumb']
|
||||||
|
poster_key = metadata['rating_key']
|
||||||
|
poster_title = metadata['title']
|
||||||
|
elif metadata['media_type'] in ('season', 'album'):
|
||||||
|
thumb = metadata['thumb']
|
||||||
|
poster_key = metadata['rating_key']
|
||||||
|
poster_title = '%s - %s' % (metadata['parent_title'],
|
||||||
|
metadata['title'])
|
||||||
|
elif metadata['media_type'] in ('episode', 'track'):
|
||||||
|
thumb = metadata['parent_thumb']
|
||||||
|
poster_key = metadata['parent_rating_key']
|
||||||
|
poster_title = '%s - %s' % (metadata['grandparent_title'],
|
||||||
|
metadata['parent_title'])
|
||||||
|
else:
|
||||||
|
thumb = None
|
||||||
|
|
||||||
|
poster_url = ''
|
||||||
|
|
||||||
|
if thumb:
|
||||||
|
# Try to retrieve a poster_url from the database
|
||||||
|
data_factory = datafactory.DataFactory()
|
||||||
|
poster_url = data_factory.get_poster_url(rating_key=poster_key)
|
||||||
|
|
||||||
|
# If no previous poster_url
|
||||||
|
if not poster_url:
|
||||||
|
try:
|
||||||
|
thread_name = str(threading.current_thread().ident)
|
||||||
|
poster_file = os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster-%s' % thread_name)
|
||||||
|
|
||||||
|
# Retrieve the poster from Plex and cache to file
|
||||||
|
pms_connect = pmsconnect.PmsConnect()
|
||||||
|
result = pms_connect.get_image(img=thumb)
|
||||||
|
if result and result[0]:
|
||||||
|
with open(poster_file, 'wb') as f:
|
||||||
|
f.write(result[0])
|
||||||
|
else:
|
||||||
|
raise Exception(u'PMS image request failed')
|
||||||
|
|
||||||
|
# Upload thumb to Imgur and get link
|
||||||
|
poster_url = helpers.uploadToImgur(poster_file, poster_title)
|
||||||
|
|
||||||
|
# Delete the cached poster
|
||||||
|
os.remove(poster_file)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(u"PlexPy Notifier :: Unable to retrieve poster for rating_key %s: %s." % (str(rating_key), e))
|
||||||
|
|
||||||
|
return poster_url
|
||||||
|
|
|
@ -504,12 +504,12 @@ def send_notification(notifier_id=None, subject='', body='', notify_action='', *
|
||||||
|
|
||||||
|
|
||||||
class PrettyMetadata(object):
|
class PrettyMetadata(object):
|
||||||
def __init__(self, metadata):
|
def __init__(self, parameters):
|
||||||
self.metadata = metadata
|
self.parameters = parameters
|
||||||
self.media_type = metadata['media_type']
|
self.media_type = parameters['media_type']
|
||||||
|
|
||||||
def get_poster_url(self):
|
def get_poster_url(self):
|
||||||
self.poster_url = self.metadata.get('poster_url','')
|
self.poster_url = self.parameters['poster_url']
|
||||||
if not self.poster_url:
|
if not self.poster_url:
|
||||||
if self.media_type in ('artist', 'album', 'track'):
|
if self.media_type in ('artist', 'album', 'track'):
|
||||||
self.poster_url = 'https://raw.githubusercontent.com/%s/plexpy/master/data/interfaces/default/images/cover.png' % plexpy.CONFIG.GIT_USER
|
self.poster_url = 'https://raw.githubusercontent.com/%s/plexpy/master/data/interfaces/default/images/cover.png' % plexpy.CONFIG.GIT_USER
|
||||||
|
@ -519,59 +519,59 @@ class PrettyMetadata(object):
|
||||||
|
|
||||||
def get_poster_link(self):
|
def get_poster_link(self):
|
||||||
self.poster_link = ''
|
self.poster_link = ''
|
||||||
if self.metadata.get('thetvdb_url',''):
|
if self.parameters['thetvdb_url']:
|
||||||
self.poster_link = self.metadata.get('thetvdb_url', '')
|
self.poster_link = self.parameters['thetvdb_url']
|
||||||
elif self.metadata.get('themoviedb_url',''):
|
elif self.parameters['themoviedb_url']:
|
||||||
self.poster_link = self.metadata.get('themoviedb_url', '')
|
self.poster_link = self.parameters['themoviedb_url']
|
||||||
elif self.metadata.get('imdb_url',''):
|
elif self.parameters['imdb_url']:
|
||||||
self.poster_link = self.metadata.get('imdb_url', '')
|
self.poster_link = self.parameters['imdb_url']
|
||||||
elif self.metadata.get('lastfm_url',''):
|
elif self.parameters['lastfm_url']:
|
||||||
self.poster_link = self.metadata.get('lastfm_url', '')
|
self.poster_link = self.parameters['lastfm_url']
|
||||||
return self.poster_link
|
return self.poster_link
|
||||||
|
|
||||||
def get_caption(self):
|
def get_caption(self):
|
||||||
self.caption = ''
|
self.caption = ''
|
||||||
if self.metadata.get('thetvdb_url',''):
|
if self.parameters['thetvdb_url']:
|
||||||
self.caption = 'View on TheTVDB'
|
self.caption = 'View on TheTVDB'
|
||||||
elif self.metadata.get('themoviedb_url',''):
|
elif self.parameters['themoviedb_url']:
|
||||||
self.caption = 'View on The Movie Database'
|
self.caption = 'View on The Movie Database'
|
||||||
elif self.metadata.get('imdb_url',''):
|
elif self.parameters['imdb_url']:
|
||||||
self.caption = 'View on IMDB'
|
self.caption = 'View on IMDB'
|
||||||
elif self.metadata.get('lastfm_url',''):
|
elif self.parameters['lastfm_url']:
|
||||||
self.caption = 'View on Last.fm'
|
self.caption = 'View on Last.fm'
|
||||||
return self.caption
|
return self.caption
|
||||||
|
|
||||||
def get_title(self, divider = '-'):
|
def get_title(self, divider = '-'):
|
||||||
self.title = ''
|
self.title = ''
|
||||||
if self.media_type == 'movie':
|
if self.media_type == 'movie':
|
||||||
self.title = '%s (%s)' % (self.metadata['title'], self.metadata['year'])
|
self.title = '%s (%s)' % (self.parameters['title'], self.parameters['year'])
|
||||||
elif self.media_type == 'show':
|
elif self.media_type == 'show':
|
||||||
self.title = '%s (%s)' % (self.metadata['title'], self.metadata['year'])
|
self.title = '%s (%s)' % (self.parameters['show_name'], self.parameters['year'])
|
||||||
elif self.media_type == 'season':
|
elif self.media_type == 'season':
|
||||||
self.title = '%s - %s' % (self.metadata['parent_title'], self.metadata['title'])
|
self.title = '%s - Season %s' % (self.parameters['show_name'], self.parameters['season_num'])
|
||||||
elif self.media_type == 'episode':
|
elif self.media_type == 'episode':
|
||||||
self.title = '%s - %s (S%s %s E%s)' % (self.metadata['grandparent_title'],
|
self.title = '%s - %s (S%s %s E%s)' % (self.parameters['show_name'],
|
||||||
self.metadata['title'],
|
self.parameters['episode_name'],
|
||||||
self.metadata['parent_media_index'],
|
self.parameters['season_num'],
|
||||||
divider,
|
divider,
|
||||||
self.metadata['media_index'])
|
self.parameters['episode_num'])
|
||||||
elif self.media_type == 'artist':
|
elif self.media_type == 'artist':
|
||||||
self.title = self.metadata['title']
|
self.title = self.parameters['artist_name']
|
||||||
elif self.media_type == 'album':
|
elif self.media_type == 'album':
|
||||||
self.title = '%s - %s' % (self.metadata['parent_title'], self.metadata['title'])
|
self.title = '%s - %s' % (self.parameters['artist_name'], self.parameters['album_name'])
|
||||||
elif self.media_type == 'track':
|
elif self.media_type == 'track':
|
||||||
self.title = '%s - %s' % (self.metadata['grandparent_title'], self.metadata['title'])
|
self.title = '%s - %s' % (self.parameters['artist_name'], self.parameters['track_name'])
|
||||||
return self.title.encode("utf-8")
|
return self.title.encode("utf-8")
|
||||||
|
|
||||||
def get_subtitle(self):
|
def get_subtitle(self):
|
||||||
if self.media_type == 'track':
|
if self.media_type == 'track':
|
||||||
self.subtitle = self.metadata['parent_title']
|
self.subtitle = self.parameters['album_name']
|
||||||
else:
|
else:
|
||||||
self.subtitle = self.metadata['summary']
|
self.subtitle = self.parameters['summary']
|
||||||
return self.subtitle.encode("utf-8")
|
return self.subtitle.encode("utf-8")
|
||||||
|
|
||||||
def get_plex_url(self):
|
def get_plex_url(self):
|
||||||
self.plex_url = self.metadata['plex_url']
|
self.plex_url = self.parameters['plex_url']
|
||||||
return self.plex_url
|
return self.plex_url
|
||||||
|
|
||||||
|
|
||||||
|
@ -777,9 +777,9 @@ class DISCORD(Notifier):
|
||||||
#if self.config['tts']:
|
#if self.config['tts']:
|
||||||
# data['tts'] = True
|
# data['tts'] = True
|
||||||
|
|
||||||
if self.config['incl_poster'] and 'metadata' in kwargs:
|
if self.config['incl_poster'] and kwargs.get('parameters'):
|
||||||
# Grab formatted metadata
|
# Grab formatted metadata
|
||||||
pretty_metadata = PrettyMetadata(kwargs['metadata'])
|
pretty_metadata = PrettyMetadata(kwargs['parameters'])
|
||||||
poster_url = pretty_metadata.get_poster_url()
|
poster_url = pretty_metadata.get_poster_url()
|
||||||
plex_url = pretty_metadata.get_plex_url()
|
plex_url = pretty_metadata.get_plex_url()
|
||||||
poster_link = pretty_metadata.get_poster_link()
|
poster_link = pretty_metadata.get_poster_link()
|
||||||
|
@ -1075,9 +1075,9 @@ class FACEBOOK(Notifier):
|
||||||
|
|
||||||
attachment = {}
|
attachment = {}
|
||||||
|
|
||||||
if self.config['incl_poster'] and 'metadata' in kwargs:
|
if self.config['incl_poster'] and kwargs.get('parameters'):
|
||||||
# Grab formatted metadata
|
# Grab formatted metadata
|
||||||
pretty_metadata = PrettyMetadata(kwargs['metadata'])
|
pretty_metadata = PrettyMetadata(kwargs['parameters'])
|
||||||
poster_url = pretty_metadata.get_poster_url()
|
poster_url = pretty_metadata.get_poster_url()
|
||||||
plex_url = pretty_metadata.get_plex_url()
|
plex_url = pretty_metadata.get_plex_url()
|
||||||
poster_link = pretty_metadata.get_poster_link()
|
poster_link = pretty_metadata.get_poster_link()
|
||||||
|
@ -2261,7 +2261,7 @@ class SCRIPTS(Notifier):
|
||||||
|
|
||||||
# Allow overrides for shitty systems
|
# Allow overrides for shitty systems
|
||||||
if prefix and script_args:
|
if prefix and script_args:
|
||||||
if script_args[0] in ['python2', 'python', 'pythonw', 'php', 'ruby', 'perl']:
|
if script_args[0] in ('python2', 'python', 'pythonw', 'php', 'ruby', 'perl'):
|
||||||
script[0] = script_args[0]
|
script[0] = script_args[0]
|
||||||
del script_args[0]
|
del script_args[0]
|
||||||
|
|
||||||
|
@ -2337,9 +2337,9 @@ class SLACK(Notifier):
|
||||||
else:
|
else:
|
||||||
data['icon_url'] = self.config['icon_emoji']
|
data['icon_url'] = self.config['icon_emoji']
|
||||||
|
|
||||||
if self.config['incl_poster'] and 'metadata' in kwargs:
|
if self.config['incl_poster'] and kwargs.get('parameters'):
|
||||||
# Grab formatted metadata
|
# Grab formatted metadata
|
||||||
pretty_metadata = PrettyMetadata(kwargs['metadata'])
|
pretty_metadata = PrettyMetadata(kwargs['parameters'])
|
||||||
poster_url = pretty_metadata.get_poster_url()
|
poster_url = pretty_metadata.get_poster_url()
|
||||||
plex_url = pretty_metadata.get_plex_url()
|
plex_url = pretty_metadata.get_plex_url()
|
||||||
poster_link = pretty_metadata.get_poster_link()
|
poster_link = pretty_metadata.get_poster_link()
|
||||||
|
@ -2461,12 +2461,12 @@ class TELEGRAM(Notifier):
|
||||||
else:
|
else:
|
||||||
text = body.encode('utf-8')
|
text = body.encode('utf-8')
|
||||||
|
|
||||||
if self.config['incl_poster'] and 'metadata' in kwargs:
|
if self.config['incl_poster'] and kwargs.get('parameters'):
|
||||||
poster_data = {'chat_id': self.config['chat_id'],
|
poster_data = {'chat_id': self.config['chat_id'],
|
||||||
'disable_notification': True}
|
'disable_notification': True}
|
||||||
|
|
||||||
metadata = kwargs['metadata']
|
parameters = kwargs['parameters']
|
||||||
poster_url = metadata.get('poster_url','')
|
poster_url = parameters.get('poster_url','')
|
||||||
|
|
||||||
if poster_url:
|
if poster_url:
|
||||||
files = {'photo': (poster_url, urllib.urlopen(poster_url).read())}
|
files = {'photo': (poster_url, urllib.urlopen(poster_url).read())}
|
||||||
|
@ -2595,9 +2595,9 @@ class TWITTER(Notifier):
|
||||||
return
|
return
|
||||||
|
|
||||||
poster_url = ''
|
poster_url = ''
|
||||||
if self.config['incl_poster'] and 'metadata' in kwargs:
|
if self.config['incl_poster'] and kwargs.get('parameters'):
|
||||||
metadata = kwargs['metadata']
|
parameters = kwargs['parameters']
|
||||||
poster_url = metadata.get('poster_url','')
|
poster_url = parameters.get('poster_url','')
|
||||||
|
|
||||||
if self.config['incl_subject']:
|
if self.config['incl_subject']:
|
||||||
return self._send_tweet(subject + '\r\n' + body, attachment=poster_url)
|
return self._send_tweet(subject + '\r\n' + body, attachment=poster_url)
|
||||||
|
|
|
@ -728,7 +728,7 @@ class PmsConnect(object):
|
||||||
'parent_rating_key': helpers.get_xml_attr(metadata_main, 'parentRatingKey'),
|
'parent_rating_key': helpers.get_xml_attr(metadata_main, 'parentRatingKey'),
|
||||||
'grandparent_rating_key': helpers.get_xml_attr(metadata_main, 'grandparentRatingKey'),
|
'grandparent_rating_key': helpers.get_xml_attr(metadata_main, 'grandparentRatingKey'),
|
||||||
'title': helpers.get_xml_attr(metadata_main, 'title'),
|
'title': helpers.get_xml_attr(metadata_main, 'title'),
|
||||||
'parent_title': helpers.get_xml_attr(metadata_main, 'parentTitle'),
|
'parent_title': 'Season %s' % helpers.get_xml_attr(metadata_main, 'parentIndex'),
|
||||||
'grandparent_title': helpers.get_xml_attr(metadata_main, 'grandparentTitle'),
|
'grandparent_title': helpers.get_xml_attr(metadata_main, 'grandparentTitle'),
|
||||||
'media_index': helpers.get_xml_attr(metadata_main, 'index'),
|
'media_index': helpers.get_xml_attr(metadata_main, 'index'),
|
||||||
'parent_media_index': helpers.get_xml_attr(metadata_main, 'parentIndex'),
|
'parent_media_index': helpers.get_xml_attr(metadata_main, 'parentIndex'),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue