Chunk requests to Wikimedia Commons

This commit is contained in:
Edward Betts 2019-10-14 11:08:42 +01:00
parent 5941f28ebd
commit 039240b3c5

View file

@ -1,6 +1,7 @@
from . import mediawiki, utils
commons_url = 'https://www.wikidata.org/w/api.php'
page_size = 50
def image_detail(filenames, thumbheight=None, thumbwidth=None):
if not isinstance(filenames, list):
@ -10,7 +11,6 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
params = {
'action': 'query',
'titles': '|'.join(f'File:{f}' for f in filenames),
'prop': 'imageinfo',
'iiprop': 'url',
}
@ -18,13 +18,18 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
params['iiurlheight'] = thumbheight
if thumbwidth is not None:
params['iiurlwidth'] = thumbwidth
r = mediawiki.api_call(params, api_url=commons_url)
images = {}
for image in r.json()['query']['pages']:
filename = utils.drop_start(image['title'], 'File:')
images[filename] = image['imageinfo'][0]
for cur in utils.chunk(filenames, page_size):
call_params = params.copy()
call_params['titles'] = '|'.join(f'File:{f}' for f in cur)
r = mediawiki.api_call(call_params, api_url=commons_url)
for image in r.json()['query']['pages']:
filename = utils.drop_start(image['title'], 'File:')
images[filename] = image['imageinfo'][0]
return images