Caches pms images to disk

This commit is contained in:
JonnyWong16 2016-05-02 22:08:06 -07:00
commit 7be651f5cf
3 changed files with 79 additions and 37 deletions

View file

@ -1886,7 +1886,7 @@ class PmsConnect(object):
return labels_list
def get_image(self, img=None, width=None, height=None):
def get_image(self, img=None, width=None, height=None, fallback=None):
"""
Return image data as array.
Array contains the image content type and image binary
@ -1896,21 +1896,51 @@ class PmsConnect(object):
height { the image height }
Output: array
"""
if img:
if not img:
logger.error(u"PlexPy Pmsconnect :: Image proxy queried but no input received.")
return None
# Remove the timestamp from PMS image uri
image_uri = img.rsplit('/', 1)[0]
# Try to retrieve the image from cache if it isn't a bif index.
if not 'indexes' in image_uri:
image_path, content_type = helpers.cache_image(image_uri)
if image_path and content_type:
return [open(image_path, 'rb'), content_type]
try:
if width.isdigit() and height.isdigit():
uri = '/photo/:/transcode?url=http://127.0.0.1:32400' + img + '&width=' + width + '&height=' + height
else:
uri = '/photo/:/transcode?url=http://127.0.0.1:32400' + img
logger.error(u"PlexPy Pmsconnect :: Image proxy queried but no width or height specified.")
raise Exception
request, content_type = self.request_handler.make_request(uri=uri,
proto=self.protocol,
request_type='GET',
return_type=True)
# Save the image to cache if it isn't a bif index.
if not 'indexes' in image_uri:
helpers.cache_image(image_uri, request)
return [request, content_type]
else:
logger.error(u"PlexPy Pmsconnect :: Image proxy queries but no input received.")
return None
except Exception as e:
if fallback:
logger.debug(u"PlexPy Pmsconnect :: Trying fallback %s image." % fallback)
try:
if fallback == 'poster':
return [open(common.DEFAULT_POSTER_THUMB, 'rb'), 'image/png']
elif fallback == 'cover':
return [open(common.DEFAULT_COVER_THUMB, 'rb'), 'image/png']
elif fallback == 'art':
return [open(common.DEFAULT_ART, 'rb'), 'image/png']
except IOError as e:
logger.error(u"PlexPy Pmsconnect :: Unable to read fallback %s image: %s" % (fallback, e))
return None
def get_search_results(self, query=''):
"""