Chunk requests to Wikimedia Commons
This commit is contained in:
parent
5941f28ebd
commit
039240b3c5
|
@ -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,10 +18,15 @@ 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 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]
|
||||
|
|
Loading…
Reference in a new issue