=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
Shows this manpage.
+=item --two
+
+Assumes that two adults are travelling, with a two together railcard.
+
=back
=head1 EXAMPLE USAGE
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);
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' },
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 );
##
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 );
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,
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 );
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