Creating A Personalised Events List With SimplePie

One nice feature for any blog or site (especially news sites) is to have a little list of forthcoming events that would interest your readers. One way of collating this information is to scour the web and update the list manually. However, this is time consuming and tedious, especially as there is an easier way to do it.

Have a look at the Yahoo site called Upcoming. On this site you can search for events on lots of different subjects in lots of different locations all over the world. Also, if you have an event you can put it up on the site.

Because all of the searches can be accessed via RSS it is possible to use SimplePie to collate these results together. The following script will take two arrays, one of locations and the other being the subjects. It will then create the necessary RSS URLs and use the multi-feed aggregator feature of SimplePie to collate all of the different events into a single array. All the script needs to do after this is run through the array and remove duplicates and sort the data by date.

<?php
include('simplepie.inc');
 
// enter a list of areas
$locations = array('england','britain','europe');
// enter a list of subjects
$subjects = array('php','web development','code','javascript');
 
// initialise feed array
$feeds = array();
 
// set up feed array
foreach($locations as $location){
  foreach($subjects as $subject){
    $feeds[] = 'http://upcoming.yahoo.com/syndicate/v2/search_all/?q='.urlencode($subject).'&loc='.$location.'&rt=1';
  }
}
 
// create SimplePie object
$feed = new SimplePie();
// add feeds to SimplePie
$feed->set_feed_url($feeds);
// turn off feed ordering by date
$feed->enable_order_by_date(false);
 
// get the feed contents
$feed->init();
 
// initialise previously done feed ID array
$prevIds = array();
 
// initialise item array
$list = array();
 
// for each feed item
foreach ( $feed->get_items() as $item ) {
  // check if we have not done this item before
  if ( !in_array($item->get_id(true), $prevIds) ) {
 
    // extract and convert the xCal:dtstart variable
    $when = $item->get_item_tags('urn:ietf:params:xml:ns:xcal', 'dtstart');
    $date = $when[0]['data'];
    $sortDate = SimplePie_Misc::parse_date($date);
    $gCalDate = date('j M Y', $sortDate);
 
    // check if the date is already in the list
    if ( isset($list[$sortDate]) ) {
      // try 10 attempts to increase the sort date timestamp and insert date
      for ( $i=0 ; $i < 10 ; ++$i ) {
        ++$sortDate;
        if ( !isset($list[$sortDate]) ) {
          $list[$sortDate] = '<li><a href="'.$item->get_permalink().'" title="'.$item->get_title().'">'.$item->get_title().'</a></li>'. "\n";
          break;
        }
      }
 
    }else{
    // store item in list array using the date as a
    $list[$sortDate] = '<li><a href="'.$item->get_permalink().'" title="'.$item->get_title().'">'.$item->get_title().'</a></li>'. "\n";
    }
    // store a hash of the ID so that the same thing isn't added twice.
    $prevIds[] = $item->get_id(true);
  }
}
 
// sort array by keys
ksort($list);
 
$list = array_slice($list,0,10);
 
echo '<ul>';
echo implode(' ',$list);
echo '</ul>';
 
?>

Rather than explain every step in turn I have just added comments to explain what is going on. Also, in this example I have searched for events in England and Europe that deal with php, web development, code and javascript. However, it seems that PHP isn't such a good keyword to use as it comes up with any event that contains a link with a .php extension. I'm sure this case would be similar with things like HTML so it is best to avoid those keywords for now.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.