Wikimedia Commons features many images regularly on its main page. As described by Commons,
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:
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
gcmtype: this param’s value is
fileas 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.
prop: Setting the value of
imageinfoblock in the API response. It contains the
extmetadatafields 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.
query-continuenode 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!