Refactored PrettyMetadata class.

Trying basic Hipchat poster without links (unfortunately not allowed to use <div>s).
This commit is contained in:
greg.gaskill 2016-07-12 17:13:54 -04:00
parent 6d9ef8bbc3
commit 809f120db0
2 changed files with 107 additions and 88 deletions

View file

@ -215,6 +215,9 @@ _CONFIG_DEFINITIONS = {
'HIPCHAT_URL': (str, 'Hipchat', ''), 'HIPCHAT_URL': (str, 'Hipchat', ''),
'HIPCHAT_COLOR': (str, 'Hipchat', ''), 'HIPCHAT_COLOR': (str, 'Hipchat', ''),
'HIPCHAT_INCL_SUBJECT': (int, 'Hipchat', 1), 'HIPCHAT_INCL_SUBJECT': (int, 'Hipchat', 1),
'HIPCHAT_FROM_SUBJECT': (int, 'Hipchat', 0),
'HIPCHAT_INCL_PMSLINK': (int, 'Hipchat', 0),
'HIPCHAT_INCL_POSTER': (int, 'Hipchat', 0),
'HIPCHAT_EMOTICON': (str, 'Hipchat', ''), 'HIPCHAT_EMOTICON': (str, 'Hipchat', ''),
'HIPCHAT_ENABLED': (int, 'Hipchat', 0), 'HIPCHAT_ENABLED': (int, 'Hipchat', 0),
'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0), 'HIPCHAT_ON_PLAY': (int, 'Hipchat', 0),

View file

@ -609,100 +609,78 @@ def send_notification(agent_id, subject, body, notify_action, **kwargs):
return join.notify(message=body, subject=subject) return join.notify(message=body, subject=subject)
elif agent_id == 19: elif agent_id == 19:
hipchat = HIPCHAT() hipchat = HIPCHAT()
return hipchat.notify(message=body, subject=subject) return hipchat.notify(message=body, subject=subject, **kwargs)
else: else:
logger.debug(u"PlexPy Notifiers :: Unknown agent id received.") logger.debug(u"PlexPy Notifiers :: Unknown agent id received.")
else: else:
logger.debug(u"PlexPy Notifiers :: Notification requested but no agent id received.") logger.debug(u"PlexPy Notifiers :: Notification requested but no agent id received.")
class PrettyMetadata(object): class PrettyMetadata(object):
def __init__(self, metadata, divider): def __init__(self, metadata):
self.poster_url = metadata.get('poster_url','') self.metadata = metadata
self.poster_link = '' self.media_type = metadata['media_type']
self.caption = ''
self.plex_url = None
self.title = None
self.season_divider = divider
self.subtitle = None
def get_poster_url(self):
self.poster_url = self.metadata.get('poster_url','')
if not self.poster_url: if not self.poster_url:
if metadata['media_type'] in ['artist', 'track']: if self.metadata['media_type'] in ['artist', 'track']:
self.poster_url = 'https://raw.githubusercontent.com/drzoidberg33/plexpy/master/data/interfaces/default/images/cover.png' self.poster_url = 'https://raw.githubusercontent.com/drzoidberg33/plexpy/master/data/interfaces/default/images/cover.png'
else: else:
self.poster_url = 'https://raw.githubusercontent.com/drzoidberg33/plexpy/master/data/interfaces/default/images/poster.png' self.poster_url = 'https://raw.githubusercontent.com/drzoidberg33/plexpy/master/data/interfaces/default/images/poster.png'
if metadata['media_type'] == 'movie':
self.title = '%s (%s)' % (metadata['title'], metadata['year'])
self.unicode_title = self.title
self.subtitle = metadata['summary']
if metadata.get('imdb_url',''):
self.poster_link = metadata.get('imdb_url', '')
self.caption = 'View on IMDB'
elif metadata.get('themoviedb_url',''):
self.poster_link = metadata.get('themoviedb_url', '')
self.caption = 'View on The Movie Database'
elif metadata['media_type'] == 'show':
self.title = '%s (%s)' % (metadata['title'], metadata['year'])
self.unicode_title = self.title
self.subtitle = metadata['summary']
if metadata.get('thetvdb_url',''):
self.poster_link = metadata.get('thetvdb_url', '')
self.caption = 'View on TheTVDB'
elif metadata.get('themoviedb_url',''):
self.poster_link = metadata.get('themoviedb_url', '')
self.caption = 'View on The Movie Database'
elif metadata['media_type'] == 'episode':
self.title = '%s - %s (S%s %s E%s)' % (metadata['grandparent_title'],
metadata['title'],
metadata['parent_media_index'],
self.season_divider,
metadata['media_index'])
self.subtitle = metadata['summary']
if metadata.get('thetvdb_url',''):
self.poster_link = metadata.get('thetvdb_url', '')
self.caption = 'View on TheTVDB'
elif metadata.get('themoviedb_url',''):
self.poster_link = metadata.get('themoviedb_url', '')
self.caption = 'View on The Movie Database'
elif metadata['media_type'] == 'artist':
self.title = metadata['title']
self.unicode_title = self.title
self.subtitle = metadata['summary']
if metadata.get('lastfm_url',''):
self.poster_link = metadata.get('lastfm_url', '')
self.caption = 'View on Last.fm'
elif metadata['media_type'] == 'track':
self.title = '%s - %s' % (metadata['grandparent_title'], metadata['title'])
self.unicode_title = self.title
self.subtitle = metadata['parent_title']
if metadata.get('lastfm_url',''):
self.poster_link = metadata.get('lastfm_url', '')
self.caption = 'View on Last.fm'
self.plex_url = metadata['plex_url']
def get_poster_url(self):
return self.poster_url return self.poster_url
def get_plex_url(self):
return self.plex_url
def get_poster_link(self): def get_poster_link(self):
self.poster_link = ''
if self.metadata.get('thetvdb_url',''):
self.poster_link = self.metadata.get('thetvdb_url', '')
elif self.metadata.get('themoviedb_url',''):
self.poster_link = self.metadata.get('themoviedb_url', '')
elif self.metadata.get('imdb_url',''):
self.poster_link = self.metadata.get('imdb_url', '')
elif self.metadata.get('lastfm_url',''):
self.poster_link = self.metadata.get('lastfm_url', '')
return self.poster_link return self.poster_link
def get_caption(self): def get_caption(self):
self.caption = ''
if self.metadata.get('thetvdb_url',''):
self.caption = 'View on TheTVDB'
elif self.metadata.get('themoviedb_url',''):
self.caption = 'View on The Movie Database'
elif self.metadata.get('imdb_url',''):
self.caption = 'View on IMDB'
elif self.metadata.get('lastfm_url',''):
self.caption = 'View on Last.fm'
return self.caption return self.caption
def get_title(self): def get_title(self, divider = ''):
if self.metadata['media_type'] == 'movie':
self.title = '%s (%s)' % (self.metadata['title'], self.metadata['year'])
elif self.metadata['media_type'] == 'show':
self.title = '%s (%s)' % (self.metadata['title'], self.metadata['year'])
elif self.metadata['media_type'] == 'artist':
self.title = self.metadata['title']
elif self.metadata['media_type'] == 'track':
self.title = '%s - %s' % (self.metadata['grandparent_title'], self.metadata['title'])
elif self.metadata['media_type'] == 'episode':
self.title = '%s - %s (S%s %s E%s)' % (self.metadata['grandparent_title'],
self.metadata['title'],
self.metadata['parent_media_index'],
divider,
self.metadata['media_index'])
return self.title return self.title
def get_subtitle(self): def get_subtitle(self):
if self.metadata['media_type'] == 'track':
self.subtitle = self.metadata['parent_title']
else:
self.subtitle = self.metadata['summary']
return self.subtitle return self.subtitle
def get_plex_url(self):
self.plex_url = self.metadata['plex_url']
return self.plex_url
class GROWL(object): class GROWL(object):
""" """
Growl notifications, for OS X. Growl notifications, for OS X.
@ -2007,12 +1985,12 @@ class SLACK(object):
if self.incl_poster and 'metadata' in kwargs: if self.incl_poster and 'metadata' in kwargs:
# Grab formatted metadata # Grab formatted metadata
pretty_metadata = PrettyMetadata(kwargs['metadata'], '-') pretty_metadata = PrettyMetadata(kwargs['metadata'])
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()
caption = pretty_metadata.get_caption() caption = pretty_metadata.get_caption()
title = pretty_metadata.get_title() title = pretty_metadata.get_title('-')
# Build Slack post attachment # Build Slack post attachment
attachment = {} attachment = {}
@ -2418,12 +2396,12 @@ class FacebookNotifier(object):
if self.incl_poster and 'metadata' in kwargs: if self.incl_poster and 'metadata' in kwargs:
# Grab formatted metadata # Grab formatted metadata
pretty_metadata = PrettyMetadata(kwargs['metadata'], '\xc2\xb7'.decode('utf8')) pretty_metadata = PrettyMetadata(kwargs['metadata'])
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()
caption = pretty_metadata.get_caption() caption = pretty_metadata.get_caption()
title = pretty_metadata.get_unicode_title() title = pretty_metadata.get_title('\xc2\xb7'.decode('utf8'))
subtitle = pretty_metadata.get_subtitle() subtitle = pretty_metadata.get_subtitle()
# Build Facebook post attachment # Build Facebook post attachment
@ -2744,32 +2722,52 @@ class HIPCHAT(object):
def __init__(self): def __init__(self):
self.apiurl = plexpy.CONFIG.HIPCHAT_URL self.apiurl = plexpy.CONFIG.HIPCHAT_URL
self.color = plexpy.CONFIG.HIPCHAT_COLOR self.color = plexpy.CONFIG.HIPCHAT_COLOR
self.incl_subject = plexpy.CONFIG.HIPCHAT_INCL_SUBJECT
self.emoticon = plexpy.CONFIG.HIPCHAT_EMOTICON self.emoticon = plexpy.CONFIG.HIPCHAT_EMOTICON
self.from_subject = plexpy.CONFIG.HIPCHAT_FROM_SUBJECT
self.incl_pmslink = plexpy.CONFIG.FACEBOOK_INCL_PMSLINK
self.incl_poster = plexpy.CONFIG.FACEBOOK_INCL_POSTER
self.incl_subject = plexpy.CONFIG.HIPCHAT_INCL_SUBJECT
def notify(self, message, subject): def notify(self, message, subject, **kwargs):
if not message or not subject: if not message or not subject:
return return
if self.incl_subject: data = {'notify': 'false'}
text = subject.encode('utf-8') + ': ' + message.encode('utf-8')
else:
text = message.encode('utf-8') text = message.encode('utf-8')
if self.incl_subject:
text = subject.encode('utf-8') + ': ' + text
if self.emoticon: if self.emoticon:
text = self.emoticon + ' ' + text text = self.emoticon + ' ' + text
data = {'message': text,
'notify': 'false',
'message_format': 'text'}
if self.color: if self.color:
data['color'] = self.color data['color'] = self.color
if self.from_subject:
data['from'] = subject.encode('utf-8')
if self.incl_poster and 'metadata' in kwargs:
pretty_metadata = PrettyMetadata(kwargs['metadata'])
poster_url = pretty_metadata.get_poster_url()
poster_link = pretty_metadata.get_poster_link()
caption = pretty_metadata.get_caption()
title = pretty_metadata.get_title('-')
subtitle = pretty_metadata.get_subtitle()
message = ( '{0}<br>'
'<table cellspacing="0"><tbody><tr>'
'<td><img style="display: block; margin-left: 2px;" height="160" src="{1}"></img></td>'
'<td><b>{2}</b><br>{3}</td></tr></tbody></table>').format(text,poster_url,title,subtitle)
data['message'] = message
data['message_format'] = 'html'
else:
data['message'] = text
data['message_format'] = 'text'
hiphost = urlparse(self.apiurl).hostname hiphost = urlparse(self.apiurl).hostname
hippath = urlparse(self.apiurl).path hipfullq = urlparse(self.apiurl).path + '?' + urlparse(self.apiurl).query
hipquery = urlparse(self.apiurl).query
hipfullq = hippath + '?' + hipquery
http_handler = HTTPSConnection(hiphost) http_handler = HTTPSConnection(hiphost)
http_handler.request("POST", http_handler.request("POST",
@ -2829,6 +2827,24 @@ class HIPCHAT(object):
' <a href="' + helpers.anon_url('https://www.hipchat.com/emoticons/') + '" target="_blank">here</a>.', ' <a href="' + helpers.anon_url('https://www.hipchat.com/emoticons/') + '" target="_blank">here</a>.',
'input_type': 'text' 'input_type': 'text'
}, },
{'label': 'Include Subject as "From"',
'value': self.from_subject,
'name': 'hipchat_from_subject',
'description': 'Include the subject line in the Hipchat "From" field, which appears on the top line above the message.',
'input_type': 'checkbox'
},
{'label': 'Include Poster',
'value': self.incl_poster,
'name': 'hipchat_incl_poster',
'description': 'Include a poster in the notifications.<br>This will change the notification type to HTML and emoticons will no longer work.',
'input_type': 'checkbox'
},
{'label': 'Include Link to Plex Web',
'value': self.incl_pmslink,
'name': 'hipchat_incl_pmslink',
'description': 'Include a link to the media in Plex Web with the notifications.',
'input_type': 'checkbox'
},
{'label': 'Include Subject Line', {'label': 'Include Subject Line',
'value': self.incl_subject, 'value': self.incl_subject,
'name': 'hipchat_incl_subject', 'name': 'hipchat_incl_subject',