Ben Dodson

Freelance iOS, Apple Watch, and Apple TV Developer

New website goes live!

I've finally got around to giving the site a visual refresh which can now be viewed here at bendodson.com/weblog - There are a few bugs (mainly IE6) to iron out but on the whole it should be working!

The site was redesigned by Liza Hayes who took her inspiration mainly from Doctor Who! I've been building it slowly over the past few months, but I'm currently at Mashed so decided whilst I had the time dedicated to coding I really should get on and finish it!

There will be various updates to the site over the next few days so keep posted!

RSS Feed of the London Underground Tube Status

Please note that this page is now outdated - for any updates to the London Underground, you should use my dedicated Tube Status API and RSS feeds

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://tubeupdates.com/rss/ - This is automatically updated every minute 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/weblog/</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 Tube Updates, an API for any developer that would like to make use of this data. There are still a few improvements to be made but you get the general idea!

Firefox extensions updated for Beta 3 compatibility

I've spent some time updating my firefox extensions so that all of them work with Firefox 3 Beta 3. At present they can only be downloaded from this site as it takes the Mozilla Addons team a few days to approve updates. My extensions page is now back up and running though and contains details on all 3 of my Firefox Extensions (Font Finder, Stop! Hammertime!, and STOP in the name of love) as well as download links for the new releases.

I won't be making any further updates to the stop buttons (apart from making them work with future version of Firefox) but I will be continuing development on Font Finder which could benefit from a visual overhaul as well as a clipboard function! I'll be updating on here when it happens so sign up to the RSS feed for the latest news!

Google extended encoding made easy!

I've been having a play around with Google Charts recently but came across a problem with the range of values that can be used. With simple encoding, you can only have values between 0 and 64, and with text encoding only values between 0 to 100. This is annoying when dealing with things such as tracking website hits or weight when values are typically much higher.

I played around with some functions to try and factorise the numbers so that they would be in the range of 0 to 100 but this didn't go so well so I decided to tackle Google's extended encoding.

This is a system of encoding that basically takes a pair of alphanumeric characters and translates them into a number between 0 and 4095 (much better!). So for example, B9 translates to 125 and .a would translate to 4058. However, although this has a much larger range, it's a bit harder to get your head around and so what is needed is a simple function (or two) to convert to and from the extended encoding. Predictably, I have said PHP functions here:

<?php

function array_to_extended_encoding($array)
{
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';

    $encoding = '';
    foreach ($array as $value) {
        $first = floor($value / 64);
        $second = $value % 64;
        $encoding .= $characters[$first] . $characters[$second];
    }

    return $encoding;
}

?>
<?php

function extended_encoding_to_array($string)
{
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';  

    for ($i=0;$i<strlen($characters);$i++) {
        $first[$characters[$i]] = $i * 64;
    }

    for ($i=0;$i<strlen($characters);$i++) {
        $second[$characters[$i]] = $i;
    }

    $pairs = str_split($string,2);
    foreach ($pairs as $pair) {
        $value = $first[$pair[0]];
        $value += $second[$pair[1]];
        $values[] = $value;
    }

    return $values;
}

?>

They work fairly simply by taking advantage of PHP's casting methods in that a string can be interpreted as an array.

In the first function, array_to_extended_encoding(), an array of numbers should be passed e.g. array(250,39,400,1904,2). The function first lists all of the extended encoding characters in order as one string named $characters. It then loops through our array of numbers, and creates two variables; $first and $second. In $first, we store the number from our array divided by 64 and rounded down using the floor() function. For $second, we use the modulus operator (%) to find the remainder once the number from the array is divided by 64. We then take both $first and $second and work out the encoding by looking up the numbers from within the $characters cast as an array. This gives us 2 characters which make up the extended encoding for the number. We keep extending the value of $encoding until we end up with a string representing the full extended encoding of the array passed to the function.

The second function, extended_encoding_to_array(), is slightly more advanced. It accepts a string as its only parameter which should be an extended encoding (e.g. $encoding = 'AA..B9aC'). We first list all of the characters in a string as we did for our previous function, but then we create two arrays which will contain all of the numbers we need to decode the extended encoding. In $first, we create an array of each character in the $characters string as a number multiplied by 64 (so the letter b would be 64 as it is signified by number 1 in the array multiplied by 64). Within $second we perform a similar operation but instead just assign the number of the character rather than multiplying it by 64. This gives us two arrays, with keys relating to each character that can be used as an encoding and values as the numerical equivalent. It is now a simple case of splitting the string that was passed to the function into chunks of 2 characters using str_split() and looping through the returned array setting the value of each character within the pair from the arrays we created. We then add the two returned numbers together to give us the decoded figure and add it to an array which will be returned by the function.

Simple? It has certainly made things a lot easier for the functions I'm writing for a PHP wrapper for Google Charts and hopefully it will help somebody else as well. If you have any questions or comments, please get in touch.

Firefox Extensions page will be right back!

As some of you may have noticed, the new site design has caused the Firefox Extensions page to go awry. This will return shortly (along with a few new extensions) but for the time being you are better to go the Mozilla Addons site to download the extensions.

Stop! Hammertime - https://addons.mozilla.org/en-US/firefox/addon/974
Font Finder - https://addons.mozilla.org/en-US/firefox/addon/4415
Stop in the Name of Love - https://addons.mozilla.org/en-US/firefox/addon/3771

Update: The extensions page is now up and running again and can be viewed at https://bendodson.com/weblog/extensions/

Newer Entries »