Example: Reusing a YQL query

In this example, the Flickr Recent Photos YQL table is used to pull in a small set of recent Flickr images every 8 seconds.

Setting Up the Query

The easiest way to build a YQL query is by visiting the YQL Console. In this example we will be using the flickr.photos.recent table. The YQL statement that we are using looks like this:

select * from flickr.photos.recent where (api_key = "1895311ec0d2e23431a6407f3e8dffcc")

Setting Up the YUI Instance

Now we need to create our YUI instance and tell it to load the yql and node modules.

YUI().use('node', 'yql');

Making the Query

Now that we have a YUI instance with the yql module, we can now make a query.

YUI().use('node', 'yql', function(Y) {
    
    var res = Y.one('#res'), count = 0,
        url = '<a href="http://flickr.com/photos/{owner}/{id}"><img src="http://static.flickr.com/{server}/{id}_{secret}_t.jpg"></a>';
    
    var q = Y.YQL('select * from flickr.photos.recent where (api_key = "1895311ec0d2e23431a6407f3e8dffcc")', {
        //Tell JSONP to not cache this request so we get new images on each request
        allowCache: false,
        on: {
            success: function(r) {
                if (r.query && r.query.results) {
                    count++;
                    res.setHTML('<h2>Recent Flickr Photos <em>(query #' + count + ')</em></h2>');
                    Y.each(r.query.results.photo, function(v) {
                        res.append(Y.Lang.sub(url, v));
                    });
                }
            }
        }
    });
    Y.later(8000, q, q.send, null, true);
});