]> git.michaelhowe.org Git - pub/michael/national-rail-ticket-split.git/commitdiff
More fixes
authorMichael Howe <michael@michaelhowe.org>
Tue, 8 Apr 2014 22:53:22 +0000 (23:53 +0100)
committerMichael Howe <michael@michaelhowe.org>
Tue, 8 Apr 2014 22:53:22 +0000 (23:53 +0100)
* document lack of error 500 handling
* check if the route is direct, die if not
* time how long it takes

split-route.pl

index 3000c28ba32406fbdc9700bb27b575375e4de65f..43de160da47c72b6e80d76c80999509f29989d69 100755 (executable)
@@ -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;