Permalink
Browse files

restructured the app based on the Flask tutorial, cleaned up unused code

  • Loading branch information...
shawnmjones committed May 28, 2018
1 parent 7592549 commit 443e2634c1c4d2277032009e337f173b209022bb
View
@@ -1,5 +1,9 @@
FROM python:3.6.4-stretch
# the flask documentation recommends WSGI server waitress for use
RUN pip install waitress
# TODO: publish archiveit_utilities so that we don't need to do this
RUN git clone https://github.com/shawnmjones/archiveit_utilities.git
RUN cd archiveit_utilities && pip install .
@@ -16,4 +20,5 @@ ENV FLASK_APP=mementoembed/mementoembed.py
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "5550"]
# CMD ["/bin/bash"]
# TODO: actually use waitress
# CMD ["waitress-serve", "--call", "mementoembed:create_app"]
View
@@ -4,7 +4,7 @@ import requests
import errno
import json
from mementoembed import Surrogate, identify_archive, identify_collection, get_archive_favicon
from mementoembed import MementoSurrogate
from archivenow import archivenow
if __name__ == '__main__':
@@ -53,18 +53,22 @@ if __name__ == '__main__':
logger.info("creating surrogate information")
s = Surrogate(urim, resp.text, resp.headers)
s = MementoSurrogate(urim)
output = {
"URI-M": urim,
"Memento-Datetime": s.memento_datetime,
"Text Snippet": s.text_snippet,
"Striking Image": s.striking_image,
"Title": s.title,
"Member of Archive": identify_archive(urim),
"Archive Collection": identify_collection(urim),
"Archive Favicon": get_archive_favicon(urim),
"Memento-Datetime": mdt
# "Site Favicon":
"Member of Archive": s.archive_name,
"Archive URI": s.archive_uri,
"Archive Collection": s.collection_name,
"Archive Collection URI": s.archive_collection_uri,
"Archive Favicon": s.archive_favicon,
"Original Favicon": s.original_favicon,
"Original Link Status": s.original_link_status,
"Generated Datetime": s.surrogate_creation_time
}
print(json.dumps(output))
View
@@ -1,13 +1,125 @@
import os
import json
import htmlmin
import dicttoxml
import requests
from flask import Flask, request, render_template, make_response
from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache
from .mementosurrogate import MementoSurrogate, NotMementoException, \
MementoConnectionTimeout, MementoImageConnectionError, \
MementoImageConnectionTimeout
from .archiveinfo import identify_archive, identify_collection, \
get_archive_favicon, archive_names, get_collection_uri, \
get_archive_uri
__all__ = [
"MementoSurrogate", "identify_archive",
"identify_collection", "get_archive_favicon",
"archive_names", "get_collection_uri",
"get_archive_uri"
]
"MementoSurrogate", "NotMementoException", "MementoConnectionTimeout",
"MementoImageConnectionError", "MementoImageConnectionTimeout"
]
user_agent_string = "ODU WS-DL Researcher Shawn M. Jones <sjone@cs.odu.edu>"
def create_app():
app = Flask(__name__, instance_relative_config=True)
# pylint: disable=no-member
app.logger.info("loading Flask app for {}".format(app.name))
#pylint: disable=unused-variable
@app.route('/', methods=['GET', 'HEAD'])
def front_page():
return render_template('index.html')
@app.route('/services/oembed', methods=['GET', 'HEAD'])
def oembed_endpoint():
urim = request.args.get("url")
responseformat = request.args.get("format")
# JSON is the default
if responseformat == None:
responseformat = "json"
if responseformat != "json":
if responseformat != "xml":
return "The provider cannot return a response in the requested format.", 501
app.logger.debug("received url {}".format(urim))
app.logger.debug("format: {}".format(responseformat))
if os.environ.get('FLASK_ENV'):
if 'development' in os.environ.get('FLASK_ENV').lower():
session = CacheControl(requests.session(), cache=FileCache('.web_cache', forever=True))
else:
session = CacheControl(requests.session(), cache=FileCache('.web_cache'))
else:
session = CacheControl(requests.session(), cache=FileCache('.web_cache'))
try:
s = MementoSurrogate(
urim=urim,
session=session,
logger=app.logger
)
output = {}
output["type"] = "rich"
output["version"] = "1.0"
output["url"] = urim
output["provider_name"] = s.archive_name
output["provider_uri"] = s.archive_uri
urlroot = request.url_root
urlroot = urlroot if urlroot[-1] != '/' else urlroot[0:-1]
app.logger.info("generating oEmbed output for {}".format(urim))
output["html"] = htmlmin.minify( render_template(
"social_card.html",
urim = urim,
urir = s.original_uri,
image = s.striking_image,
archive_uri = s.archive_uri,
archive_favicon = s.archive_favicon,
archive_collection_id = s.collection_id,
archive_collection_uri = s.collection_uri,
archive_collection_name = s.collection_name,
archive_name = s.archive_name,
original_favicon = s.original_favicon,
original_domain = s.original_domain,
original_link_status = s.original_link_status,
surrogate_creation_time = s.creation_time,
memento_datetime = s.memento_datetime,
me_title = s.title,
me_snippet = s.text_snippet,
server_domain = urlroot
), remove_empty_space=True,
remove_optional_attribute_quotes=False )
output["width"] = 500
#TODO: fix this to the correct height!
output["height"] = 200
if responseformat == "json":
response = make_response(json.dumps(output, indent=4))
response.headers['Content-Type'] = 'application/json'
else:
response = make_response( dicttoxml.dicttoxml(output, custom_root='oembed') )
response.headers['Content-Type'] = 'text/xml'
app.logger.info("returning output as application/json...")
except NotMementoException:
return render_template(
'make_your_own_memento.html',
urim = urim
), 400
return response
return app
View

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 443e263

Please sign in to comment.