Using YQL to load and convert RSS feeds really, really fast.
Tuesday, December 8th, 2009My esteemed colleague Stoyan Stefanov is currently running an advent calendar (blog post a day) on performance. Today I have a guest slot on his blog showing how you can use YQL to retrieve five RSS feeds much faster than with any other technology.
As stated at the end of the article, you could use a YQL open table with embedded JavaScript to move all of the hard conversion work to the YQL server, too.
This table does exactly that. The speed of the retrieval slows down a bit with this (as YQL needs to do another request to pull the table definition):
However, using this table to retrieve multiple feeds as HTML is dead easy:
$data = array(
‘http://code.flickr.com/blog/feed/rss/’,
‘http://feeds.delicious.com/v2/rss/codepo8?count=15’,
‘http://www.stevesouders.com/blog/feed/rss’,
‘http://www.yqlblog.net/blog/feed/’,
‘http://www.quirksmode.org/blog/index.xml’
);
$url =’http://query.yahooapis.com/v1/public/yql?q=’;
$query = “use ‘http://github.com/codepo8/yql-rss-speed-comparison/raw/master/rss.multi.list.xml’ as m;select * from m where feeds=”’”.implode(“’,’”,$data).”’” and html=’true’ and compact=’true’”;
$url.=urlencode($query).’&format=xml&diagnostics=false’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
$content = preg_replace(‘/.*
$content = preg_replace(‘/div>.*/’,’div>‘,$content);
echo $content;
To use the open table you simply need to give it the list of RSS feeds as the feeds parameter:
use ‘http://github.com/codepo8/yql-rss-speed-comparison/raw/master/rss.multi.list.xml’ as m;
select * from m where feeds=”
‘http://code.flickr.com/blog/feed/rss/’,
‘http://feeds.delicious.com/v2/rss/codepo8?count=15’,
‘http://www.stevesouders.com/blog/feed/rss’,
‘http://www.yqlblog.net/blog/feed/’,
‘http://www.quirksmode.org/blog/index.xml’
” and html=’true’ and compact=’true’
Try it out in the YQL console.
The html
parameter defines if you want to get HTML back from the table. Take it out to get a list of feeds instead.
See the results as HTML (with an HTML parameter) or as feeds (without the HTML parameter).
The compact
parameter defines if you want to get descriptions back for each entry or not.
See the results as HTML (without descriptions) or as HTML with descriptions.
By using the JSON-P-X output format (xml with callback) you could easily use this in JavaScript:
If you want to compare yourself, get the source code of all the examples from GitHub.