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

自作多情 提交于 2019-12-17 16:23:26

问题


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?


回答1:


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" }}}]
}

where img_name.ext is the name of the image you are looking for (all whitespaces have to be replaced with _).

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 md5sum of the img_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




回答2:


Took me a while to figure this one out. The hash sum that is being talked about is not the value at P18[0].mainsnak.hash.

You need to hashsum("Junior-Jaguar-Belize-Zoo.jpg"). The string that is the name, (after replacing whitespaces with "_". That will give you the 213B31EC141DAFEBE457E49BCD7F9329 hash sum which you can use to determine the url.



来源:https://stackoverflow.com/questions/34393884/how-to-get-image-url-property-from-wikidata-item-by-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!