Fetching Featured Images from Wikimedia Commons

Recently featured image on Commons: https://bit.ly/2IyitSG

Wikimedia Commons features many images regularly on its main page. As described by Commons,

Featured pictures are images from highly skilled photographers and illustrators that the Wikimedia Commons community has chosen as some of the highest quality on the site.

Getting an image featured is quite difficult as the statistics suggest.

There are currently 11,192 of such images in the Commons repository which is roughly 0.024% of the available images (46,591,843).

In the Wikimedia Commons Android app, we thought showing these featured images in the app would be a good addition as it would:

  • Show users what a great Commons picture is
  • Inspire/motivate users to take part in the same project
  • Let users hope that one day their picture will be shown at that place (in fact, show them that taking great pictures is worth the effort)

I started exploring the API that would let us fetch featured images and then we could consume it in the app. I came across the MediaWiki Generator API.

Check out the following API call that returns the first 10 features images from the API. https://bit.ly/2KRXg4l

Here’s a screenshot from the app showing the featured images from Commons:

Featured Images Screen

Let us discuss the parameters that the API takes:

  • action: query which is common param for all MediaWiki API calls
  • generator: we are going to generate a list of categorymembers
  • gcmtype: this param’s value is file as we want to extract information of the Commons image files
  • gcmtitle: this is name of the category under which all featured images on Commons are stored. Category:Featured_pictures_on_Wikimedia_Commons
  • prop: Setting the value of prop to imageinfo adds the imageinfo block in the API response. It contains the url and extmetadata fields that are required for getting some of the properties.

We wanted to display all the images in a category instead of showing just the first 10. So we used the continue query of MediaWiki API. To get more data, you use the query-continue value in the response.

The query-continue node will contain a subnode for each module used in the query that needs continuation, and these subnodes will contain properties to be used when making the followup "continuation" query. Note that clients should not be depending on the particular property names given for continuation of any module or the format of the values returned for continuation, as these may change.

Continue query: https://bit.ly/2Idkhgt

Moreover, the implementation in generic enough to load images from any category of Commons.

CategoryImagesActivity.startYourself(this, "Page title", "Category name");

Check out the full implementation for this feature here:

The changes are now merged to master and you could take a pull of the code base and run it for yourself. :)


Make sure you give this post 50 claps and follow me if you enjoyed this post and want to see more!