Transport for Londons nice web 2.0 style website would provide this basic requirement but alas they don't." />

RSS Feed of the London Underground Tube Status

I've been spending some time making an RSS feed of the London Underground tube status as I've been working on a few projects that could use one. You would think that Transport for Londons nice web 2.0 style website would provide this basic requirement but alas they don't. There are a few developers out there who have written some scraping scripts to convert this information, but most of them no longer work as they haven't been updated to work with the new site.

Previously I have used the script at conor.net to power any projects requiring a tube RSS feed but this went down a few weeks ago when I was in the middle of some crucial testing. So, like any other crazy developer would, I decided to write my own PHP scraping script that would generate me a nice RSS feed to basically tell me TFL had messed up my journey home!

For those of you just looking for an RSS feed of the London Underground tube delays, you can find it at http://tube.bendodson.com/rss.xml - This is automatically updated every 5 minutes and will email me if anything goes wrong so I can fix it! If you are going to use it for any projects, please let me know so that I can monitor bandwidth, etc. If you are interested in how it works, then the code that powers this is below - feel free to use and modify it for your own projects:

<?php

// This file should be run on a CRON job every 5 minutes.
// The server will need to be able to write to this folder in order to create the XML file.

$array = array(
        
'bakerloo',
        
'central',
        
'circle',
        
'district',
        
'east london',
        
'hammersmith & city',
        
'jubilee',
        
'metropolitan',
        
'northern',
        
'piccadilly',
        
'victoria',
        
'waterloo & city'
        
);

$i 0;
foreach (
$array as $ref) {
    
$id str_replace(' ','',$ref);
    
$id str_replace('&','and',$id);

    
$lines[$i]['id'] = $id;
    
$lines[$i]['name'] = ucwords($ref);
    
$lines[$i]['url'] = 
        
'http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/tube-' $id '-now.html';

    
$i++;
}

$i 0;
foreach (
$lines as $line) {
    
$file file_get_contents($line['url']);
    
$needle strtoupper($line['ref']) . ' LINE:';
    
$pos strpos($file,$needle);
    if (
$pos !== false) {
        
$pos += strlen($needle);
        
$end strpos($file,'</div>',$pos);
         
$status strip_tags(substr($file,$pos,($end-$pos)));
    } else {
        
$status 'A good service is currently running.';
    }
    
    
$lines[$i]['status'] = $status;
    
$i++;
}

$now date('r');

$xml = <<<XML

<rss version="2.0">
    <channel>
        <title>London Tube Updates</title>
        <link>http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html</link>
        <description>The latest London Underground tube update information</description>
        <language>en</language>
        <pubDate>$now</pubDate>
        <lastBuildDate>$now</lastBuildDate>
        <docs>http://tube.bendodson.com/</docs>
        <webMaster>ben@bendodson.com (Ben Dodson)</webMaster>
        <generator>PHP script by Ben Dodson</generator>
        <ttl>5</ttl>
        <image>
            <title>Transport for London</title>
            <url>http://www.tfl.gov.uk/tfl-global/images/roundel.gif</url>
            <link>http://www.tfl.gov.uk/</link>
            <width>52</width>
            <height>44</height>
            <description>Transport for London Logo</description>
        </image>
XML;

foreach (
$lines as $line) {
    
$xml .= '<item>' "\r\n";
    
$xml .= '<title>' htmlentities(ucwords($line['name'])) . '</title>' "\r\n";
    
$xml .= '<link>' $line['url'] . '</link>' "\r\n";
    
$xml .= '<description>' htmlentities($line['status']) . '</description>' "\r\n";
    
$xml .= '</item>' "\r\n";
}
        
$xml .= '</channel>' "\r\n";
$xml .= '</rss>';

file_put_contents('rss.xml',$xml);

?>

As I mentioned earlier, I am working on a few projects that utilise this script and I shall be making those available shortly. However, for a sneak peek, you can check out iTube, my Web App for iPhone that keeps you up-to-date on all of the London Underground lines. There are still a few improvements to be made but you get the general idea!

Comments

Posted by Athan at 10:35am on October 5th, 2008

This is one of the best sites I have ever found. Thanks!!! Very nice and informal. I enjoy being here.,

Add a Comment





« Return to homepage or archive?