Tidy code and add docstrings.

This commit is contained in:
Edward Betts 2023-09-24 21:36:45 +01:00
parent 2fe7948981
commit b12a89c3b6

View file

@ -1,3 +1,5 @@
"""Access the Wikidata API."""
import hashlib
import json
import os
@ -9,9 +11,10 @@ import requests
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)"
s = requests.Session()
s.headers.update({"User-Agent": "conference-archive/0.1 (contact: edward@4angle.com)"})
s.headers.update({"User-Agent": user_agent})
def md5sum(s: str) -> str:
@ -20,6 +23,7 @@ def md5sum(s: str) -> str:
def search(q: str) -> list[dict[str, typing.Any]]:
"""Search Wikidata with caching."""
q_md5 = md5sum(q)
cache_filename = os.path.join("cache", q_md5 + ".json")
@ -58,6 +62,7 @@ def api_image_detail_call(filename: str) -> requests.Response:
def get_item(qid: str) -> typing.Any:
"""Get an item from Wikidata."""
cache_filename = os.path.join("items", qid + ".json")
if os.path.exists(cache_filename):
item = json.load(open(cache_filename))