Flickr for Dojo

Published:

I've been working on a little Dojo based application which talks to Flickr, so I put together a little library which uses Dojo to talk to Flickr using it's rest JSON interface.

Use It's pretty simple to use, just include the JavaScript file:

<script src="flickr.js"></script>
Tell the library what your keys are:
flickr.keys(API_KEY, SECRET_KEY);
And you're set to go.

The main entry-point is flickr.call. As the first argument, you pass in a hash of arguments, as described in the Flickr API documentation. The method you're calling is included in this hash. The second argument is optional and is a callback to be called with the response from the Flickr servers. The response will come back in JSON format so it is easy to handle it in JavaScript. The Flickr JSON response format is discussed in detail on the Flickr site.

So what would all this look like? Something like this will load interesting photos from Flickr and add them to the current document:

flickr.keys(API_KEY, SECRET_KEY);
var pagenum = 1;
function interesting () {
    flickr.call({method:'flickr.interestingness.getList',
            page: pagenum, per_page: 10}, interesting_cb);
    pagenum++;
}
function interesting_cb (response) {
    if (response.stat != 'ok') {
        var error = document.createElement('div');
        error.appendChild(document.createTextNode(response.message));
        document.body.appendChild(error);
        return;
    }
    for (var i in response.photos.photo) {
        var photo = response.photos.photo[i];
        var img = document.createElement('img');
        img.classname = 'interesting';
        img.setAttribute('src', 'http://farm'+photo.farm+
                '.static.flickr.com/'+photo.server+'/'+photo.id+
                '_'+photo.secret+'_s.jpg');
        img.setAttribute('width', '75');
        img.setAttribute('height', '75');

var a = document.createElement('a'); a.setAttribute('href', 'http://www.flickr.com/photos/'+ photo.owner+'/'+photo.id); a.appendChild(img);

document.body.appendChild(a) } }

Implementation I'm not actually using all that much from Dojo. The main thing I'm taking is the crypto library, specifically dojo.crypto.MD5. The way I'm making the actual JSON calls is by appending elements to the page. Perhaps at some point I'll move to using Dojo's ScriptSrcIO but right now I'm not.

The current version of the code is attached: flickr.js