From: Michael Howe Date: Tue, 8 Apr 2014 22:53:22 +0000 (+0100) Subject: More fixes X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=3c14b66861725bf0cd08e6e481913afa1e0be34c;p=pub%2Fmichael%2Fnational-rail-ticket-split.git More fixes * document lack of error 500 handling * check if the route is direct, die if not * time how long it takes --- diff --git a/split-route.pl b/split-route.pl index 3000c28..43de160 100755 --- a/split-route.pl +++ b/split-route.pl @@ -51,6 +51,11 @@ Shows this manpage. split-route.pl OXF BHM 2014-05-16 17:00 +=head1 BUGS + +Sometimes traintimes.org.uk returns 500 errors. These are not caught, and so +cause the script to fail. + =cut use WWW::Mechanize; @@ -77,6 +82,9 @@ unless( scalar( @ARGV ) == 4 ){ pod2usage( "Invalid number of arguments passed (use --help for more details)" ); } +# Time everything +my $timer = time; + my ( $src_in, $dest_in, $date, $depart ) = @ARGV; my $mech = WWW::Mechanize->new(); @@ -148,6 +156,7 @@ for(my $i = 0; $i < $#best_route; $i++ ){ } printf "Total price: £%.2f\n", $sum; printf "Direct price: £%.2f\n", $route->{price}; +printf "[calculated in %ds]\n", ( time - $timer ); ## ## functions below here @@ -161,6 +170,12 @@ sub get_stops { my $tree = HTML::TreeBuilder->new_from_content( $mech->content ); my $first_stop = $tree->look_down( '_tag', 'li', 'id', 'result0' ); + + # check if it's direct or not: + my $change_link = $first_stop->look_down('_tag', 'a', 'class', 'change_link'); + if( $change_link ){ + die "Error: first train returned by $url is not direct!\n"; + } my $details_url = $first_stop->look_down( '_tag', 'a', 'class', 'calling_link' )->attr('href'); my $ticket_type = $first_stop->look_down( '_tag', 'span', 'class', 'fare-type tooltip')->look_down('_tag', 'a')->as_text; my $ticket_price = ( $first_stop->look_down( '_tag', 'label' ) )[0]->as_text;