]> git.michaelhowe.org Git - pub/michael/national-rail-ticket-split.git/commitdiff
Add support for checking prices for two master
authorMichael Howe <michael@michaelhowe.org>
Sat, 6 Aug 2016 13:25:55 +0000 (14:25 +0100)
committerMichael Howe <michael@michaelhowe.org>
Sat, 6 Aug 2016 13:25:55 +0000 (14:25 +0100)
Adds a --two option, which assumes a two together railcard and 2 adults.

Also accept 502 as a valid response code - this seems to be what
traintimes.org.uk returns if you have a query string on the URL.

split-route.pl

index 753370f99e578714ae4cb4abcbc228943f13f24a..7a93cff5a71767bb4384cc65a6882560a0da5f7f 100755 (executable)
@@ -9,7 +9,7 @@ split-route.pl - parse a train route and attempt to find split tickets
 
 =head1 SYNOPSIS
 
-split-route.pl [--debug] [--help|--man] SRC DEST DATE TIME
+split-route.pl [--debug] [--help|--man] [--two] SRC DEST DATE TIME
 
 =head1 DESCRIPTION
 
@@ -45,6 +45,10 @@ Gives a short help
 
 Shows this manpage.
 
+=item --two
+
+Assumes that two adults are travelling, with a two together railcard.
+
 =back
 
 =head1 EXAMPLE USAGE
@@ -68,12 +72,14 @@ use Getopt::Long;
 use Pod::Usage;
 
 my $debug = 0;
+my $two = 0;
 my ( $help, $man );
 
 GetOptions(
     "debug" => \$debug,
     "help"  => \$help,
     "man"   => \$man,
+    "two"   => \$two,
 );
 
 pod2usage(1) if ($help);
@@ -88,13 +94,19 @@ my $timer = time;
 
 my ( $src_in, $dest_in, $date, $depart ) = @ARGV;
 
-my $mech = WWW::Mechanize->new();
+my $mech = WWW::Mechanize->new( autocheck => 0 );
 
 my $query_site = "http://traintimes.org.uk";
 
+# XXX: hack for my default 2-together journey
+my $url_suffix = "";
+if( $two ){
+    $url_suffix = "?railcard=2TR&railcardN=1&adults=2";
+}
+
 say "Finding routes from $src_in to $dest_in on $date at $depart";
 
-my $url = "${query_site}/${src_in}/${dest_in}/${depart}/${date}";
+my $url = "${query_site}/${src_in}/${dest_in}/${depart}/${date}${url_suffix}";
 
 # Hash to hold all journeys, of the form:
 # SRCSHORTCODE => { DESTSHORTCODE1 => { stops => [], price => 'price', type => 'type' },
@@ -168,6 +180,9 @@ printf "Direct price: £%.2f\n", $route->{price};
 my $saving  = $route->{price} - $sum;
 my $percent = 100 * $saving / $route->{price};
 printf "Saving: £%d (%.f%%)\n", $saving, $percent;
+if( $two ){
+    print "For two adults with a two together railcard\n";
+}
 printf "[calculated in %ds]\n", ( time - $timer );
 
 ##
@@ -178,6 +193,12 @@ sub get_stops {
     debug("get_stops( $url )");
 
     $mech->get($url);
+    # Unfortunately, the site seems to return 502 if we pass through a query
+    # string (eg ?railcard=...), but still returns the correct data.  Therfore,
+    # treat that as correct.
+    unless( $mech->success or $mech->status == 502 ){
+        die "Error fetching $url: " . $mech->status;
+    }
 
     my $tree = HTML::TreeBuilder->new_from_content( $mech->content );
 
@@ -222,7 +243,7 @@ sub get_stops {
     debug("Ticket type: $ticket_type");
     debug("Cost: $ticket_price");
 
-    my $stops  = _parse_stopping_points("$query_site/$details_url");
+    my $stops  = _parse_stopping_points("$query_site/$details_url$url_suffix");
     my %return = (
         stops => $stops,
         type  => $ticket_type,
@@ -240,6 +261,9 @@ sub _parse_stopping_points {
     my $mech = WWW::Mechanize->new();
 
     $mech->get($url);
+    unless( $mech->success or $mech->status == 502 ){
+        die "Error fetching $url: " . $mech->status;
+    }
 
     my $tree = HTML::TreeBuilder->new_from_content( $mech->content );
 
@@ -292,7 +316,7 @@ sub get_substops {
             debug("checking $src -> $dst ($i -> $j)");
 
             my $depart  = $stop_details{$src}->{departs};
-            my $url     = "${query_site}/${src}/${dst}/${depart}/${date}";
+            my $url     = "${query_site}/${src}/${dst}/${depart}/${date}$url_suffix";
             my $journey = get_stops($url);
 
             # Sanity check departure and arrival times match up with the source journey