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.
A common web design pattern is to incorporate an image into the design of the page. This creates a tighter integration with the image and the rest of the page.
The main issue in designing a page around the image is that the colours of the page must match the image. Otherwise this creates a dissonance between the image and the styles of the site.
XML is a useful format for configuration, data storage, and transmitting data from one system to another. As a human readable format that can be easily read by machines it quickly gained favor in lots of different systems as a mechanism for data storage.
Pi day was a few weeks ago, but I came across this simple approximation of pi recently and decided to put together an example in PHP since it seemed pretty simple.
This approximation of pi centers around a real world example, but we can simulate this using some code.
A parabolic curve is a type of curve where every point is an equal distance from a focal point. There a number of different way to generate this sort of curve using math, but one of the simplest is to use straight lines to create the illusion of the curve.
I quite like the end of the year report from Spotify that they call "Wrapped". This is a little application in which they tell you what your favorite artist was and what sort of genres you listened to the most during the year.
Add new comment