example calling mediawiki api via mwclient to make thumbnails and then to get semantic data

master
Michael Murtaugh 5 years ago
parent b11cd65a54
commit a1f725177b

@ -0,0 +1,63 @@
# https://www.mediawiki.org/wiki/API:Main_page
# https://mwclient.readthedocs.io/en/latest/
import mwclient
from mwclient import Site
from secrets import BOTPASSWORD
import json
site = Site("hub.xpub.nl", path="/sandbox/itchwiki/")
site.login("Bot", BOTPASSWORD)
for i in site.allimages():
# Use imageinfo to request/create a thumbnail
# NB: uses i.name as "titles" (the api call can take a list of titles)
# but here it just uses one.
# NB: iiurlwidth specifies a max width for the resulting thumbnail
# check out the API doc for all the options
# https://www.mediawiki.org/wiki/API:Imageinfo
r = site.api("query", prop="imageinfo", titles=i.name, iiprop="url", iiurlwidth="80", formatversion=2)
iinfo = r['query']['pages'][0]['imageinfo'][0]
thumburl = iinfo['thumburl']
fullsizeurl = iinfo['url']
filepageurl = iinfo['descriptionurl']
print ("""
<a href="{0}"><img src="{1}"></a>
""".format(filepageurl, thumburl))
# print (i.name, thumburl, fullsizeurl, filepageurl)
# NOW do an "ASK" api call to get semantic meta data from the same image
# This query is the "code" that you get from the "Semantic Ask" page
# Except you need to remove the {{#ask: and the }} at the end.
ask_query = """[[{0}]]
|?Title
|?Date
|?Creator
|?Format
|?Organization
|?Part
|?Partof
|?Event
|?Topic
|?Language
|format=json
|limit=50
|offset=0
|link=all
|sort=
|order=asc
""".format(i.name)
# print ("Made an ASK query", ask_query)
ask_result = r = site.api("ask", query=ask_query)
# print (json.dumps(ask_result, indent=2))
image_metadata = ask_result['query']['results'][i.name]["printouts"]
print (json.dumps(image_metadata, indent=2))
# Calling the api to make/give a thumbnail of a particular image
# https://hub.xpub.nl/sandbox/itchwiki/api.php?action=query&format=json&prop=imageinfo&iiprop=url&iiurlwidth=320&titles=File:VF250Commitment-18.jpg
# site.api()
# Magic URL to generate and get the URL of a thumbnail of an image
# https://hub.xpub.nl/sandbox/itchwiki/api.php?action=query&format=json&prop=imageinfo&iiprop=url&iiurlwidth=640&titles=File:VF250Commitment-18.jpg
Loading…
Cancel
Save