From a1f725177b89b13547b5d1d0964cedb3b2f1b81d Mon Sep 17 00:00:00 2001 From: Michael Murtaugh Date: Thu, 5 Mar 2020 09:27:13 +0100 Subject: [PATCH] example calling mediawiki api via mwclient to make thumbnails and then to get semantic data --- example_api_calls.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 example_api_calls.py diff --git a/example_api_calls.py b/example_api_calls.py new file mode 100644 index 0000000..2671675 --- /dev/null +++ b/example_api_calls.py @@ -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 (""" + +""".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