Compare commits
No commits in common. "1001c51bc120a6dae54ec518a82440cc659748f0" and "295e8336214993c3126127cea420e847f714dc8b" have entirely different histories.
1001c51bc1
...
295e833621
11
confarchive/view.py
Normal file → Executable file
11
confarchive/view.py
Normal file → Executable file
|
@ -1,4 +1,4 @@
|
||||||
"""Flask views."""
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ database.init_app(app)
|
||||||
|
|
||||||
@app.route("/person/<int:person_id>", methods=["GET", "POST"])
|
@app.route("/person/<int:person_id>", methods=["GET", "POST"])
|
||||||
def person(person_id: int) -> str | Response:
|
def person(person_id: int) -> str | Response:
|
||||||
"""Person page."""
|
|
||||||
item = model.Person.query.get(person_id)
|
item = model.Person.query.get(person_id)
|
||||||
if flask.request.method == "POST" and check_admin_mode():
|
if flask.request.method == "POST" and check_admin_mode():
|
||||||
qid = flask.request.form["wikidata_qid"] or None
|
qid = flask.request.form["wikidata_qid"] or None
|
||||||
|
@ -389,6 +388,14 @@ def person_image_filename(person_id: int) -> str:
|
||||||
person = model.Person.query.get(person_id)
|
person = model.Person.query.get(person_id)
|
||||||
return os.path.join("wikidata_photo", "thumb", person.wikidata_photo[0])
|
return os.path.join("wikidata_photo", "thumb", person.wikidata_photo[0])
|
||||||
|
|
||||||
|
for filename in person.wikidata_photo:
|
||||||
|
face_crop = "face_1_" + filename
|
||||||
|
full = os.path.join("static", "wikidata_photo", "face_cropped", face_crop)
|
||||||
|
if os.path.exists(full):
|
||||||
|
return os.path.join("wikidata_photo", "face_cropped", face_crop)
|
||||||
|
|
||||||
|
return os.path.join("wikidata_photo", "thumb", person.wikidata_photo[0])
|
||||||
|
|
||||||
|
|
||||||
@app.route("/login", methods=["GET", "POST"])
|
@app.route("/login", methods=["GET", "POST"])
|
||||||
def login() -> str | Response:
|
def login() -> str | Response:
|
||||||
|
|
|
@ -8,14 +8,12 @@ import time
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import flask
|
|
||||||
|
|
||||||
commons_url = "https://www.wikidata.org/w/api.php"
|
commons_url = "https://www.wikidata.org/w/api.php"
|
||||||
wikidata_api = "https://www.wikidata.org/w/api.php"
|
wikidata_api = "https://www.wikidata.org/w/api.php"
|
||||||
user_agent = "conference-archive/0.1 (contact: edward@4angle.com)"
|
user_agent = "conference-archive/0.1 (contact: edward@4angle.com)"
|
||||||
|
|
||||||
CallParams = dict[str, str | int]
|
CallParams = dict[str, str | int]
|
||||||
SearchHit = dict[str, typing.Any]
|
|
||||||
|
|
||||||
s = requests.Session()
|
s = requests.Session()
|
||||||
s.headers.update({"User-Agent": user_agent})
|
s.headers.update({"User-Agent": user_agent})
|
||||||
|
@ -26,26 +24,15 @@ def md5sum(s: str) -> str:
|
||||||
return hashlib.md5(s.encode("utf-8")).hexdigest()
|
return hashlib.md5(s.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def get_cache_filename(q: str) -> str:
|
def search(q: str) -> list[dict[str, typing.Any]]:
|
||||||
"""Cache filename for query."""
|
|
||||||
q_md5 = md5sum(q)
|
|
||||||
data_dir = flask.current_app.config["DATA_DIR"]
|
|
||||||
return os.path.join(data_dir, "cache", q_md5 + ".json")
|
|
||||||
|
|
||||||
|
|
||||||
def get_item_filename(qid: str) -> str:
|
|
||||||
data_dir = flask.current_app.config["DATA_DIR"]
|
|
||||||
return os.path.join(data_dir, "item", qid + ".json")
|
|
||||||
|
|
||||||
|
|
||||||
def search(q: str) -> list[SearchHit]:
|
|
||||||
"""Search Wikidata with caching."""
|
"""Search Wikidata with caching."""
|
||||||
cache_filename = get_cache_filename(q)
|
q_md5 = md5sum(q)
|
||||||
|
|
||||||
|
cache_filename = os.path.join("cache", q_md5 + ".json")
|
||||||
|
|
||||||
if os.path.exists(cache_filename):
|
if os.path.exists(cache_filename):
|
||||||
data = json.load(open(cache_filename))
|
data = json.load(open(cache_filename))
|
||||||
return typing.cast(list[SearchHit], data["query"]["search"])
|
else:
|
||||||
|
|
||||||
params: dict[str, str | int] = {
|
params: dict[str, str | int] = {
|
||||||
"action": "query",
|
"action": "query",
|
||||||
"list": "search",
|
"list": "search",
|
||||||
|
@ -57,8 +44,9 @@ def search(q: str) -> list[SearchHit]:
|
||||||
r = requests.get(wikidata_api, params=params)
|
r = requests.get(wikidata_api, params=params)
|
||||||
open(cache_filename, "w").write(r.text)
|
open(cache_filename, "w").write(r.text)
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
return typing.cast(list[SearchHit], data["query"]["search"])
|
return typing.cast(list[dict[str, typing.Any]], data["query"]["search"])
|
||||||
|
|
||||||
|
|
||||||
def api_image_detail_call(filename: str) -> requests.Response:
|
def api_image_detail_call(filename: str) -> requests.Response:
|
||||||
|
@ -77,9 +65,9 @@ def api_image_detail_call(filename: str) -> requests.Response:
|
||||||
|
|
||||||
def get_item(qid: str) -> typing.Any:
|
def get_item(qid: str) -> typing.Any:
|
||||||
"""Get an item from Wikidata."""
|
"""Get an item from Wikidata."""
|
||||||
item_filename = get_item_filename(qid)
|
cache_filename = os.path.join("items", qid + ".json")
|
||||||
if os.path.exists(item_filename):
|
if os.path.exists(cache_filename):
|
||||||
item = json.load(open(item_filename))
|
item = json.load(open(cache_filename))
|
||||||
else:
|
else:
|
||||||
params: dict[str, str | int] = {
|
params: dict[str, str | int] = {
|
||||||
"action": "wbgetentities",
|
"action": "wbgetentities",
|
||||||
|
@ -89,8 +77,9 @@ def get_item(qid: str) -> typing.Any:
|
||||||
}
|
}
|
||||||
r = s.get(wikidata_api, params=params)
|
r = s.get(wikidata_api, params=params)
|
||||||
item = r.json()["entities"][qid]
|
item = r.json()["entities"][qid]
|
||||||
with open(item_filename, "w") as f:
|
with open(cache_filename, "w") as f:
|
||||||
json.dump(item, f, indent=2)
|
json.dump(item, f, indent=2)
|
||||||
|
time.sleep(0.1)
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue