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

@ -156,10 +156,10 @@
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="email_notifier">Email Notification Agent</label>
<label for="newsletter_email_notifier">Email Notification Agent</label>
<div class="row">
<div class="col-md-12">
<select class="form-control" id="email_notifier" name="email_notifier">
<select class="form-control" id="newsletter_email_notifier" name="newsletter_email_notifier">
% for notifier in email_notifiers:
<% selected = 'selected' if notifier['id'] == newsletter['email_config']['notifier'] else '' %>
% if notifier['friendly_name']:
@ -377,7 +377,7 @@
var incl_libraries = $incl_libraries[0].selectize;
incl_libraries.setValue(${json.dumps(next((c['value'] for c in newsletter['config_options'] if c['name'] == 'newsletter_config_incl_libraries'), [])) | n});
$('#email_notifier').change(function () {
$('#newsletter_email_notifier').change(function () {
if ($(this).val() === "0") {
$('#newsletter-email-config').show();
} else {

View file

@ -3,9 +3,12 @@
import plexpy
recently_added = data['recently_added']
base_url = ''
if self_hosted and plexpy.CONFIG.NEWSLETTER_BASE_URL:
base_url = plexpy.CONFIG.NEWSLETTER_BASE_URL + '/'
if plexpy.CONFIG.NEWSLETTER_SELF_HOSTED and plexpy.CONFIG.NEWSLETTER_BASE_URL:
base_url = plexpy.CONFIG.NEWSLETTER_BASE_URL + '/newsletter/image/'
elif plexpy.CONFIG.NEWSLETTER_SELF_HOSTED or preview:
base_url = 'newsletter/image/'
else:
base_url = ''
%>
<!doctype html>
<html>
@ -600,20 +603,19 @@
</div>
<div class="wrapper" style="box-sizing: border-box;padding: 5px;overflow: auto;">
% for movie in recently_added['movie']:
% if loop.index == len(recently_added['movie'])-1 and loop.index % 2 == 0:
<div class="clear" style="clear: both;"></div>
<div class="card-instance movie odd" style="float: none !important;width: 500px;margin: 3px auto !important;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 235px;">
% else:
<div class="card-instance movie" style="float: left;width: 500px;margin: 3px;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 235px;">
% endif
% if self_hosted:
<div class="card-background" style="background-image: url(${base_url + 'pms_image_proxy?img=' + movie['art'] + '&width=500&height=280&opacity=25&background=282828&blur=3&fallback=art&refresh=true'});background-position: center;background-size: cover;width: 100%;height: 100%;">
% else:
<div class="card-background" style="background-image: url(${movie['art_url']});background-position: center;background-size: cover;width: 100%;height: 100%;">
% endif
<%
if loop.index == len(recently_added['movie'])-1 and loop.index % 2 == 0:
clear = '<div class="clear"></div>'
odd = 'odd'
else:
clear = odd = ''
%>
${clear}
<div class="card-instance movie ${odd}" style="float: left;width: 500px;margin: 3px;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 235px;">
<div class="card-background" style="background-image: url(${(base_url + movie['art_hash']) if base_url else movie['art_url']});background-position: center;background-size: cover;width: 100%;height: 100%;">
<div class="card-poster-container" style="width: 150px;margin: 3px;border: 1px solid rgba(255,255,255,.1);float: left;position: relative;z-index: 1;height: 225px;">
<a href="${parameters['pms_web_url']}#!/server/${parameters['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${movie['rating_key']}" title="${movie['title']}" target="_blank" style="color: #3498db;text-decoration: underline;">
<div class="card-poster" style="background-image: url(${base_url + 'pms_image_proxy?img=' + movie['thumb'] + '&width=300&height=450&fallback=poster&refresh=true' if self_hosted else movie['poster_url']});background-color: #3F4245;background-position: center;background-size: cover;height: 100%;width: 100%;">
<div class="card-poster" style="background-image: url(${(base_url + movie['thumb_hash']) if base_url else movie['poster_url']});background-color: #3F4245;background-position: center;background-size: cover;height: 100%;width: 100%;">
<div class="card-poster-overlay" style="background: url(https://cdn.discordapp.com/attachments/334355557706235906/401064397780287489/newsletter-view-on-plex-flat.png) bottom right no-repeat;width: 100%;height: 100%;"></div>
</div>
</a>
@ -682,21 +684,19 @@
else:
link_rating_key = show['rating_key']
link_title = show['title']
if loop.index == len(recently_added['show'])-1 and loop.index % 2 == 0:
clear = '<div class="clear"></div>'
odd = 'odd'
else:
clear = odd = ''
%>
% if loop.index == len(recently_added['show'])-1 and loop.index % 2 == 0:
<div class="clear" style="clear: both;"></div>
<div class="card-instance show odd" style="float: none !important;width: 500px;margin: 3px auto !important;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 235px;">
% else:
<div class="card-instance show" style="float: left;width: 500px;margin: 3px;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 235px;">
% endif
% if self_hosted:
<div class="card-background" style="background-image: url(${base_url + 'pms_image_proxy?img=' + show['art'] + '&width=500&height=280&opacity=25&background=282828&blur=3&fallback=art&refresh=true'});background-position: center;background-size: cover;width: 100%;height: 100%;">
% else:
<div class="card-background" style="background-image: url(${show['art_url']});background-position: center;background-size: cover;width: 100%;height: 100%;">
% endif
${clear}
<div class="card-instance show ${odd}" style="float: left;width: 500px;margin: 3px;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 235px;">
<div class="card-background" style="background-image: url(${(base_url + show['art_hash']) if base_url else show['art_url']});background-position: center;background-size: cover;width: 100%;height: 100%;">
<div class="card-poster-container" style="width: 150px;margin: 3px;border: 1px solid rgba(255,255,255,.1);float: left;position: relative;z-index: 1;height: 225px;">
<a href="${parameters['pms_web_url']}#!/server/${parameters['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${link_rating_key}" title="${link_title}" target="_blank" style="color: #3498db;text-decoration: underline;">
<div class="card-poster" style="background-image: url(${base_url + 'pms_image_proxy?img=' + show['thumb'] + '&width=300&height=450&fallback=poster&refresh=true' if self_hosted else show['poster_url']});background-color: #3F4245;background-position: center;background-size: cover;height: 100%;width: 100%;">
<div class="card-poster" style="background-image: url(${(base_url + show['thumb_hash']) if base_url else show['poster_url']});background-color: #3F4245;background-position: center;background-size: cover;height: 100%;width: 100%;">
<div class="card-poster-overlay" style="background: url(https://cdn.discordapp.com/attachments/334355557706235906/401064397780287489/newsletter-view-on-plex-flat.png) bottom right no-repeat;width: 100%;height: 100%;"></div>
</div>
</a>
@ -787,21 +787,21 @@
<% album_count = 0 %>
% for artist in recently_added['artist']:
% for album in artist['album']:
<% album_count += 1 %>
% if album_count == total_albums and album_count % 2 == 1:
<div class="clear" style="clear: both;"></div>
<div class="card-instance album odd" style="float: none !important;width: 500px;margin: 3px auto !important;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 160px;">
% else:
<div class="card-instance album" style="float: left;width: 500px;margin: 3px;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 160px;">
% endif
% if self_hosted:
<div class="card-background" style="background-image: url(${base_url + 'pms_image_proxy?img=' + album['art'] + '&width=500&height=280&opacity=25&background=282828&blur=3&fallback=art&refresh=true'});background-position: center;background-size: cover;width: 100%;height: 100%;">
% else:
<div class="card-background" style="background-image: url(${album['art_url']});background-position: center;background-size: cover;width: 100%;height: 100%;">
% endif
<%
album_count += 1
if album_count == total_albums and album_count % 2 == 1:
clear = '<div class="clear"></div>'
odd = 'odd'
else:
clear = odd = ''
%>
${clear}
<div class="card-instance album ${odd}" style="float: left;width: 500px;margin: 3px;border: 1px solid rgba(255,255,255,.1);-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 12px;overflow: hidden;position: relative;height: 160px;">
<div class="card-background" style="background-image: url(${(base_url + album['art_hash']) if base_url else album['art_url']});background-position: center;background-size: cover;width: 100%;height: 100%;">
<div class="card-poster-container" style="width: 150px;margin: 3px;border: 1px solid rgba(255,255,255,.1);float: left;position: relative;z-index: 1;height: 150px;">
<a href="${parameters['pms_web_url']}#!/server/${parameters['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${album['rating_key']}" title="${album['title']}" target="_blank" style="color: #3498db;text-decoration: underline;">
<div class="card-poster" style="background-image: url(${base_url + 'pms_image_proxy?img=' + album['thumb'] + '&width=300&height=450&fallback=poster&refresh=true' if self_hosted else album['poster_url']});background-color: #3F4245;background-position: center;background-size: cover;height: 100%;width: 100%;">
<div class="card-poster" style="background-image: url(${(base_url + album['thumb_hash']) if base_url else album['poster_url']});background-color: #3F4245;background-position: center;background-size: cover;height: 100%;width: 100%;">
<div class="card-poster-overlay" style="background: url(https://cdn.discordapp.com/attachments/334355557706235906/401064397780287489/newsletter-view-on-plex-flat.png) bottom right no-repeat;width: 100%;height: 100%;"></div>
</div>
</a>

View file

@ -3,9 +3,12 @@
import plexpy
recently_added = data['recently_added']
base_url = ''
if self_hosted and plexpy.CONFIG.NEWSLETTER_BASE_URL:
base_url = plexpy.CONFIG.NEWSLETTER_BASE_URL + '/'
if plexpy.CONFIG.NEWSLETTER_SELF_HOSTED and plexpy.CONFIG.NEWSLETTER_BASE_URL:
base_url = plexpy.CONFIG.NEWSLETTER_BASE_URL + '/newsletter/image/'
elif plexpy.CONFIG.NEWSLETTER_SELF_HOSTED or preview:
base_url = 'newsletter/image/'
else:
base_url = ''
%>
<!doctype html>
<html>
@ -600,20 +603,19 @@
</div>
<div class="wrapper">
% for movie in recently_added['movie']:
% if loop.index == len(recently_added['movie'])-1 and loop.index % 2 == 0:
<div class="clear"></div>
<div class="card-instance movie odd">
% else:
<div class="card-instance movie">
% endif
% if self_hosted:
<div class="card-background" style="background-image: url(${base_url + 'pms_image_proxy?img=' + movie['art'] + '&width=500&height=280&opacity=25&background=282828&blur=3&fallback=art&refresh=true'});">
% else:
<div class="card-background" style="background-image: url(${movie['art_url']});">
% endif
<%
if loop.index == len(recently_added['movie'])-1 and loop.index % 2 == 0:
clear = '<div class="clear"></div>'
odd = 'odd'
else:
clear = odd = ''
%>
${clear}
<div class="card-instance movie ${odd}">
<div class="card-background" style="background-image: url(${(base_url + movie['art_hash']) if base_url else movie['art_url']});">
<div class="card-poster-container">
<a href="${parameters['pms_web_url']}#!/server/${parameters['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${movie['rating_key']}" title="${movie['title']}" target="_blank">
<div class="card-poster" style="background-image: url(${base_url + 'pms_image_proxy?img=' + movie['thumb'] + '&width=300&height=450&fallback=poster&refresh=true' if self_hosted else movie['poster_url']})">
<div class="card-poster" style="background-image: url(${(base_url + movie['thumb_hash']) if base_url else movie['poster_url']})">
<div class="card-poster-overlay"></div>
</div>
</a>
@ -682,21 +684,19 @@
else:
link_rating_key = show['rating_key']
link_title = show['title']
if loop.index == len(recently_added['show'])-1 and loop.index % 2 == 0:
clear = '<div class="clear"></div>'
odd = 'odd'
else:
clear = odd = ''
%>
% if loop.index == len(recently_added['show'])-1 and loop.index % 2 == 0:
<div class="clear"></div>
<div class="card-instance show odd">
% else:
<div class="card-instance show">
% endif
% if self_hosted:
<div class="card-background" style="background-image: url(${base_url + 'pms_image_proxy?img=' + show['art'] + '&width=500&height=280&opacity=25&background=282828&blur=3&fallback=art&refresh=true'});">
% else:
<div class="card-background" style="background-image: url(${show['art_url']});">
% endif
${clear}
<div class="card-instance show ${odd}">
<div class="card-background" style="background-image: url(${(base_url + show['art_hash']) if base_url else show['art_url']});">
<div class="card-poster-container">
<a href="${parameters['pms_web_url']}#!/server/${parameters['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${link_rating_key}" title="${link_title}" target="_blank">
<div class="card-poster" style="background-image: url(${base_url + 'pms_image_proxy?img=' + show['thumb'] + '&width=300&height=450&fallback=poster&refresh=true' if self_hosted else show['poster_url']});">
<div class="card-poster" style="background-image: url(${(base_url + show['thumb_hash']) if base_url else show['poster_url']});">
<div class="card-poster-overlay"></div>
</div>
</a>
@ -787,21 +787,21 @@
<% album_count = 0 %>
% for artist in recently_added['artist']:
% for album in artist['album']:
<% album_count += 1 %>
% if album_count == total_albums and album_count % 2 == 1:
<div class="clear"></div>
<div class="card-instance album odd">
% else:
<div class="card-instance album">
% endif
% if self_hosted:
<div class="card-background" style="background-image: url(${base_url + 'pms_image_proxy?img=' + album['art'] + '&width=500&height=280&opacity=25&background=282828&blur=3&fallback=art&refresh=true'});">
% else:
<div class="card-background" style="background-image: url(${album['art_url']});">
% endif
<%
album_count += 1
if album_count == total_albums and album_count % 2 == 1:
clear = '<div class="clear"></div>'
odd = 'odd'
else:
clear = odd = ''
%>
${clear}
<div class="card-instance album ${odd}">
<div class="card-background" style="background-image: url(${(base_url + album['art_hash']) if base_url else album['art_url']});">
<div class="card-poster-container">
<a href="${parameters['pms_web_url']}#!/server/${parameters['pms_identifier']}/details?key=%2Flibrary%2Fmetadata%2F${album['rating_key']}" title="${album['title']}" target="_blank">
<div class="card-poster" style="background-image: url(${base_url + 'pms_image_proxy?img=' + album['thumb'] + '&width=300&height=450&fallback=poster&refresh=true' if self_hosted else album['poster_url']});">
<div class="card-poster" style="background-image: url(${(base_url + album['thumb_hash']) if base_url else album['poster_url']});">
<div class="card-poster-overlay"></div>
</div>
</a>

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