I'm on stack exchange I'm on stack exchange - 1 year ago 148
Java Question

How to get image URL property from Wikidata item by API?

I've made an android app that uses the JSON Google image search API to provide images but I have noticed that Google have stopped supporting it. I have also discovered that Wikidata sometimes provides a image property on some items, however I can't seem to get the URL location of the image using the Wikidata API.

Is there any way to get the image URL property from items in Wikidata?

Answer Source

If some Wikidata item has image (P18) property, you can access it by MediaWiki API:


where Qxxx is the item id. The response will include:

    "claims": {
        "P18": [{
            "mainsnak": {
                "datavalue": {
                    "value": "image_name.ext",

where the image_name.ext is the image you are looking for (all spaces have to be replaced with _).

Now, we can get the URL by two ways:

First variant:

You can make another request by using Wikimedia Commons API: https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=url&titles=File:image_name.ext from where you will get the image URL directly.

Second variant:

The final image URL will be: https://upload.wikimedia.org/wikipedia/commons/a/ab/image_name.ext, where a and b are the first and the second chars of the md5sum of the image_name.ext.

For example, for item jaguar (Q35694) the API will returns image name "Junior-Jaguar-Belize-Zoo.jpg", which has md5sum: 213b31ec141dafebe457e49bcd7f9329, so a=2 and b=1, or the final image URL will be: https://upload.wikimedia.org/wikipedia/commons/2/21/Junior-Jaguar-Belize-Zoo.jpg

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download