Playing with YouTube feeds

This is a followup to my Simplepie post a while back. YouTube offers Atom feeds, great – you’d think. However the data that YouTube outputs in its Atom feeds is…a bit different to what you might expect from a standard RSS feed in that it contains a lot of escaped HTML. Essentially for each item’s descripton in the feed YouTube outputs something like the following:

<div style="font-size: 12px; margin: 3px 0px;"><span></span></div></td>
<td style="font-size: 11px; line-height: 1.4em; padding-left: 20px;             padding-top: 1px;" width="146" valign="top"><div><span style="color: #666666; font-size: 11px;">From:</span>
<a href="http://www.youtube.com/profile?user=YouTube">YouTube</a></div>
<div><span style="color: #666666; font-size: 11px;">Views:</span>
15774</div>

Which is basically a div that contains a table and is pretty intrusive if included in it’s native form. A rough and ready solution to just getting the bits you want (or at least I want) is to grab each item’s description and echo it via the substr function, this function allows you to specify which parts of a string you want to echo. It did rely a little bit of counting characters and I’m sure that there are far more efficient ways of doing it but if you, say, wanted the screenshot of the video linked to the video’s location on YouTube then your substr function would look something like this:

<?php $description=$item->get_description(); ?>

<?php echo substr($description,113,145); ?>

Which essentially tells the function to starting echoing from character 114 in the string (the count, as always, starts from 0) and to echo out the 145 characters from that point – which is the portion of the string involved in referencing the video’s screenshot and location.

There are a few other ways I can immediately think of using this particular set of data, will post them when I get a minute.

EDIT –

To produce embedded videos from the Atom feed then something like this will work:

<?php $description=$item->get_description(); ?>

<iframe title=”YouTube video player” width=”480″ height=”390″ src=”http://www.youtube.com/embed/<?php echo substr($description,155,11); ?>” frameborder=”0″ allowfullscreen></iframe>

Simplepie and integrating/parsing RSS feeds

Wow, how’s that for a snappy blog title!

Ok, I realise it sounds slightly dull but utilising RSS feeds can be a useful and relatively straight-forward way to include dynamic information within a site.

I’ve searched far and wide for various RSS integration tools and my favourite is by far and away a RSS parsing class written in php called Simplepie (http://simplepie.org/). Although this hasn’t been developed since version 1.2 was released in 2009 it remains the most straightforward and robust solution I’ve found.

Once you’ve downloaded and included the simplepie.inc file it’s so simple (excuse the pun) to use (here is a very good tutorial on how to set up a simple page featuring one feed). If you want to include multiple feeds there is some helpful info here.

But this is the best solution I’ve found to this particular problem – if you’ve got other solutions then please recommend!