#!/usr/bin/perl -w
#
# Get dart times and parse them into something readable
#
# http://www.irishrail.ie/your_journey/picker_real_time_results.asp?station_name=Glenageary&station=GYDN%2CGYUP&direction=S&mins=60&image1.x=11&image1.y=5&radioservice=1&radioservice1=1
# http://www.irishrail.ie/your_journey/picker_real_time_results.asp?station_name=Glenageary&direction=S&mins=60&image1.x=11&image1.y=5&radioservice=1&radioservice1=1 also works
#
use WWW::Mechanize;
use HTML::TokeParser;
use CGI;
use strict;
use constant CACHEDIR => "/var/tmp/.dartcache";
-d CACHEDIR or mkdir CACHEDIR, 0755;
my $agent = WWW::Mechanize->new( env_proxy => 1, autocheck => 1 );
$agent->agent_alias( 'Windows IE 6' );
# arrange these from north to south
my @locations = (
[ "Skerries", 0, 0 ],
[ "Sutton", 0, 0 ],
[ "Balbriggan", 0, 0 ],
[ "Bayside", 0, 0 ],
[ "Clontarf Road", 0, 0 ],
[ "Donabate", 0, 0 ],
[ "Drogheda", 0, 0 ],
[ "Drumcondra", 0, 0 ],
[ "Gormanston", 0, 0 ],
[ "Harmonstown", 0, 0 ],
[ "Howth Jct", 0, 0 ],
[ "Howth", 0, 0 ],
[ "Kilbarrack", 0, 0 ],
[ "Killester", 0, 0 ],
[ "Laytown", 0, 0 ],
[ "Malahide", 0, 0 ],
[ "Raheny", 0, 0 ],
[ "Rush & Lusk", 0, 0 ],
[ "Portmarnock", 0, 0 ],
[ "Connolly", 0, 0 ],
[ "Tara Street", 0, 0 ],
[ "Pearse", 0, 0 ],
[ "Gd Canal Dock", 0, 0 ],
[ "Lansdowne Rd", 0, 0 ],
[ "Sandymount", 0, 0 ],
[ "Sydney Parade", 0, 0 ],
[ "Booterstown", 0, 0 ],
[ "Blackrock", 0, 0 ],
[ "Seapoint", 0, 0 ],
[ "Salthill", 53.29532, -6.15241 ],
[ "Dun Laoghaire", 53.29503, -6.13496 ],
[ "Sandycove", 53.28812, -6.12722 ],
[ "Glenageary", 53.28132, -6.12290 ],
[ "Dalkey", 53.27562, -6.10307 ],
[ "Killiney", 0, 0 ],
[ "Shankill", 0, 0 ],
[ "Bray", 0, 0 ],
[ "Greystones", 0, 0 ],
);
my $station;
my $cgi;
my $output = "text";
my ( $wap, $html );
if ( $ENV{'REMOTE_ADDR'} ) {
$cgi = new CGI;
# find out what the hell is talking to us
$wap = $cgi->Accept( "text/vnd.wap.wml" );
$html = $cgi->Accept( "text/html" );
# prefer wap only if it's higher up the list
if ( $wap >= $html ) {
$output = "wap";
} else {
$output = "html";
}
# or explictly requested
if ( $cgi->param( "fmt" )) {
$output = $cgi->param( "fmt" );
if ( $output ne "html" and $output ne "wap" and $output ne "xml" ) {
$output = "html";
}
}
$station = $cgi->param( "station" );
}
if ( $output eq "html" ) {
print $cgi->header;
print $cgi->start_html( -title => "DART Timetable (HTML)" );
} elsif ( $output eq "xml" ) {
print $cgi->header( -type => 'text/xml' );
} elsif ( $output eq "wap" ) {
print $cgi->header( -type => 'text/vnd.wap.wml',
-vary => '*' );
print <<"WML";
";
}
if ( @times ) {
my $maxw = 0;
map {
if ( length( $_->[0] ) > $maxw ) {
$maxw = length( $_->[0] );
}
} @times;
$maxw++;
print "Times for $se\n";
if ( $output ne "text" ) {
print "\n";
}
for my $t ( @times ) {
if ( $output eq "text" ) {
printf( "%-${maxw}s %-8s %7s\n", @{$t} );
} else {
my @t = ( $t->[0], $t->[2] ); # skip platform
print "
\n ";
}
}
if ( $output ne "text" ) {
print "";
print join( " ", @t );
print "
pref wml: $wap, html: $html
\n"; print ""; } print "Please choose a DART station"; if ( $output ne "text" ) { print "
"; } print "\n"; if ( $output eq "html" ) { print "