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."""
import urllib.parse
from typing import Any
import requests
from . import utils
from . import CallParams, utils
commons_start = "http://commons.wikimedia.org/wiki/Special:FilePath/"
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))
def api_call(params: dict[str, str | int]) -> requests.models.Response:
"""Make an API call."""
call_params = {
def api_call(params: CallParams) -> requests.Response:
"""Call the Commons API."""
call_params: CallParams = {
"format": "json",
"formatversion": 2,
**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)
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."""
params = {
params: CallParams = {
"action": "query",
"prop": "imageinfo",
"iiprop": "url",
@ -39,7 +42,7 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
if thumbwidth is not None:
params["iiurlwidth"] = thumbwidth
images = {}
images: dict[str, Any] = {}
for cur in utils.chunk(filenames, page_size):
call_params = params.copy()