-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (66 loc) · 2.54 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
pseudo code
let user type in an orcid, accept orcid-parameter
lookup and parse all DOIs from the orcid page
lookup each doi in Wikidata using QLever (its crazy fast compared to current Wikimedia services)
present the dois to the user and clickable links to scholia for those not found in wikidata
"""
import logging
from flask import Flask, jsonify, render_template, request
from flask.typing import ResponseReturnValue
from markupsafe import escape
import config
from models.item import Item
from models.orcid import Orcid
app = Flask(__name__)
if config.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
invalid_format = "Not a valid QID, format must be 'Q[0-9]+'"
user_agent = "Wikidata Orcid Scraper (https://github.com/dpriskorn/WikidataOrcidScraper; User:So9q) python-requests/2.31.0"
documentation_url = (
"https://www.wikidata.org/wiki/Wikidata:Tools/Wikidata_Orcid_Scraper"
)
@app.route("/", methods=["GET"])
def index() -> ResponseReturnValue:
orcid = escape(request.args.get("orcid", ""))
qid = escape(request.args.get("qid", ""))
# if not str(qid) and not str(orcid):
# return jsonify("Error: Got no QID or ORCID")
return render_template("index.html", qid=qid, orcid=orcid)
@app.route("/results", methods=["GET"])
def results() -> ResponseReturnValue:
raw_orcid = escape(request.args.get("orcid", ""))
qid = escape(request.args.get("qid", ""))
# label = escape(request.args.get("label", ""))
# description = escape(request.args.get("description", ""))
size = escape(request.args.get("size", 10))
offset = escape(request.args.get("offset", 0))
# First get using orcid, fallback to looking up the orcid via the QID.
if raw_orcid:
orcid = Orcid(string=raw_orcid, size=int(size), offset=int(offset))
rows = orcid.get_works_html
elif qid and not raw_orcid:
item = Item(qid=qid)
item_orcid = item.orcid
if item_orcid:
orcid = Orcid(string=item_orcid, size=int(size), offset=int(offset))
rows = orcid.get_works_html
else:
return render_template(
"error.html", qid=qid, qid_url=item.url, query=item.orcid_query
)
else:
return jsonify("Error: We need either a QID or ORCID")
return render_template(
"results.html",
qid=qid,
orcid=raw_orcid,
# label=label,
# description=description,
rows=rows,
offset=int(offset) + int(size),
size=size,
)