Chunk requests to Wikimedia Commons
This commit is contained in:
parent
5941f28ebd
commit
039240b3c5
|
@ -1,6 +1,7 @@
|
||||||
from . import mediawiki, utils
|
from . import mediawiki, utils
|
||||||
|
|
||||||
commons_url = 'https://www.wikidata.org/w/api.php'
|
commons_url = 'https://www.wikidata.org/w/api.php'
|
||||||
|
page_size = 50
|
||||||
|
|
||||||
def image_detail(filenames, thumbheight=None, thumbwidth=None):
|
def image_detail(filenames, thumbheight=None, thumbwidth=None):
|
||||||
if not isinstance(filenames, list):
|
if not isinstance(filenames, list):
|
||||||
|
@ -10,7 +11,6 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'action': 'query',
|
'action': 'query',
|
||||||
'titles': '|'.join(f'File:{f}' for f in filenames),
|
|
||||||
'prop': 'imageinfo',
|
'prop': 'imageinfo',
|
||||||
'iiprop': 'url',
|
'iiprop': 'url',
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,18 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
|
||||||
params['iiurlheight'] = thumbheight
|
params['iiurlheight'] = thumbheight
|
||||||
if thumbwidth is not None:
|
if thumbwidth is not None:
|
||||||
params['iiurlwidth'] = thumbwidth
|
params['iiurlwidth'] = thumbwidth
|
||||||
r = mediawiki.api_call(params, api_url=commons_url)
|
|
||||||
|
|
||||||
images = {}
|
images = {}
|
||||||
|
|
||||||
for image in r.json()['query']['pages']:
|
for cur in utils.chunk(filenames, page_size):
|
||||||
filename = utils.drop_start(image['title'], 'File:')
|
call_params = params.copy()
|
||||||
images[filename] = image['imageinfo'][0]
|
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
|
return images
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue