Creating A Personalised Events List With SimplePie
Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
28th November 2008 - 5 minutes read time
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.
If you've ever looked at the settings in a digital camera, or have experience with image processing programs like GIMP, then you may have seen a colour histogram. This is a simple graph that shows the amount of different shades of colour are present in the image.
I was writing unit tests for a API mapping function recently and came across this interesting issue. The code I was writing tests for was in a legacy codebase that I was making changes to, and it made sense to have some unit tests in there before I started work to ensure everything worked before and after.
Generating a PDF document from a web page through PHP can be problematic. It's often something that seems quite simple, but actually generating the document can be difficult and time consuming.
Reflection is used to gather more information about code during the runtime of the program. This allows code to inspect itself and to make small modifications, which is useful in a variety of situations.
Add new comment