Permalink
Browse files

now a configuragle default image is displayed when a striking image c…

…annot be found for a social card
  • Loading branch information...
shawnmjones committed Oct 11, 2018
1 parent e0eef1e commit 7e720c1da5d55fc0970ca43f44ffd838da4739ba
View
@@ -26,7 +26,7 @@
# The short X.Y version
version = ''
# The full version, including alpha/beta/rc tags
release = '0.2018.10.11.193540'
release = '0.2018.10.11.222821'
# -- General configuration ---------------------------------------------------
View
@@ -14,7 +14,7 @@ CACHEHOST = "localhost"
CACHEPORT = "6379"
# CACHEDB only has meaning for Redis, specifying the Redis database to use
CACHEDB = "1"
CACHEDB = "2"
# CACHE_EXPIRETIME indicates how often to expire entries in the cache
CACHE_EXPIRETIME = "604800"
@@ -48,3 +48,9 @@ ACCESS_LOGFILE = "/app/logs/mementoembed-access.log"
# This value indicates how long, in seconds, the system should wait for a
# server to respond to an HTTP request
REQUEST_TIMEOUT = "15"
# Default image to use if no other image can be found
DEFAULT_IMAGE_PATH = "mementoembed/static/images/96px-Sphere_wireframe.svg.png"
USE_DATA_URI_FOR_DEFAULT_IMAGE = "Yes"
View
@@ -2,6 +2,7 @@
import os
import json
import logging
import base64
import redis
import requests
@@ -219,6 +220,21 @@ def create_app():
app.config['REQUEST_TIMEOUT_FLOAT'] = get_requests_timeout(app.config)
application_logger.info("Requests timeout is set to {}".format(app.config['REQUEST_TIMEOUT_FLOAT']))
application_logger.info("Default image path set to {}".format(app.config['DEFAULT_IMAGE_PATH']))
application_logger.info("Use data URIs for default image: {}".format(app.config['USE_DATA_URI_FOR_DEFAULT_IMAGE']))
if app.config['USE_DATA_URI_FOR_DEFAULT_IMAGE'].lower() == "yes":
application_logger.info("Opening default image for conversion to data URI")
with open(app.config['DEFAULT_IMAGE_PATH'], 'rb') as f:
imgdata = f.read()
application_logger.info("Default image has been opened and stored")
application_logger.info("Converting image data to a base64 data URI")
app.config['DEFAULT_IMAGE_PATH'] = "data:png;base64,{}".format( base64.b64encode(imgdata).decode('utf-8') )
application_logger.info("Done with image conversion")
application_logger.debug("Default image path now set to {}".format(app.config['DEFAULT_IMAGE_PATH']))
application_logger.info("All Configuration successfully loaded for MementoEmbed")
from .services import oembed, memento, product
@@ -107,7 +107,7 @@ def get_image_list(uri, http_cache):
return image_list
def get_best_image(uri, http_cache):
def get_best_scoring_image(uri, http_cache):
module_logger.debug("getting the best image for content at URI {}".format(uri))
@@ -192,3 +192,13 @@ def get_best_image(uri, http_cache):
start += 15
return max_score_image
def get_best_image(uri, http_cache, default_image_uri=None):
best_image_uri = get_best_scoring_image(uri, http_cache)
if best_image_uri == None:
if default_image_uri != None:
best_image_uri = default_image_uri
return best_image_uri
@@ -16,7 +16,7 @@ class MementoSurrogate:
all information about surrogates
related to content, uri, and response_headers.
"""
def __init__(self, urim, httpcache, working_directory="/tmp/mementosurrogate"):
def __init__(self, urim, httpcache, working_directory="/tmp/mementosurrogate", default_image_uri=None):
self.surrogate_creation_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
self.urim = urim
@@ -29,6 +29,8 @@ def __init__(self, urim, httpcache, working_directory="/tmp/mementosurrogate"):
self.archive = ArchiveResource(self.urim, self.httpcache)
self.default_image_uri = default_image_uri
@property
def creation_time(self):
return self.surrogate_creation_time
@@ -52,7 +54,7 @@ def memento_datetime(self):
@property
def striking_image(self):
self.logger.info("looking for the best image in the memento")
return get_best_image(self.memento.urim, self.httpcache)
return get_best_image(self.memento.urim, self.httpcache, default_image_uri=self.default_image_uri)
@property
def original_uri(self):
@@ -93,16 +93,16 @@ def bestimage(urim, preferences):
memento = memento_resource_factory(urim, httpcache)
best_image_uri = get_best_image(memento.urim, httpcache)
if best_image_uri == None:
best_image_uri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='
else:
if preferences['datauri_image'].lower() == 'yes':
best_image_uri = convert_imageuri_to_pngdata_uri(
best_image_uri, httpcache, 96
)
best_image_uri = get_best_image(
memento.urim,
httpcache,
current_app.config['DEFAULT_IMAGE_PATH']
)
if preferences['datauri_image'].lower() == 'yes':
best_image_uri = convert_imageuri_to_pngdata_uri(
best_image_uri, httpcache, 96
)
output = {}
@@ -89,7 +89,8 @@ def generate_socialcard_response(urim, preferences):
s = MementoSurrogate(
urim,
httpcache
httpcache,
default_image_uri=current_app.config['DEFAULT_IMAGE_PATH']
)
urlroot = request.url_root
@@ -99,10 +100,8 @@ def generate_socialcard_response(urim, preferences):
original_favicon_uri = s.original_favicon
striking_image_uri = s.striking_image
if striking_image_uri == None:
striking_image_uri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='
else:
if preferences['datauri_image'].lower() == 'yes':
if preferences['datauri_image'].lower() == 'yes':
if striking_image_uri[0:5] != 'data:':
striking_image_uri = convert_imageuri_to_pngdata_uri(
striking_image_uri, httpcache, 96
)
Binary file not shown.
View
@@ -1,3 +1,3 @@
__appname__ = "MementoEmbed"
__appversion__ = '0.2018.10.11.193540'
__appversion__ = '0.2018.10.11.222821'
__useragent__ = "{}/{}".format(__appname__, __appversion__)
View
@@ -77,4 +77,5 @@ ALLOW_SOCIALCARD_DATAURIS_FOR_IMAGE = "No"
# Should this service use data URIs for favicons
ALLOW_SOCIALCARD_DATAURIS_FOR_FAVICONS = "No"
# Default image to use if no other image can be found
DEFAULT_IMAGE_PATH = "mementoembed/static/images/96px-Sphere_wireframe.svg.png"

0 comments on commit 7e720c1

Please sign in to comment.