Permalink
Browse files

memento services allow data uris for favicons and images

  • Loading branch information...
shawnmjones committed Jul 28, 2018
1 parent 2634843 commit 57439138d8089e7847e042330bf2c6086bbb46f9
Showing with 61 additions and 11 deletions.
  1. +1 −0 mementoembed/imageselection.py
  2. +58 −9 mementoembed/services/memento.py
  3. +2 −2 sample_appconfig.cfg
@@ -14,6 +14,7 @@
def convert_imageuri_to_pngdata_uri(imageuri, httpcache, width, height=None):
# TODO: throw an exception when things go wrong, what could go wrong?
response = httpcache.get(imageuri)
imagedata = response.content
@@ -13,7 +13,7 @@
from mementoembed.textprocessing import extract_text_snippet, extract_title
from mementoembed.cachesession import CacheSession
from mementoembed.archiveresource import ArchiveResource
from mementoembed.imageselection import get_best_image
from mementoembed.imageselection import get_best_image, convert_imageuri_to_pngdata_uri
from mementoembed.version import __useragent__
from .errors import handle_errors
@@ -59,12 +59,19 @@ def originaldata(urim, preferences):
originalresource = OriginalResource(memento, httpcache)
if preferences['datauri_favicon'].lower() == 'yes':
original_favicon = convert_imageuri_to_pngdata_uri(
originalresource.favicon, httpcache, 16, 16
)
else:
original_favicon = originalresource.favicon
output['urim'] = urim
output['generation-time'] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
output['original-uri'] = originalresource.uri
output['original-domain'] = originalresource.domain
output['original-favicon'] = originalresource.favicon
output['original-favicon'] = original_favicon
output['original-linkstatus'] = originalresource.link_status
response = make_response(json.dumps(output, indent=4))
@@ -83,6 +90,11 @@ def bestimage(urim, preferences):
best_image_uri = get_best_image(memento.urim, httpcache)
if preferences['datauri_image'].lower() == 'yes':
best_image_uri = convert_imageuri_to_pngdata_uri(
best_image_uri, httpcache, 96
)
output = {}
output['urim'] = urim
@@ -106,14 +118,21 @@ def archivedata(urim, preferences):
archive = ArchiveResource(urim, httpcache)
if preferences['datauri_favicon'].lower() == 'yes':
archive_favicon = convert_imageuri_to_pngdata_uri(
archive.favicon, httpcache, 16, 16
)
else:
archive_favicon = archive.favicon
output = {}
output['urim'] = urim
output['generation-time'] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
output['archive-uri'] = archive.home_uri
output['archive-name'] = archive.name
output['archive-favicon'] = archive.favicon
output['archive-favicon'] = archive_favicon
output['archive-collection-id'] = archive.collection_id
output['archive-collection-name'] = archive.collection_name
output['archive-collection-uri'] = archive.collection_uri
@@ -140,17 +159,47 @@ def textinformation_endpoint(subpath):
@bp.route('/services/memento/bestimage/<path:subpath>')
def bestimage_endpoint(subpath):
urim = subpath
preferences = {}
return handle_errors(bestimage, urim, preferences)
prefs = {}
prefs['datauri_image'] = 'no'
if 'Prefer' in request.headers:
preferences = request.headers['Prefer'].split(',')
for pref in preferences:
key, value = pref.split('=')
prefs[key] = value.lower()
return handle_errors(bestimage, urim, prefs)
@bp.route('/services/memento/archivedata/<path:subpath>')
def archivedata_endpoint(subpath):
urim = subpath
preferences = {}
return handle_errors(archivedata, urim, preferences)
prefs = {}
prefs['datauri_favicon'] = 'no'
if 'Prefer' in request.headers:
preferences = request.headers['Prefer'].split(',')
for pref in preferences:
key, value = pref.split('=')
prefs[key] = value.lower()
return handle_errors(archivedata, urim, prefs)
@bp.route('/services/memento/originalresourcedata/<path:subpath>')
def originaldata_endpoint(subpath):
urim = subpath
preferences = {}
return handle_errors(originaldata, urim, preferences)
prefs = {}
prefs['datauri_favicon'] = 'no'
if 'Prefer' in request.headers:
preferences = request.headers['Prefer'].split(',')
for pref in preferences:
key, value = pref.split('=')
prefs[key] = value.lower()
return handle_errors(originaldata, urim, prefs)
View
@@ -72,9 +72,9 @@ REQUEST_TIMEOUT = "15"
# --- SOCIAL CARD SETTINGS ---
# Should this service use data URIs for striking images
SOCIALCARD_DATAURIS_FOR_IMAGE = "No"
ALLOW_SOCIALCARD_DATAURIS_FOR_IMAGE = "No"
# Should this service use data URIs for favicons
SOCIALCARD_DATAURIS_FOR_FAVICONS = "No"
ALLOW_SOCIALCARD_DATAURIS_FOR_FAVICONS = "No"

0 comments on commit 5743913

Please sign in to comment.