Test poster images for Facebook notifications

This commit is contained in:
JonnyWong16 2016-02-14 22:36:19 -08:00
parent 25c2f95e48
commit 0bd40405b5
2 changed files with 80 additions and 28 deletions

View file

@ -53,7 +53,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
# 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) set_notify_state(session=stream_data, state=notify_action, agent_info=agent)
@ -66,7 +67,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
set_notify_state(session=stream_data, state=notify_action, agent_info=agent) set_notify_state(session=stream_data, state=notify_action, agent_info=agent)
@ -78,7 +80,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
set_notify_state(session=stream_data, state=notify_action, agent_info=agent) set_notify_state(session=stream_data, state=notify_action, agent_info=agent)
@ -90,7 +93,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
set_notify_state(session=stream_data, state=notify_action, agent_info=agent) set_notify_state(session=stream_data, state=notify_action, agent_info=agent)
@ -117,7 +121,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
# 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) set_notify_state(session=stream_data, state=notify_action, agent_info=agent)
@ -132,7 +137,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
# 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) set_notify_state(session=stream_data, state=notify_action, agent_info=agent)
@ -219,7 +225,8 @@ def notify_timeline(timeline_data=None, notify_action=None):
subject=notify_strings[0], subject=notify_strings[0],
body=notify_strings[1], body=notify_strings[1],
notify_action=notify_action, notify_action=notify_action,
script_args=notify_strings[2]) script_args=notify_strings[2],
metadata=notify_strings[3])
# 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) set_notify_state(session=timeline_data, state=notify_action, agent_info=agent)
@ -618,9 +625,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
elif state == 'stop': elif state == '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'],
@ -642,9 +649,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
elif state == 'pause': elif state == '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'],
@ -666,9 +673,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
elif state == 'resume': elif state == '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'],
@ -690,9 +697,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
elif state == 'buffer': elif state == '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'],
@ -714,9 +721,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
elif state == 'watched': elif state == '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'],
@ -738,9 +745,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
elif state == 'created': elif state == '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
@ -760,9 +767,9 @@ def build_notify_text(session=None, timeline=None, state=None):
except: except:
logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.") logger.error(u"PlexPy NotificationHandler :: Unable to parse custom notification body. Using fallback.")
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return [subject_text, body_text, script_args] return [subject_text, body_text, script_args, metadata]
else: else:
return None return None

View file

@ -19,7 +19,7 @@ import json
import cherrypy import cherrypy
from email.mime.text import MIMEText from email.mime.text import MIMEText
import email.utils import email.utils
from httplib import HTTPSConnection from httplib import HTTPSConnection, HTTPConnection
import os import os
import shlex import shlex
import smtplib import smtplib
@ -473,7 +473,7 @@ def send_notification(agent_id, subject, body, **kwargs):
scripts.notify(message=body, subject=subject, **kwargs) scripts.notify(message=body, subject=subject, **kwargs)
elif agent_id == 16: elif agent_id == 16:
facebook = FacebookNotifier() facebook = FacebookNotifier()
facebook.notify(subject=subject, message=body) facebook.notify(subject=subject, message=body, **kwargs)
else: else:
logger.debug(u"PlexPy Notifiers :: Unknown agent id received.") logger.debug(u"PlexPy Notifiers :: Unknown agent id received.")
else: else:
@ -2074,14 +2074,14 @@ class FacebookNotifier(object):
self.group_id = plexpy.CONFIG.FACEBOOK_GROUP self.group_id = plexpy.CONFIG.FACEBOOK_GROUP
self.incl_subject = plexpy.CONFIG.FACEBOOK_INCL_SUBJECT self.incl_subject = plexpy.CONFIG.FACEBOOK_INCL_SUBJECT
def notify(self, subject, message): def notify(self, subject, message, **kwargs):
if not subject or not message: if not subject or not message:
return return
else: else:
if self.incl_subject: if self.incl_subject:
self._post_facebook(subject + ': ' + message) self._post_facebook(subject + ': ' + message, **kwargs)
else: else:
self._post_facebook(message) self._post_facebook(message, **kwargs)
def test_notify(self): def test_notify(self):
return self._post_facebook(u"PlexPy Notifiers :: This is a test notification from PlexPy at " + helpers.now()) return self._post_facebook(u"PlexPy Notifiers :: This is a test notification from PlexPy at " + helpers.now())
@ -2117,15 +2117,60 @@ class FacebookNotifier(object):
return True return True
def _post_facebook(self, message=None): def _post_facebook(self, message=None, **kwargs):
access_token = plexpy.CONFIG.FACEBOOK_TOKEN access_token = plexpy.CONFIG.FACEBOOK_TOKEN
group_id = plexpy.CONFIG.FACEBOOK_GROUP group_id = plexpy.CONFIG.FACEBOOK_GROUP
if group_id: if group_id:
api = facebook.GraphAPI(access_token=access_token, version='2.5') api = facebook.GraphAPI(access_token=access_token, version='2.5')
attachment = {}
if 'metadata' in kwargs:
metadata = kwargs['metadata']
if metadata['media_type'] == 'movie' and metadata['imdb_id']:
uri = 'i=' + metadata['imdb_id']
title = metadata['title']
subtitle = metadata['year']
elif metadata['media_type'] == 'show':
uri = 't=' + metadata['title'] + '&y=' + metadata['year']
title = metadata['title']
subtitle = metadata['year']
elif metadata['media_type'] == 'episode':
uri = 't=' + metadata['grandparent_title']
title = metadata['grandparent_title'] + ' - ' + metadata['title']
subtitle = 'S' + metadata['parent_media_index'] + ' ' + '\xc2\xb7'.decode('utf8') + ' E' + metadata['media_index']
else:
uri = ''
title = ''
subtitle = ''
# Get poster using OMDb API
poster = ''
if uri:
http_handler = HTTPConnection("www.omdbapi.com")
http_handler.request('GET', '/?' + uri)
response = http_handler.getresponse()
request_status = response.status
if request_status == 200:
data = json.loads(response.read())
poster = data.get('Poster', '')
elif request_status >= 400 and request_status < 500:
logger.warn(u"PlexPy Notifiers :: Unable to retrieve IMDB poster: %s" % response.reason)
else:
logger.warn(u"PlexPy Notifiers :: Unable to retrieve IMDB poster.")
if poster and poster != 'N/A':
attachment['link'] = 'http://app.plex.tv/web/app#!/server/' + plexpy.CONFIG.PMS_IDENTIFIER + \
'/details/%2Flibrary%2Fmetadata%2F' + metadata['rating_key']
attachment['picture'] = poster
attachment['name'] = title
attachment['description'] = subtitle
try: try:
api.put_wall_post(profile_id=group_id, message=message) api.put_wall_post(profile_id=group_id, message=message, attachment=attachment)
logger.info(u"PlexPy Notifiers :: Facebook notification sent.") logger.info(u"PlexPy Notifiers :: Facebook notification sent.")
except Exception as e: except Exception as e:
logger.warn(u"PlexPy Notifiers :: Error sending Facebook post: %s" % e) logger.warn(u"PlexPy Notifiers :: Error sending Facebook post: %s" % e)