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],
body=notify_strings[1],
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_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],
body=notify_strings[1],
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)
@ -78,7 +80,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0],
body=notify_strings[1],
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)
@ -90,7 +93,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0],
body=notify_strings[1],
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)
@ -117,7 +121,8 @@ def notify(stream_data=None, notify_action=None):
subject=notify_strings[0],
body=notify_strings[1],
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_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],
body=notify_strings[1],
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_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],
body=notify_strings[1],
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_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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
elif state == 'stop':
# Default body text
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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
elif state == 'pause':
# Default body text
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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
elif state == 'resume':
# Default body text
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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
elif state == 'buffer':
# Default body text
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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
elif state == 'watched':
# Default body text
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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
elif state == 'created':
# Default body text
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:
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:
return [subject_text, body_text, script_args]
return [subject_text, body_text, script_args, metadata]
else:
return None

View file

@ -19,7 +19,7 @@ import json
import cherrypy
from email.mime.text import MIMEText
import email.utils
from httplib import HTTPSConnection
from httplib import HTTPSConnection, HTTPConnection
import os
import shlex
import smtplib
@ -473,7 +473,7 @@ def send_notification(agent_id, subject, body, **kwargs):
scripts.notify(message=body, subject=subject, **kwargs)
elif agent_id == 16:
facebook = FacebookNotifier()
facebook.notify(subject=subject, message=body)
facebook.notify(subject=subject, message=body, **kwargs)
else:
logger.debug(u"PlexPy Notifiers :: Unknown agent id received.")
else:
@ -2074,14 +2074,14 @@ class FacebookNotifier(object):
self.group_id = plexpy.CONFIG.FACEBOOK_GROUP
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:
return
else:
if self.incl_subject:
self._post_facebook(subject + ': ' + message)
self._post_facebook(subject + ': ' + message, **kwargs)
else:
self._post_facebook(message)
self._post_facebook(message, **kwargs)
def test_notify(self):
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
def _post_facebook(self, message=None):
def _post_facebook(self, message=None, **kwargs):
access_token = plexpy.CONFIG.FACEBOOK_TOKEN
group_id = plexpy.CONFIG.FACEBOOK_GROUP
if group_id:
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:
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.")
except Exception as e:
logger.warn(u"PlexPy Notifiers :: Error sending Facebook post: %s" % e)