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,10 +18,15 @@ 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 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']:
|
for image in r.json()['query']['pages']:
|
||||||
filename = utils.drop_start(image['title'], 'File:')
|
filename = utils.drop_start(image['title'], 'File:')
|
||||||
images[filename] = image['imageinfo'][0]
|
images[filename] = image['imageinfo'][0]
|
||||||
|
|
Loading…
Reference in a new issue