Quantcast
Channel: Dented Reality » simplepie
Viewing all articles
Browse latest Browse all 3

Simple Activity Streaming with SimplePie

0
0

A few different people have asked me recently how I created the activity stream/life stream you see in the sidebar of Dented Reality. It’s actually really simple, and all it does is load up the feeds from a few different locations, combine them in date order and then output them on my page using an HTML “UL” (unordered list). Based on the source of each feed, it also adds a CSS class to each list element (LI) so that I can add an appropriate icon. Here’s the complete code that I use, and then I’ll explain some parts of it, and some of what makes it tick:

<h2>Me, Live (ish)!</h2>
 <ul>
 <?php

 // Create a new SimplePie object
 $feed = new SimplePie();

 // Instead of only passing in one feed url, we'll pass in whole list
 $feed->set_feed_url(array(
 'http://feeds.delicious.com/rss/beau',
 'http://dentedreality.com.au/TwitterProxy/TwitterProxy.php?strip_replies=true&create_links=true&hashtags=true&strip_name=true&link_people=true',
 'http://ws.audioscrobbler.com/1.0/user/borkazoid/recenttracks.rss',
 'http://digg.com/users/beaulebens/history/diggs.rss',
 'http://www.hulu.com/feed/history/beaulebens'
 ));

 // Initialize the feed object
 $feed->set_cache_location(WP_CONTENT_DIR . '/feeds');
 $feed->set_cache_duration(3600);
 $feed->init();

 // This will work if all of the feeds accept the same settings.
 $feed->handle_content_type();

 // Now we output the combined feed
 $out = 0;
 foreach ($feed->get_items() as $item) {
 $f = $item->get_feed();
 switch ($f->get_link()) {
 case 'http://www.last.fm/user/borkazoid':
 $class = 'music';
 $strings = explode('–', $item->get_title());
 $text = 'Listened to <a href="http://last.fm/music/' . urlencode(trim($strings[0])) . '/_/' . trim($strings[1]) . '">' . trim($strings[1]) . '</a>, <a href="http://last.fm/music/' . urlencode(trim($strings[0])) . '">' . trim($strings[0]) . '</a>';
 break;
 case 'http://twitter.com/beaulebens':
 $text = 'Tweeted: ' . $item->get_title() . ' <a href="' . $item->get_link() . '">#</a>';
 $class = 'status';
 break;
 case 'http://digg.com/users/beaulebens/history/diggs':
 $text = 'Dugg <a href="' . $item->get_link() . '">' . $item->get_title() . '</a>';
 $class = 'digg';
 break;
 case 'http://del.icio.us/beau':
 case 'http://delicious.com/beau':
 $class = 'bookmark';
 $text = 'Bookmarked <a href="' . $item->get_link() . '">' . $item->get_title() . '</a>' . (strlen($item->get_description()) ? ' - ' . $item->get_description() : '');
 break;
 case 'http://www.hulu.com/users/history':
 $class = 'hulu';
 $text = 'Watched <a href="' . $item->get_link() . '">' . $item->get_title() . '</a> on <a href="http://hulu.com/">hulu.com</a>';
 break;
 default:
 $class = 'generic';
 $test = $item->get_title();
 }
 echo '<li>' . $text . ' <small>(' . $item->get_date('j M, g:i a') .')</small></li>';
 $out++;
 if ($out >= 30) break;
 }

 ?>
 </ul>

That’s it. Apologies for the lack of indentation, apparently my tabs didn’t come through properly. OK, let’s walk through it quickly.

This code all appears in my sidebar.php file (part of my custom theme), although I suppose ideally it should be a widget (maybe one day). As I said, it uses the SimplePie library, which is bundled with WordPress and is automatically available in theme files. It makes use of SimplePie’s ability to set an array of feed URLs (instead of just one) to load a couple different URLs and combine them all together.

You’ll notice that one of the URLs in there is weird-looking (http://dentedreality.com.au/TwitterProxy/TwitterProxy.php?strip_replies=true&create_links=true&hashtags=true&strip_name=true&link_people=true). That just uses another script of mine called TwitterProxy to load my Twitter updates and reformat them a bit to include links, exclude certain ones etc. The other feed URLs are all just standard URLs available from different web services I use. I could add/remove feeds on this list to include other ones in my simple activity stream.

Then I set up some configuration options for SimplePie, including configuring a specific cache directory so that I can store local copies of both the source feeds and the combined feed, rather than go and download them every time someone loads my website (which would be bad). I cache everything for an hour each time (3600 seconds), since it doesn’t need to be anywhere near live.

The next big loop (foreach…) just goes through each of the items in the combined collection of feeds and does some processing. Based on where that item came from (which source feed it was in), I set up a string for the output, set a CSS class to add to the LI and then output the item with the datestamp for when it happened. I only output the most recent 30 items across all services, and then I finish up my list and bail out.

So there you have it. That block of code in my theme handles everything for me, providing a custom activity stream on my blog. I could have used one of the available WordPress plugins like Lifestream, RSS Stream or the very powerful DiSo Action Stream, but I decided to just roll my own. If anything, I might go back and use the DiSo option at some point, since I think that project is doing some really interesting things.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images