Move get poster to notification handler

This commit is contained in:
JonnyWong16 2016-02-18 21:09:07 -08:00
parent b1ecff3d10
commit 0e53252a27
3 changed files with 63 additions and 51 deletions

View file

@ -1479,6 +1479,13 @@ available_notification_agents = sorted(notifiers.available_notification_agents()
<td><strong>{duration}</strong></td> <td><strong>{duration}</strong></td>
<td>The duration (in minutes) for the item.</td> <td>The duration (in minutes) for the item.</td>
</tr> </tr>
<tr>
<td><strong>{poster_url}</strong></td>
<td>
A URL for the movie or TV show poster.<br />
<p class="small-muted">(PMS agent must be Freebase or TheTVDB)</p>
</td>
</tr>
<tr> <tr>
<td><strong>{imdb_id}</strong></td> <td><strong>{imdb_id}</strong></td>
<td>The IMDB ID for the movie. <span class="small-muted">(e.g. tt2488496)</span> <td>The IMDB ID for the movie. <span class="small-muted">(e.g. tt2488496)</span>

View file

@ -14,9 +14,13 @@
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>. # along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
import arrow
import json
from httplib import HTTPConnection
import openanything
import re import re
import time import time
import arrow import urllib2
from plexpy import logger, config, notifiers, database, helpers, plextv, pmsconnect from plexpy import logger, config, notifiers, database, helpers, plextv, pmsconnect
import plexpy import plexpy
@ -495,6 +499,47 @@ def build_notify_text(session=None, timeline=None, state=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']
# Get posters (only IMDB and TheTVDB supported)
if metadata['media_type'] == 'movie' and metadata.get('imdb_id', ''):
uri = '/?i=' + metadata['imdb_id']
# Get poster using OMDb API
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_url = data.get('Poster', '')
metadata['poster_url'] = poster_url if poster_url != 'N/A' else ''
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.")
elif (metadata['media_type'] == 'show' or metadata['media_type'] == 'episode') \
and (metadata.get('imdb_id', '') or metadata.get('thetvdb_id', '')):
if metadata.get('imdb_id', ''):
uri = '/lookup/shows?imdb=' + metadata['imdb_id']
elif metadata.get('thetvdb_id', ''):
uri = '/lookup/shows?thetvdb=' + metadata['thetvdb_id']
# Get poster using TVmaze API
request = urllib2.Request('http://api.tvmaze.com' + uri)
opener = urllib2.build_opener(openanything.SmartRedirectHandler())
response = opener.open(request)
request_status = response.status
if request_status == 301:
data = json.loads(response.read())
image = data.get('image', '')
metadata['poster_url'] = image.get('original', image.get('medium',''))
elif request_status >= 400 and request_status < 500:
logger.warn(u"PlexPy Notifiers :: Unable to retrieve TVmaze poster: %s" % response.reason)
else:
logger.warn(u"PlexPy Notifiers :: Unable to retrieve TVmaze poster.")
# 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 state == 'created' and plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT:
show_name = metadata['title'] show_name = metadata['title']
@ -576,6 +621,7 @@ def build_notify_text(session=None, timeline=None, state=None):
'tagline': metadata['tagline'], 'tagline': metadata['tagline'],
'rating': metadata['rating'], 'rating': metadata['rating'],
'duration': duration, 'duration': duration,
'poster_url': metadata.get('poster_url',''),
'imdb_id': metadata.get('imdb_id',''), 'imdb_id': metadata.get('imdb_id',''),
'imdb_url': metadata.get('imdb_url',''), 'imdb_url': metadata.get('imdb_url',''),
'thetvdb_id': metadata.get('thetvdb_id',''), 'thetvdb_id': metadata.get('thetvdb_id',''),

View file

@ -2127,32 +2127,11 @@ class FacebookNotifier(object):
attachment = {} attachment = {}
if self.incl_poster and 'metadata' in kwargs: if self.incl_poster and 'metadata' in kwargs:
poster = ''
caption = 'View in Plex Web.'
metadata = kwargs['metadata'] metadata = kwargs['metadata']
poster_url = metadata.get('poster_url','')
caption = 'View in Plex Web.'
if metadata['media_type'] == 'movie' and metadata.get('imdb_id', ''): if metadata['media_type'] == 'movie' or metadata['media_type'] == 'show':
title = metadata['title']
subtitle = metadata['year']
uri = '/?i=' + metadata['imdb_id']
# Get poster using OMDb API
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.")
elif (metadata['media_type'] == 'show' or metadata['media_type'] == 'episode') \
and (metadata.get('imdb_id', '') or metadata.get('thetvdb_id', '')):
if metadata['media_type'] == 'show':
title = metadata['title'] title = metadata['title']
subtitle = metadata['year'] subtitle = metadata['year']
elif metadata['media_type'] == 'episode': elif metadata['media_type'] == 'episode':
@ -2160,30 +2139,10 @@ class FacebookNotifier(object):
subtitle = 'S' + metadata['parent_media_index'] + ' ' + '\xc2\xb7'.decode('utf8') + \ subtitle = 'S' + metadata['parent_media_index'] + ' ' + '\xc2\xb7'.decode('utf8') + \
' E' + metadata['media_index'] ' E' + metadata['media_index']
if metadata.get('imdb_id', ''): if poster_url:
uri = '/lookup/shows?imdb=' + metadata['imdb_id']
elif metadata.get('thetvdb_id', ''):
uri = '/lookup/shows?thetvdb=' + metadata['thetvdb_id']
# Get poster using TVmaze API
request = urllib2.Request('http://api.tvmaze.com' + uri)
opener = urllib2.build_opener(openanything.SmartRedirectHandler())
response = opener.open(request)
request_status = response.status
if request_status == 301:
data = json.loads(response.read())
image = data.get('image', '')
poster = image.get('original', image.get('medium',''))
elif request_status >= 400 and request_status < 500:
logger.warn(u"PlexPy Notifiers :: Unable to retrieve TVmaze poster: %s" % response.reason)
else:
logger.warn(u"PlexPy Notifiers :: Unable to retrieve TVmaze poster.")
if poster and poster != 'N/A':
attachment['link'] = 'http://app.plex.tv/web/app#!/server/' + plexpy.CONFIG.PMS_IDENTIFIER + \ attachment['link'] = 'http://app.plex.tv/web/app#!/server/' + plexpy.CONFIG.PMS_IDENTIFIER + \
'/details/%2Flibrary%2Fmetadata%2F' + metadata['rating_key'] '/details/%2Flibrary%2Fmetadata%2F' + metadata['rating_key']
attachment['picture'] = poster attachment['picture'] = poster_url
attachment['name'] = title attachment['name'] = title
attachment['description'] = subtitle attachment['description'] = subtitle
attachment['caption'] = caption attachment['caption'] = caption