mediawiki-apiwikimedia-commons

Wikipedia API: Cannot query Picture of the Day URL


I read the MediaWiki API documentation but I cannot find how to get the current URL of the picture of the day.

Following RTFM, I call the query from Commons:Potd to its page URL.

It returns pageid=2518149, it is the current POTD page.

Then I try to call prop=imageinfo, but I didn't find the way to get the image URL.

The only successful call is this:

http://commons.wikimedia.org/w/api.php?titles=File:POTD&action=query&prop=images

"Successful" means it returns an image URL ... but of a different pageid (not the current POTD)!


Solution

  • Hmm, this isn't very easy. You're not getting anything back from Commons:Potd because that's just a redirect, but even if you fetch images from Commons:Picture of the Day, you just get a huge list of all images ever in alphabetical order.

    But here's one solution. First expand the Potd template to figure out the current image name:

    http://commons.wikimedia.org/w/api.php?action=expandtemplates&text={{Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY2}}}}

    <api>
      <expandtemplates xml:space="preserve">20110421 Tbilisi Georgia Panoramic.jpg</expandtemplates>
    </api>
    

    Then plug that filename (URL-encoded) into a separate request to imageinfo, with iiprop=url to get the URL:

    http://commons.wikimedia.org/w/api.php?titles=Image:20110421%20Tbilisi%20Georgia%20Panoramic.jpg&action=query&prop=imageinfo&iiprop=url

    And there it is!
    The picture file itself can be retrieved using the value of the url attribute:

    <api>
      <query>
        <normalized>
          <n from="Image:20110421 Tbilisi Georgia Panoramic.jpg" to="File:20110421 Tbilisi Georgia Panoramic.jpg" />
        </normalized>
        <pages>
          <page pageid="15527584" ns="6" title="File:20110421 Tbilisi Georgia Panoramic.jpg" imagerepository="local">
            <imageinfo>
              <ii url="http://upload.wikimedia.org/wikipedia/commons/a/ae/20110421_Tbilisi_Georgia_Panoramic.jpg" descriptionurl="http://commons.wikimedia.org/wiki/File:20110421_Tbilisi_Georgia_Panoramic.jpg" />
            </imageinfo>
          </page>
        </pages>
      </query>
    </api>