Store image hash for self-hosted newsletters

This commit is contained in:
JonnyWong16 2018-03-20 22:54:36 -07:00
parent e9bdbb863c
commit 7f67213ff7
8 changed files with 188 additions and 125 deletions

View file

@ -690,6 +690,13 @@ def dbcheck():
'themoviedb_id INTEGER, themoviedb_url TEXT, themoviedb_json TEXT)'
)
# image_hash_lookup table :: This table keeps record of the image hash lookups
c_db.execute(
'CREATE TABLE IF NOT EXISTS image_hash_lookup (id INTEGER PRIMARY KEY AUTOINCREMENT, '
'img_hash TEXT, img TEXT, rating_key INTEGER, width INTEGER, height INTEGER, '
'opacity INTEGER, background TEXT, blur INTEGER, fallback TEXT)'
)
# Upgrade sessions table from earlier versions
try:
c_db.execute('SELECT started FROM sessions')

View file

@ -392,7 +392,7 @@ class Newsletter(object):
title=self.subject_formatted,
parameters=self.parameters,
data=self.data,
self_hosted=self.is_preview or plexpy.CONFIG.NEWSLETTER_SELF_HOSTED
preview=self.is_preview
)
def send(self):
@ -640,7 +640,7 @@ class RecentlyAdded(Newsletter):
return recently_added
def retrieve_data(self):
from notification_handler import get_poster_info
from notification_handler import get_poster_info, set_hash_image_info
if not self.config['incl_libraries']:
logger.warn(u"Tautulli Newsletters :: Failed to retrieve %s newsletter data: no libraries selected." % self.NAME)
@ -653,31 +653,50 @@ class RecentlyAdded(Newsletter):
if media_type not in recently_added:
recently_added[media_type] = self._get_recently_added(media_type)
if not self.is_preview:
movies = recently_added.get('movie', [])
shows = recently_added.get('show', [])
artists = recently_added.get('artist', [])
albums = [a for artist in artists for a in artist['album']]
if self.is_preview or plexpy.CONFIG.NEWSLETTER_SELF_HOSTED:
for item in movies + shows + albums:
item['thumb_hash'] = set_hash_image_info(img=item['thumb'],
width=300,
height=450,
fallback='poster')
item['art_hash'] = set_hash_image_info(img=item['art'],
width=500,
height=280,
opacity=25,
background='282828',
blur=3,
fallback='art')
item['poster_url'] = ''
item['art_url'] = ''
else:
# Upload posters and art to Imgur
movies = recently_added.get('movie', [])
shows = recently_added.get('show', [])
artists = recently_added.get('artist', [])
albums = [a for artist in artists for a in artist['album']]
for item in movies + shows + albums:
poster_info = get_poster_info(poster_thumb=item['thumb'],
poster_key=item['rating_key'],
poster_title=item['title'])
if poster_info:
item['poster_url'] = poster_info['poster_url'] or common.ONLINE_POSTER_THUMB
if not plexpy.CONFIG.NEWSLETTER_SELF_HOSTED:
for item in movies + shows + albums:
poster_info = get_poster_info(poster_thumb=item['thumb'],
poster_key=item['rating_key'],
poster_title=item['title'])
if poster_info:
item['poster_url'] = poster_info['poster_url'] or common.ONLINE_POSTER_THUMB
art_info = get_poster_info(poster_thumb=item['art'],
poster_key=item['rating_key'],
poster_title=item['title'],
art=True,
width='500',
height='280',
opacity='25',
background='282828',
blur='3')
item['art_url'] = art_info.get('art_url', '')
art_info = get_poster_info(poster_thumb=item['art'],
poster_key=item['rating_key'],
poster_title=item['title'],
art=True,
width='500',
height='280',
opacity='25',
background='282828',
blur='3')
item['art_url'] = art_info.get('art_url', '')
item['thumb_hash'] = ''
item['art_hash'] = ''
self.data['recently_added'] = recently_added

View file

@ -17,6 +17,7 @@
import arrow
import bleach
from collections import Counter, defaultdict
import hashlib
from itertools import groupby
import json
from operator import itemgetter
@ -1127,6 +1128,41 @@ def get_poster_info(poster_thumb='', poster_key='', poster_title='', art=False,
return poster_info or default_poster_info
def set_hash_image_info(img=None, rating_key=None, width=600, height=1000,
opacity=100, background='000000', blur=0, fallback=None):
if rating_key and not img:
img = '/library/metadata/{}/thumb'.format(rating_key)
img_split = img.split('/')
img = '/'.join(img_split[:5])
rating_key = rating_key or img_split[3]
img_string = '{}{}{}{}{}{}{}'.format(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}
db = database.MonitorDatabase()
db.upsert('image_hash_lookup', key_dict=keys, value_dict=values)
return img_hash
def get_hash_image_info(img_hash=None):
db = database.MonitorDatabase()
query = 'SELECT * FROM image_hash_lookup WHERE img_hash = ?'
result = db.select_single(query, args=[img_hash])
return result
def lookup_tvmaze_by_id(rating_key=None, thetvdb_id=None, imdb_id=None):
db = database.MonitorDatabase()

View file

@ -2425,8 +2425,8 @@ class PmsConnect(object):
return labels_list
def get_image(self, img=None, width='1000', height='1500', opacity=None, background=None, blur=None, img_format='png',
clip=False):
def get_image(self, img=None, width=600, height=1000, opacity=None, background=None, blur=None,
img_format='png', clip=False):
"""
Return image data as array.
Array contains the image content type and image binary
@ -2440,8 +2440,8 @@ class PmsConnect(object):
Output: array
"""
width = width or '1000'
height = height or '1500'
width = width or 600
height = height or 1000
if img:
if clip:

View file

@ -3893,7 +3893,6 @@ class WebInterface(object):
return {'result': 'error', 'message': 'Notification failed.'}
@cherrypy.expose
@requireAuth()
def pms_image_proxy(self, **kwargs):
""" See real_pms_image_proxy docs string"""
@ -3906,8 +3905,8 @@ 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',
opacity=None, background=None, blur=None, img_format='png',
def real_pms_image_proxy(self, img='', rating_key=None, width=0, height=0,
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.
@ -3939,17 +3938,7 @@ class WebInterface(object):
img = '/library/metadata/%s/thumb/1337' % rating_key
img_string = img.rsplit('/', 1)[0] if '/library/metadata' in img else img
if width:
img_string += width
if height:
img_string += height
if opacity:
img_string += opacity
if background:
img_string += background
if blur:
img_string += blur
img_string = '{}{}{}{}{}{}'.format(img_string, width, height, opacity, background, blur)
fp = hashlib.md5(img_string).hexdigest()
fp += '.%s' % img_format # we want to be able to preview the thumbs
@ -4004,6 +3993,15 @@ class WebInterface(object):
fp = os.path.join(plexpy.PROG_DIR, 'data', fbi)
return serve_file(path=fp, content_type='image/png')
@cherrypy.expose
def image(self, *args, **kwargs):
if args:
img_hash = args[0]
img_info = notification_handler.get_hash_image_info(img_hash=img_hash)
kwargs.update(img_info)
return self.real_pms_image_proxy(**kwargs)
return
@cherrypy.expose
@requireAuth(member_of("admin"))
@ -5603,6 +5601,9 @@ class WebInterface(object):
@cherrypy.expose
def newsletter(self, *args, **kwargs):
if args:
if len(args) >= 2 and args[0] == 'image':
return self.image(args[1], refresh=True)
newsletter_uuid = args[0]
newsletter = newsletter_handler.get_newsletter(newsletter_uuid=newsletter_uuid)
return newsletter