aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat_web/ref_routes.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2021-11-02 18:14:43 -0700
committerBryan Newbold <bnewbold@robocracy.org>2021-11-02 18:14:43 -0700
commit9dc891b8098542bb089c8c47098b60a8beb76a53 (patch)
tree2c9b1c569b6b9bb4041ef51076d024b6980089c5 /python/fatcat_web/ref_routes.py
parent6464631dbe5c4afeb76f2f3c9d63b89f917c9a3b (diff)
downloadfatcat-9dc891b8098542bb089c8c47098b60a8beb76a53.tar.gz
fatcat-9dc891b8098542bb089c8c47098b60a8beb76a53.zip
fmt (black): fatcat_web/
Diffstat (limited to 'python/fatcat_web/ref_routes.py')
-rw-r--r--python/fatcat_web/ref_routes.py175
1 files changed, 130 insertions, 45 deletions
diff --git a/python/fatcat_web/ref_routes.py b/python/fatcat_web/ref_routes.py
index eed3f1df..6a5eb064 100644
--- a/python/fatcat_web/ref_routes.py
+++ b/python/fatcat_web/ref_routes.py
@@ -28,10 +28,12 @@ from fatcat_web.entity_helpers import generic_get_entity
from fatcat_web.forms import ReferenceMatchForm
-def _refs_web(direction, release_ident=None, work_ident=None, openlibrary_id=None, wikipedia_article=None) -> RefHits:
- offset = request.args.get('offset', '0')
+def _refs_web(
+ direction, release_ident=None, work_ident=None, openlibrary_id=None, wikipedia_article=None
+) -> RefHits:
+ offset = request.args.get("offset", "0")
offset = max(0, int(offset)) if offset.isnumeric() else 0
- limit = request.args.get('limit', '30')
+ limit = request.args.get("limit", "30")
limit = min(max(0, int(limit)), 100) if limit.isnumeric() else 30
if direction == "in":
hits = get_inbound_refs(
@@ -66,144 +68,227 @@ def _refs_web(direction, release_ident=None, work_ident=None, openlibrary_id=Non
return hits
-@app.route('/release/<string(length=26):ident>/refs-in', methods=['GET'])
+@app.route("/release/<string(length=26):ident>/refs-in", methods=["GET"])
def release_view_refs_inbound(ident):
if request.accept_mimetypes.best == "application/json":
return release_view_refs_inbound_json(ident)
release = generic_get_entity("release", ident)
hits = _refs_web("in", release_ident=ident)
- return render_template('release_view_fuzzy_refs.html', direction="in", entity=release, hits=hits), 200
+ return (
+ render_template(
+ "release_view_fuzzy_refs.html", direction="in", entity=release, hits=hits
+ ),
+ 200,
+ )
-@app.route('/release/<string(length=26):ident>/refs-out', methods=['GET'])
+@app.route("/release/<string(length=26):ident>/refs-out", methods=["GET"])
def release_view_refs_outbound(ident):
if request.accept_mimetypes.best == "application/json":
return release_view_refs_outbound_json(ident)
release = generic_get_entity("release", ident)
hits = _refs_web("out", release_ident=ident)
- return render_template('release_view_fuzzy_refs.html', direction="out", entity=release, hits=hits), 200
+ return (
+ render_template(
+ "release_view_fuzzy_refs.html", direction="out", entity=release, hits=hits
+ ),
+ 200,
+ )
-@app.route('/openlibrary/OL<int:id_num>W/refs-in', methods=['GET'])
+
+@app.route("/openlibrary/OL<int:id_num>W/refs-in", methods=["GET"])
def openlibrary_view_refs_inbound(id_num):
if request.accept_mimetypes.best == "application/json":
return openlibrary_view_refs_inbound_json(id_num)
openlibrary_id = f"OL{id_num}W"
hits = _refs_web("in", openlibrary_id=openlibrary_id)
- return render_template('openlibrary_view_fuzzy_refs.html', openlibrary_id=openlibrary_id, direction="in", hits=hits), 200
-
-@app.route('/wikipedia/<string(length=2):wiki_lang>:<string:wiki_article>/refs-out', methods=['GET'])
+ return (
+ render_template(
+ "openlibrary_view_fuzzy_refs.html",
+ openlibrary_id=openlibrary_id,
+ direction="in",
+ hits=hits,
+ ),
+ 200,
+ )
+
+
+@app.route(
+ "/wikipedia/<string(length=2):wiki_lang>:<string:wiki_article>/refs-out", methods=["GET"]
+)
def wikipedia_view_refs_outbound(wiki_lang: str, wiki_article: str):
if request.accept_mimetypes.best == "application/json":
return wikipedia_view_refs_outbound_json(wiki_lang, wiki_article)
wiki_url = f"https://{wiki_lang}.wikipedia.org/wiki/{wiki_article}"
- wiki_article = wiki_article.replace('_', ' ')
+ wiki_article = wiki_article.replace("_", " ")
wikipedia_article = wiki_lang + ":" + wiki_article
hits = _refs_web("out", wikipedia_article=wikipedia_article)
- return render_template('wikipedia_view_fuzzy_refs.html', wiki_article=wiki_article, wiki_lang=wiki_lang, wiki_url=wiki_url, direction="out", hits=hits), 200
-
-@app.route('/reference/match', methods=['GET', 'POST'])
+ return (
+ render_template(
+ "wikipedia_view_fuzzy_refs.html",
+ wiki_article=wiki_article,
+ wiki_lang=wiki_lang,
+ wiki_url=wiki_url,
+ direction="out",
+ hits=hits,
+ ),
+ 200,
+ )
+
+
+@app.route("/reference/match", methods=["GET", "POST"])
def reference_match():
grobid_status = None
grobid_dict = None
form = ReferenceMatchForm()
- if not form.is_submitted() and request.args.get('submit_type'):
+ if not form.is_submitted() and request.args.get("submit_type"):
form = ReferenceMatchForm(request.args)
- if form.is_submitted() or request.args.get('title'):
+ if form.is_submitted() or request.args.get("title"):
if form.validate():
- if form.submit_type.data == 'parse':
+ if form.submit_type.data == "parse":
resp_xml = grobid_api_process_citation(form.raw_citation.data)
if not resp_xml:
grobid_status = "failed"
- return render_template('reference_match.html', form=form, grobid_status=grobid_status), 400
+ return (
+ render_template(
+ "reference_match.html", form=form, grobid_status=grobid_status
+ ),
+ 400,
+ )
grobid_dict = transform_grobid_ref_xml(resp_xml)
if not grobid_dict:
grobid_status = "empty"
- return render_template('reference_match.html', form=form, grobid_status=grobid_status), 200
- #print(grobid_dict)
+ return (
+ render_template(
+ "reference_match.html", form=form, grobid_status=grobid_status
+ ),
+ 200,
+ )
+ # print(grobid_dict)
release_stub = grobid_ref_to_release(grobid_dict)
# remove empty values from GROBID parsed dict
grobid_dict = {k: v for k, v in grobid_dict.items() if v is not None}
form = ReferenceMatchForm.from_grobid_parse(grobid_dict, form.raw_citation.data)
grobid_status = "success"
- matches = close_fuzzy_release_matches(es_client=app.es_client, release=release_stub, match_limit=10) or []
- elif form.submit_type.data == 'match':
- matches = close_fuzzy_biblio_matches(es_client=app.es_client, biblio=form.data, match_limit=10) or []
+ matches = (
+ close_fuzzy_release_matches(
+ es_client=app.es_client, release=release_stub, match_limit=10
+ )
+ or []
+ )
+ elif form.submit_type.data == "match":
+ matches = (
+ close_fuzzy_biblio_matches(
+ es_client=app.es_client, biblio=form.data, match_limit=10
+ )
+ or []
+ )
else:
raise NotImplementedError()
for m in matches:
# expand releases more completely
- m.release = api.get_release(m.release.ident, expand="container,files,filesets,webcaptures", hide="abstract,refs")
+ m.release = api.get_release(
+ m.release.ident,
+ expand="container,files,filesets,webcaptures",
+ hide="abstract,refs",
+ )
# hack in access options
m.access_options = release_access_options(m.release)
- return render_template('reference_match.html', form=form, grobid_dict=grobid_dict, grobid_status=grobid_status, matches=matches), 200
+ return (
+ render_template(
+ "reference_match.html",
+ form=form,
+ grobid_dict=grobid_dict,
+ grobid_status=grobid_status,
+ matches=matches,
+ ),
+ 200,
+ )
elif form.errors:
- return render_template('reference_match.html', form=form), 400
+ return render_template("reference_match.html", form=form), 400
- return render_template('reference_match.html', form=form), 200
+ return render_template("reference_match.html", form=form), 200
### Pseudo-APIs #############################################################
-@app.route('/release/<string(length=26):ident>/refs-out.json', methods=['GET', 'OPTIONS'])
-@crossdomain(origin='*',headers=['access-control-allow-origin','Content-Type'])
+
+@app.route("/release/<string(length=26):ident>/refs-out.json", methods=["GET", "OPTIONS"])
+@crossdomain(origin="*", headers=["access-control-allow-origin", "Content-Type"])
def release_view_refs_outbound_json(ident):
hits = _refs_web("out", release_ident=ident)
return Response(hits.json(exclude_unset=True), mimetype="application/json")
-@app.route('/release/<string(length=26):ident>/refs-in.json', methods=['GET', 'OPTIONS'])
-@crossdomain(origin='*',headers=['access-control-allow-origin','Content-Type'])
+@app.route("/release/<string(length=26):ident>/refs-in.json", methods=["GET", "OPTIONS"])
+@crossdomain(origin="*", headers=["access-control-allow-origin", "Content-Type"])
def release_view_refs_inbound_json(ident):
hits = _refs_web("in", release_ident=ident)
return Response(hits.json(exclude_unset=True), mimetype="application/json")
-@app.route('/openlibrary/OL<int:id_num>W/refs-in.json', methods=['GET', 'OPTIONS'])
-@crossdomain(origin='*',headers=['access-control-allow-origin','Content-Type'])
+
+@app.route("/openlibrary/OL<int:id_num>W/refs-in.json", methods=["GET", "OPTIONS"])
+@crossdomain(origin="*", headers=["access-control-allow-origin", "Content-Type"])
def openlibrary_view_refs_inbound_json(id_num):
openlibrary_id = f"OL{id_num}W"
hits = _refs_web("in", openlibrary_id=openlibrary_id)
return Response(hits.json(exclude_unset=True), mimetype="application/json")
-@app.route('/wikipedia/<string(length=2):wiki_lang>:<string:wiki_article>/refs-out.json', methods=['GET', 'OPTIONS'])
-@crossdomain(origin='*',headers=['access-control-allow-origin','Content-Type'])
+
+@app.route(
+ "/wikipedia/<string(length=2):wiki_lang>:<string:wiki_article>/refs-out.json",
+ methods=["GET", "OPTIONS"],
+)
+@crossdomain(origin="*", headers=["access-control-allow-origin", "Content-Type"])
def wikipedia_view_refs_outbound_json(wiki_lang: str, wiki_article: str):
- wiki_article = wiki_article.replace('_', ' ')
+ wiki_article = wiki_article.replace("_", " ")
wikipedia_article = wiki_lang + ":" + wiki_article
hits = _refs_web("out", wikipedia_article=wikipedia_article)
return Response(hits.json(exclude_unset=True), mimetype="application/json")
-@app.route('/reference/match.json', methods=['GET', 'OPTIONS'])
-@crossdomain(origin='*',headers=['access-control-allow-origin','Content-Type'])
+@app.route("/reference/match.json", methods=["GET", "OPTIONS"])
+@crossdomain(origin="*", headers=["access-control-allow-origin", "Content-Type"])
def reference_match_json():
form = ReferenceMatchForm(request.args)
if form.validate():
- if form.submit_type.data == 'match':
- matches = close_fuzzy_biblio_matches(es_client=app.es_client, biblio=form.data, match_limit=10) or []
+ if form.submit_type.data == "match":
+ matches = (
+ close_fuzzy_biblio_matches(
+ es_client=app.es_client, biblio=form.data, match_limit=10
+ )
+ or []
+ )
else:
raise NotImplementedError()
resp = []
for m in matches:
# expand releases more completely
- m.release = api.get_release(m.release.ident, expand="container,files,filesets,webcaptures", hide="abstract,refs")
+ m.release = api.get_release(
+ m.release.ident,
+ expand="container,files,filesets,webcaptures",
+ hide="abstract,refs",
+ )
# hack in access options
m.access_options = release_access_options(m.release)
# and manually convert to dict (for jsonify)
info = m.__dict__
- info['release'] = entity_to_dict(m.release)
- info['access_options'] = [o.dict() for o in m.access_options]
+ info["release"] = entity_to_dict(m.release)
+ info["access_options"] = [o.dict() for o in m.access_options]
resp.append(info)
return jsonify(resp), 200
else:
- return Response(json.dumps(dict(errors=form.errors)), mimetype="application/json", status=400)
+ return Response(
+ json.dumps(dict(errors=form.errors)), mimetype="application/json", status=400
+ )