From b12a89c3b62327bab75069e507a1cbc9de79494f Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 24 Sep 2023 21:36:45 +0100 Subject: [PATCH] Tidy code and add docstrings. --- confarchive/wikidata.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/confarchive/wikidata.py b/confarchive/wikidata.py index cb73528..0df3a6e 100644 --- a/confarchive/wikidata.py +++ b/confarchive/wikidata.py @@ -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))