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

3

14

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?

Arv answered 21/12, 2015 at 10:48 Comment(0)
C
7

You can build URLs from the image property (Sample.png in the following example):

https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/Sample.png&width=300

See Wikimedia Commons' "Reusing content outside Wikimedia" documentation for more details.

Cyanide answered 22/12, 2020 at 16:31 Comment(2)
it works, but not with &width parameter, in that case for different images you will may get a 404.Radioactive
This solution, despite of the 302 redirect (to https://upload.wikimedia.org/wikipedia/commons/a/a7/Sample.png in the example above) it's more safe than others approach with md5sum (to detect /a/a7, that can be subject to changes.Radioactive
K
53

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

https://www.wikidata.org/w/api.php?action=wbgetclaims&property=P18&entity=Qxxx

The response will include:

"claims": {
        "P18": [{ "mainsnak": { "datavalue": { "value": "img_name.ext" }, "hash": ... }}]
}

where img_name.ext is the name of the image you are looking for.

The final image URL will be: https://upload.wikimedia.org/wikipedia/commons/a/ab/img_name.ext, where a and b are the first and the second chars of MD5 hashsum of the img_name.ext (with all whitespaces replaced by _).

Example: For item jaguar (Q35694) the API will returns image name "Junior-Jaguar-Belize-Zoo.jpg", which has MD5 hashsum("Junior-Jaguar-Belize-Zoo.jpg") = 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

Note: The MD5 hashsum is for the name of the image file, not the P18[0].mainsnak.hash property included in the JSON body.

Knesset answered 21/12, 2015 at 19:22 Comment(1)
nice! You can also directly get the resized image url like so: commons.wikimedia.org/w/… Nevertheless, beware that requesting a size bigger than the original image size will return an errorResupinate
C
7

You can build URLs from the image property (Sample.png in the following example):

https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/Sample.png&width=300

See Wikimedia Commons' "Reusing content outside Wikimedia" documentation for more details.

Cyanide answered 22/12, 2020 at 16:31 Comment(2)
it works, but not with &width parameter, in that case for different images you will may get a 404.Radioactive
This solution, despite of the 302 redirect (to https://upload.wikimedia.org/wikipedia/commons/a/a7/Sample.png in the example above) it's more safe than others approach with md5sum (to detect /a/a7, that can be subject to changes.Radioactive
N
0

I use this PHP function to convert a given image filename into an url for wikipedia commons:

function getWikimediaImageURL($aFilename) {
     #-- Example:  $aURLString = getWikimediaImageURL('Fortuna Düsseldorf.svg');    
     $aFilename = str_replace( ' ', '_', $aFilename);
     $md5 = md5($aFilename); 

     return "https://upload.wikimedia.org/wikipedia/commons/" .
            substr($md5,0,1) . 
            "/" .
            substr($md5,0,2). 
            "/" . 
            urlencode($aFilename);
 }    

Commons is a little bit picky of user agent header. So provide one when downloading (change the User-Agent string according to https://meta.wikimedia.org/wiki/User-Agent_policy):

$opts = [
    "https" => [
        "method" => "GET",
        "header" => "User-Agent: oolBot/0.0 (https://example.org/coolbot/; [email protected]) generic-library/0.0\r\n" 
    ]
];            
$context = stream_context_create($opts);            
$content = file_get_contents( $pfad , false, $context ); 
Nagual answered 26/12, 2022 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.