Tidy code and add docstrings.
This commit is contained in:
parent
2fe7948981
commit
b12a89c3b6
|
@ -1,3 +1,5 @@
|
||||||
|
"""Access the Wikidata API."""
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
@ -9,9 +11,10 @@ import requests
|
||||||
|
|
||||||
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)"
|
||||||
|
|
||||||
s = requests.Session()
|
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:
|
def md5sum(s: str) -> str:
|
||||||
|
@ -20,6 +23,7 @@ def md5sum(s: str) -> str:
|
||||||
|
|
||||||
|
|
||||||
def search(q: str) -> list[dict[str, typing.Any]]:
|
def search(q: str) -> list[dict[str, typing.Any]]:
|
||||||
|
"""Search Wikidata with caching."""
|
||||||
q_md5 = md5sum(q)
|
q_md5 = md5sum(q)
|
||||||
|
|
||||||
cache_filename = os.path.join("cache", q_md5 + ".json")
|
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:
|
def get_item(qid: str) -> typing.Any:
|
||||||
|
"""Get an item from Wikidata."""
|
||||||
cache_filename = os.path.join("items", qid + ".json")
|
cache_filename = os.path.join("items", qid + ".json")
|
||||||
if os.path.exists(cache_filename):
|
if os.path.exists(cache_filename):
|
||||||
item = json.load(open(cache_filename))
|
item = json.load(open(cache_filename))
|
||||||
|
|
Loading…
Reference in a new issue