From dd3f75f1540b74d62bd5660b600fe01b36dde79e Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Thu, 23 Aug 2018 19:12:22 -0700 Subject: [PATCH] Add return_hash to pms_image_proxy API --- API.md | 10 +++++++--- plexpy/api2.py | 5 +++-- plexpy/notification_handler.py | 26 ++++++++++++++------------ plexpy/webserve.py | 15 +++++++++++---- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/API.md b/API.md index 7bfb63dc..01cbc6e2 100644 --- a/API.md +++ b/API.md @@ -1140,7 +1140,8 @@ Returns: "video_language_code": "", "video_profile": "high", "video_ref_frames": "4", - "video_width": "1920" + "video_width": "1920", + "selected": 0 }, { "audio_bitrate": "384", @@ -1153,7 +1154,8 @@ Returns: "audio_profile": "", "audio_sample_rate": "48000", "id": "511664", - "type": "2" + "type": "2", + "selected": 1 }, { "id": "511953", @@ -1164,7 +1166,8 @@ Returns: "subtitle_language": "English", "subtitle_language_code": "eng", "subtitle_location": "external", - "type": "3" + "type": "3", + "selected": 1 } ] } @@ -2496,6 +2499,7 @@ Optional parameters: img_format (str): png fallback (str): "poster", "cover", "art" refresh (bool): True or False whether to refresh the image cache + return_hash (bool): True or False to return the self-hosted image hash instead of the image Returns: None diff --git a/plexpy/api2.py b/plexpy/api2.py index 3b26b347..17108a1b 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -596,8 +596,9 @@ General optional parameters: return elif self._api_cmd == 'pms_image_proxy': - cherrypy.response.headers['Content-Type'] = 'image/jpeg' - return out['response']['data'] + if 'return_hash' not in self._api_kwargs: + cherrypy.response.headers['Content-Type'] = 'image/jpeg' + return out['response']['data'] if self._api_out_type == 'json': cherrypy.response.headers['Content-Type'] = 'application/json;charset=UTF-8' diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 67e9eb18..beb97165 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -1256,7 +1256,8 @@ def get_img_info(img=None, rating_key=None, title='', width=1000, height=1500, def set_hash_image_info(img=None, rating_key=None, width=750, height=1000, - opacity=100, background='000000', blur=0, fallback=None): + opacity=100, background='000000', blur=0, fallback=None, + add_to_db=True): if not rating_key and not img: return fallback @@ -1274,18 +1275,19 @@ def set_hash_image_info(img=None, rating_key=None, width=750, height=1000, plexpy.CONFIG.PMS_UUID, img, rating_key, width, height, opacity, background, blur, fallback) img_hash = hashlib.sha256(img_string).hexdigest() - keys = {'img_hash': img_hash} - values = {'img': img, - 'rating_key': rating_key, - 'width': width, - 'height': height, - 'opacity': opacity, - 'background': background, - 'blur': blur, - 'fallback': fallback} + if add_to_db: + keys = {'img_hash': img_hash} + values = {'img': img, + 'rating_key': rating_key, + 'width': width, + 'height': height, + 'opacity': opacity, + 'background': background, + 'blur': blur, + 'fallback': fallback} - db = database.MonitorDatabase() - db.upsert('image_hash_lookup', key_dict=keys, value_dict=values) + db = database.MonitorDatabase() + db.upsert('image_hash_lookup', key_dict=keys, value_dict=values) return img_hash diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 029cf07b..284b9846 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -4031,7 +4031,7 @@ class WebInterface(object): return self.real_pms_image_proxy(**kwargs) @addtoapi('pms_image_proxy') - def real_pms_image_proxy(self, img='', rating_key=None, width=0, height=0, + def real_pms_image_proxy(self, img=None, rating_key=None, width=750, height=1000, opacity=100, background='000000', blur=0, img_format='png', fallback=None, refresh=False, clip=False, **kwargs): """ Gets an image from the PMS and saves it to the image cache directory. @@ -4051,6 +4051,7 @@ class WebInterface(object): img_format (str): png fallback (str): "poster", "cover", "art" refresh (bool): True or False whether to refresh the image cache + return_hash (bool): True or False to return the self-hosted image hash instead of the image Returns: None @@ -4060,6 +4061,8 @@ class WebInterface(object): logger.warn('No image input received.') return + return_hash = (kwargs.get('return_hash') == 'true') + if rating_key and not img: if fallback == 'art': img = '/library/metadata/{}/art'.format(rating_key) @@ -4070,9 +4073,13 @@ class WebInterface(object): img = '/'.join(img_split[:5]) rating_key = rating_key or img_split[3] - img_string = '{}.{}.{}.{}.{}.{}.{}.{}'.format( - plexpy.CONFIG.PMS_UUID, img, rating_key, width, height, opacity, background, blur, fallback) - img_hash = hashlib.sha256(img_string).hexdigest() + img_hash = notification_handler.set_hash_image_info( + img=img, rating_key=rating_key, width=width, height=height, + opacity=opacity, background=background, blur=blur, fallback=fallback, + add_to_db=return_hash) + + if return_hash: + return {'img_hash': img_hash} fp = '{}.{}'.format(img_hash, img_format) # we want to be able to preview the thumbs c_dir = os.path.join(plexpy.CONFIG.CACHE_DIR, 'images')