12

I'm trying to query all images of cats (as an example) from Wikimedia Commons.

The way I thought about going about this is first search for the Cat page which is here (I don't know how to find this out programmatically, maybe redirects?):

https://commons.wikimedia.org/wiki/Felis_silvestris_catus

Hence to get the images I would just need to do this:

https://commons.wikimedia.org/w/api.php?action=query&prop=images&imlimit=500&titles=Felis_silvestris_catus

This would return what I want. However the same search for Cat returns nothing even though Cat redirects to Felis_silvestris_catus.

Does anyone know if there is any simple way to do an image search on Wikimedia Commons just starting with "Cat" that would return Cat images and if there isn't what's the best that can be done?

I've been looking at the API for hours but I can't seem to figure it out.

Termininja
  • 6,620
  • 12
  • 48
  • 49
thewormsterror
  • 1,608
  • 14
  • 27

2 Answers2

13

Just add redirects=1 to your request.

https://commons.wikimedia.org/w/api.php?action=query&prop=images&imlimit=500&redirects=1&titles=Cat

Update: To get the image info for each one returned image use query with generator=images and prop=imageinfo:

https://commons.wikimedia.org/w/api.php?action=query&generator=images&prop=imageinfo&gimlimit=500&redirects=1&titles=Cat&iiprop=timestamp|user|userid|comment|canonicaltitle|url|size|dimensions|sha1|mime|thumbmime|mediatype|bitdepth

where iiprop includes some of imageinfo properties (check API documentation for all them)

Termininja
  • 6,620
  • 12
  • 48
  • 49
  • 1
    Is there anyways to get the image info for each image returned as well? Or do you have to do another request using imageinfo with the titles being the files? – thewormsterror May 03 '16 at 10:32
  • I noticed this fails when redirecting to a category, for example for potato. Do you have any idea how to get around this annoying problem? – thewormsterror May 11 '16 at 19:01
  • 1
    The problem is that Potato redirects to Category, and categories does not contain images. To get the members of category you need [`generator=categorymembers`](https://commons.wikimedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Potatoes&prop=info&gcmtype=file&redirects=1&prop=imageinfo&iiprop=timestamp|url|size|dimensions|mime|thumbmime|mediatype). So, maybe there is better variant but on this stage my idea is to make a request with `generator=images` and if the response is empty to create another request with `generator=categorymembers`. – Termininja May 12 '16 at 14:33
0

There is a jQuery API and GUi for WikiCommons here: https://github.com/genolve/wikimedia-api-gui If you don't need the GUI part it is easy to call the underlying functions directly. Try

getThumbsForPage() - Get the thumbnail (and full image URL) for ARRAY of PAGES
//then in the callback tap into 
infoA[ii].thumburl  
infoA[ii].url
genolve
  • 1
  • 2