diff --git a/confarchive/view.py b/confarchive/view.py old mode 100644 new mode 100755 index ea8cdc4..82b1262 --- a/confarchive/view.py +++ b/confarchive/view.py @@ -1,4 +1,4 @@ -"""Flask views.""" +#!/usr/bin/python3 import os @@ -17,7 +17,6 @@ database.init_app(app) @app.route("/person/", methods=["GET", "POST"]) def person(person_id: int) -> str | Response: - """Person page.""" item = model.Person.query.get(person_id) if flask.request.method == "POST" and check_admin_mode(): 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) 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"]) def login() -> str | Response: diff --git a/confarchive/wikidata.py b/confarchive/wikidata.py index 82342c5..19fd78f 100644 --- a/confarchive/wikidata.py +++ b/confarchive/wikidata.py @@ -8,14 +8,12 @@ import time import typing import requests -import flask commons_url = "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)" CallParams = dict[str, str | int] -SearchHit = dict[str, typing.Any] s = requests.Session() s.headers.update({"User-Agent": user_agent}) @@ -26,39 +24,29 @@ def md5sum(s: str) -> str: return hashlib.md5(s.encode("utf-8")).hexdigest() -def get_cache_filename(q: str) -> str: - """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]: +def search(q: str) -> list[dict[str, typing.Any]]: """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): data = json.load(open(cache_filename)) - return typing.cast(list[SearchHit], data["query"]["search"]) + else: + params: dict[str, str | int] = { + "action": "query", + "list": "search", + "format": "json", + "formatversion": 2, + "srsearch": q, + "srlimit": "10", + } + r = requests.get(wikidata_api, params=params) + open(cache_filename, "w").write(r.text) + data = r.json() + time.sleep(1) - params: dict[str, str | int] = { - "action": "query", - "list": "search", - "format": "json", - "formatversion": 2, - "srsearch": q, - "srlimit": "10", - } - r = requests.get(wikidata_api, params=params) - open(cache_filename, "w").write(r.text) - data = r.json() - - 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: @@ -77,9 +65,9 @@ def api_image_detail_call(filename: str) -> requests.Response: def get_item(qid: str) -> typing.Any: """Get an item from Wikidata.""" - item_filename = get_item_filename(qid) - if os.path.exists(item_filename): - item = json.load(open(item_filename)) + cache_filename = os.path.join("items", qid + ".json") + if os.path.exists(cache_filename): + item = json.load(open(cache_filename)) else: params: dict[str, str | int] = { "action": "wbgetentities", @@ -89,8 +77,9 @@ def get_item(qid: str) -> typing.Any: } r = s.get(wikidata_api, params=params) 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) + time.sleep(0.1) return item