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.
RSS2map – a Placemaker/Yahoo Maps mashup generator script
Tuesday, July 14th, 2009This morning I went to the Telegraph Media Group to talk about GeoMaker and Placemaker. The main thing they wanted to know was how to place an RSS feed that does not have any Geo data on a map and how to style it.
To make this easier I created rss2map, a demo PHP script with all the CSS and JavaScript needed to show a feed on a map:
- Get the code from GitHub RSS2map on GitHub
- Read the documentation on GitHub RSS2map docs
- Check out the rss2map demo page
Sky is dishing out £10k for widgets using their RSS feeds
Tuesday, November 6th, 2007Well, in a competition, not just directly. I like the idea of Sky News’ Developer Competition where they offer all in all £10k in prize money for widget and badge implementations of the content of their RSS feeds.
Talented developers can use the feeds to create their own applications. The brains with the best ideas will be invited to a Dragons’ Den-style judgment day at the Sky News studios.
A bit of a shame is that they haven’t got an API yet, but I pointed them to Yahoo! Pipes to offer developers as a workaround to use the RSS feeds in an easier manner.
The deadline is 30th of November and the Dragon’s Den shootout is on the 20th of December in the Sky offices. So if you want to have a go, get coding :) Make also sure you check the Terms and Conditions of the competition.
It’s all about APIs these days.
Friday, November 2nd, 2007It is quite cool to see the increase of coverage of the topic of web APIs. It is also very exciting to APIs finally evolving to work across several systems, aggregate and move from a one way stream of retrieving data to an alternative entry point for applications. How cool will it be for example to write reviews for amazon on movie or book sites? Having write APIs would allow us to leverage the knowledge of people on the web at the places they hang out rather than having to lure them into using a web app.
Anyways with this new API interest I had a triple release today: There is a podcast about APIs for .net magazine together with Jeremy Keith, Paul Hammond, Drew McLellan and hosted by Paul Boag, Ajaxian is featuring my ‘hack’ of the Slideshare RSS feed and I uploaded the presentations I gave at the University Hack Day introduction at Dundee, Scotland yesterday.
Enjoy!
Showing off your presentation slides with slideshare, PHP and a bit of JavaScript
Wednesday, October 31st, 2007First of all, I am a big fan of slideshare, a web app that allows you to upload presentations in powerpoint, open office or PDF and share it on the web. Slideshare converts the presentation (sadly enough not 100% when it comes to fonts and kerning :-( ) and people can comment on them, there is a text version of all slides and you can embed the slides in your blog or other sites.
When I checked my slides I had a look at the API of slideshare but I am always a bit bored with having to go through a developer ID and then do everything on the server. That’s why I put on my “ethical hacker” hat and took a look at the RSS feed of my slides and found everything I need there! If you look at the source of the feed you’ll see that it contains not only the titles and descriptions but also the media code, in this case the HTML to embed the right flash movies.
Taking this information it is pretty easy to build a viewer that allows people to click through all your presentations without having to leave your site. This can look something like this:
When JavaScript is available this will be the look and feel and functionality. When JS is turned off all you’ll get is an unstyled list of links pointing to the presentations on slideshare.net.
You can check the slideshare show in action and get a zip to download and use on your site if you don’t want to know how it is done. If you do, read on…
The code necessary is really easy and done in about 70 lines. Let’s go through it bit by bit. I am using PHP4 together with cURL, DOMXML and some JavaScript using the YUI.
<?php $url = 'http://www.slideshare.net/rss/user/cheilmann'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $slides = curl_exec($ch); curl_close($ch); |
It starts with the URL we want to load and a CURL call to pull this file and store it in the variable $slides.
$slides = str_replace(‘slideshare:embed’,’slideshareembed’,$slides);
$slides = str_replace(‘media:title’,’mediatitle’,$slides);
$xml = domxml_xmltree($slides);
To make things easier (as DOMXML is a terribly hacky piece of kit – much easier with PHP5 and SimpleXML that one) I rename the namespaced attributes in the feed containing the embed code and the title of the media to simple elements and create an object collection from the XML using domxml_xmltree.
$json = array();
$slidesharelist = ‘’;
$links = $xml->get_elements_by_tagname(‘link’);
$img = $xml->get_elements_by_tagname(‘url’);
$titles = $xml->get_elements_by_tagname(‘mediatitle’);
$embeds = $xml->get_elements_by_tagname(‘slideshareembed’);
Then I need to preset an array to contain the embed code for each slides and a string to contain the list of links pointing to presentations on slideshare. I use the get_elements_by_tagname method of DOMXML to get arrays of the different bits of content that I need from the RSS feed.
foreach ($embeds as $key=>$el) {
$l = $links[$key+2]->children[0]->content;
$t = $titles[$key]->children[0]->content;
$slidesharelist .= ‘
$emb = $el->children[0]->content;
if(strpos($emb,’
preg_match_all(‘/.*(
$json[]=’‘’.$obj[1][0].’‘’;
}
?>
By looping throught the “embeds” array I assembling a list of links pointing to the different presentations and add the embed code to the JSON array. I need this one later to show the different flash movies when visitors click the presentation links. Notice that I need to skip the first two LINK elements as that is the one pointing to the main URL of the RSS feed. For some reason the order of embeds was different on my localhost and my live server, which is why I added that extra if statement. Annoying, that.
That is all the PHP we need! Now it is time to make it pretty and add the rest of the HTML.
As it is hacky enough to mix PHP and JavaScript I put all the CSS fun in an own document and only add the logo of the RSS feed as the background of the slideshow container. The markup is a main DIV with an unordered list that gets the HTML assembled earlier in the PHP script. This shows the links but doesn’t do the dynamic showing yet. For that we need JavaScript.
That’s all, except for putting the data from the RSS feed into a “slides” array and closing the module pattern.
Together with the right style sheet this is enough to have a clickable list of your latest presentations on slideshare. Enjoy.