Docstrings and types.

This commit is contained in:
Edward Betts 2023-05-14 11:07:14 +02:00
parent fd35658e51
commit 96002254ad

View file

@ -1,10 +1,11 @@
"""Use mediawiki API to look up images on Wikimedia Commons.""" """Use mediawiki API to look up images on Wikimedia Commons."""
import urllib.parse import urllib.parse
from typing import Any
import requests import requests
from . import utils from . import CallParams, utils
commons_start = "http://commons.wikimedia.org/wiki/Special:FilePath/" commons_start = "http://commons.wikimedia.org/wiki/Special:FilePath/"
commons_url = "https://www.wikidata.org/w/api.php" commons_url = "https://www.wikidata.org/w/api.php"
@ -16,9 +17,9 @@ def commons_uri_to_filename(uri: str) -> str:
return urllib.parse.unquote(utils.drop_start(uri, commons_start)) return urllib.parse.unquote(utils.drop_start(uri, commons_start))
def api_call(params: dict[str, str | int]) -> requests.models.Response: def api_call(params: CallParams) -> requests.Response:
"""Make an API call.""" """Call the Commons API."""
call_params = { call_params: CallParams = {
"format": "json", "format": "json",
"formatversion": 2, "formatversion": 2,
**params, **params,
@ -27,9 +28,11 @@ def api_call(params: dict[str, str | int]) -> requests.models.Response:
return requests.get(commons_url, params=call_params, timeout=5) return requests.get(commons_url, params=call_params, timeout=5)
def image_detail(filenames, thumbheight=None, thumbwidth=None): def image_detail(
filenames: list[str], thumbheight: int | None = None, thumbwidth: int | None = None
) -> dict[str, Any]:
"""Detail for multiple images.""" """Detail for multiple images."""
params = { params: CallParams = {
"action": "query", "action": "query",
"prop": "imageinfo", "prop": "imageinfo",
"iiprop": "url", "iiprop": "url",
@ -39,7 +42,7 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
if thumbwidth is not None: if thumbwidth is not None:
params["iiurlwidth"] = thumbwidth params["iiurlwidth"] = thumbwidth
images = {} images: dict[str, Any] = {}
for cur in utils.chunk(filenames, page_size): for cur in utils.chunk(filenames, page_size):
call_params = params.copy() call_params = params.copy()