forked from edward/owl-map
Show images in popups
This commit is contained in:
parent
c4aa27e8f4
commit
e3cefcfcbd
4 changed files with 230 additions and 1 deletions
47
matcher/commons.py
Normal file
47
matcher/commons.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import requests
|
||||
import urllib.parse
|
||||
from . import utils
|
||||
|
||||
commons_start = "http://commons.wikimedia.org/wiki/Special:FilePath/"
|
||||
commons_url = "https://www.wikidata.org/w/api.php"
|
||||
page_size = 50
|
||||
|
||||
|
||||
def commons_uri_to_filename(uri):
|
||||
return urllib.parse.unquote(utils.drop_start(uri, commons_start))
|
||||
|
||||
|
||||
def api_call(params):
|
||||
call_params = {
|
||||
"format": "json",
|
||||
"formatversion": 2,
|
||||
**params,
|
||||
}
|
||||
|
||||
return requests.get(commons_url, params=call_params, timeout=5)
|
||||
|
||||
|
||||
def image_detail(filenames, thumbheight=None, thumbwidth=None):
|
||||
params = {
|
||||
"action": "query",
|
||||
"prop": "imageinfo",
|
||||
"iiprop": "url",
|
||||
}
|
||||
if thumbheight is not None:
|
||||
params["iiurlheight"] = thumbheight
|
||||
if thumbwidth is not None:
|
||||
params["iiurlwidth"] = thumbwidth
|
||||
|
||||
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 = api_call(call_params)
|
||||
|
||||
for image in r.json()["query"]["pages"]:
|
||||
filename = utils.drop_start(image["title"], "File:")
|
||||
images[filename] = image["imageinfo"][0] if "imageinfo" in image else None
|
||||
|
||||
return images
|
||||
Loading…
Add table
Add a link
Reference in a new issue