blog archive contact about feed

UK Flight Arrival And Departure RSS Feeds

After the interest in my train arrival and departure RSS feeds, I decided to look at converting my WAP Heathrow Arrivals script to produce an RSS feed.

Instead of just rehashing the old code, I decided to write it again from scratch. The BAA now has a flight search script that makes it easier to look flights at all their UK airports. I decided to make use of this when building my new feed.

The Perl script can be found below if you want to install it on your own server to save my bandwidth, and a demo version is also available online at http://www.robertprice.co.uk/cgi-bin/flights_rss.pl.

The script takes 3 parameters.

  • flightnumber - the number of the flight you are interested in.
  • airport - the code of the airport you are interested in.
  • type - the type of data, either departures or arrivals.

For example, flight AA134 arriving at Heathrow would be http://www.robertprice.co.uk/cgi-bin/flights_rss.pl?flightnumber=AA134&airport=LHR&type=arrivals.

You'll notice how we have to use the standard 3 character IATA airport codes for the airport name. The script understands the following codes...

  • ABZ - Aberdeen
  • EDI - Edinburgh
  • LGW - Gatwick
  • GLA - Glasgow
  • LHR - Heathrow
  • SOU - Southampton
  • STN - Stanstead

Here is a screen shot of flight AA134 as shown on NetNewsWire Lite running on OS X.

RSS feed of flight AA134

The feed doesn't contain any syndication refresh information in it. I'm assuming it's best to just refresh by hand each time you want fresh data. Of course, adding the code would be trivial, so see my article on train departure and arrival RSS feeds for example code.

The script is fragile as it relies on screen scraping, but it works as of the 15th July 2004.

#!/usr/bin/perl -w ## Script to convert flight arrival and departures from BAA ## into RSS feeds. ## Takes the parameters "flightnumber","type" and "airport" ## Airports are LHR (Heathrow), LGW (Gatwick), STN (Stanstead), ## SOU (Southampton), ABZ (Aberdeen), GLA (Glasgow) or ## EDI (Edinburgh) ## Type is either "arrivals" or "departures" ## ## Robert Price - www.robertprice.co.uk - 13 July 2004 use strict; use CGI; use HTTP::Request; use LWP::UserAgent; use XML::RSS; ## Take the flightnumber, type, and airport my $CGI = new CGI; my $flightnumber = $CGI->param('flightnumber') || 'BY212B'; my $airport = $CGI->param('airport') || 'LHR'; my $type = $CGI->param('type') || 'arrivals'; ## The URL we have to screen scrape for the information. my $url = 'http://www.baa.com/cgi-bin/flightSearch.cgi?Airport=' . $airport . '&SearchType=' . $type . '&FlightNum=' . $flightnumber; ## Create our new browser, and pretend to be IE. my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); ## Get the data from the BAA website. my $req = HTTP::Request->new(GET => $url); $req->header('Accept' => 'text/html'); my $res = $ua->request($req); if ($res->is_success) { my $page = $res->content; ## Create our RSS data, update every 10 minutes. my $rss = new XML::RSS (version => '1.0'); $rss->channel( title => $flightnumber . ' ' . $type, link => 'http://www.baa.com/', description => 'The ' . $type . ' status of flight ' . $flightnumber, ); ## make sure there are no errors on the page and we have info. unless (($page =~ m[<td>No matching flights found</td></tr>]sg) || ($page =~ m[</SCRIPT>\s+</HTML>]sg) || ($page =~ m[unable to supply information]sg)) { ## regex out the information we need. $page =~ m[<td colspan=5><b>(.*?)</b></td></tr><tr id='n\d'>.*?<tr id='.*?' ><td width=12pcx>\&nbsp;</td><td><b>(.*?)</b></td><td>(.*?)</td><td>(.*?)</td> <td>(.*?)</td> <td>(.*?)</td></tr>]sg; ## Build a hash to store the information in. my $flight = { 'link' => $url, 'day' => $1, 'time' => $2, 'number' => $3, 'to_from' => $4, 'status' => $5, 'terminal' => $6, }; ## Add the flight to the RSS feed. $rss->add_item( title => $flight->{'time'} . ' - ' . $flight->{'number'} . ' (' . $flight->{'status'} . ')', link => $flight->{'link'}, description => "Day: " . $flight->{'day'} . ",\n" . "Time: " . $flight->{'time'} . ",\n" . "Number: " . $flight->{'number'} . ",\n" . "To/From: " . $flight->{'to_from'} . ",\n" . "Status: " . $flight->{'status'} . ",\n" . "Terminal: " . $flight->{'terminal'} . ",\n" . "Airport: " . $airport . ",\n" . "Arrivals/Depatures: " . $type. ",\n" ); ## We seem to have a problem, so return an error feed item. } else { $rss->add_item( title => $flightnumber . ' not found', link => 'http://www.baa.com/', description => 'Unable to find information for that flight. Please check your details or look on the BAA.com website' ); } ## Return the RSS feed, ensuring we have the correct MIME type. print "Content-type: application/rdf+xml\n\n"; print $rss->as_string; }

I'd welcome any feedback on this, so feel free to contact me.

Entered: 2004-07-15 09:08:14
TRACKBACK - http://www.robertprice.co.uk/cgi-bin/robblog/trackback.pl?id=388

Trackbacks

Ten Cool Things You Can Do With Webfeeds by Cutting Through...
Getting bored with using webfeeds to read blogs? Then here's the Cutting Through Guide To 10 Cool Things You Can Do With Webfeeds Get a new slant on the news Get a new perspective on the world by subscribing...

Tracked: 2004-10-19 11:34:58

Rob's Other Blog Entries

See other blog entries for July 2004, or an index of all blog entries.