]> git.michaelhowe.org Git - pub/michael/national-rail-ticket-split.git/commitdiff
Commit after Sunday's work, which I sort of forgot
authorMichael Howe <michael@michaelhowe.org>
Mon, 7 Apr 2014 19:21:17 +0000 (20:21 +0100)
committerMichael Howe <michael@michaelhowe.org>
Mon, 7 Apr 2014 19:21:17 +0000 (20:21 +0100)
Hence the less useful commit message.

parse-route.pl [new file with mode: 0644]

diff --git a/parse-route.pl b/parse-route.pl
new file mode 100644 (file)
index 0000000..0995bf3
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use WWW::Mechanize;
+use HTML::TreeBuilder;
+
+my $mech = WWW::Mechanize->new();
+
+my $query_site = "http://traintimes.org.uk";
+
+die "Usage: $0 <source> <dest> <date> <departure time>\n"
+    unless( scalar( @ARGV ) == 4 );
+
+my ( $src, $dest, $date, $depart ) = @ARGV;
+
+print "Finding routes from $src to $dest on $date at $depart\n";
+
+my $url = "${query_site}/${src}/${dest}/${depart}/${date}";
+print "Checking $url\n";
+
+my $stops = get_stops( $url );
+
+sub get_stops {
+    my ( $url ) = @_;
+
+    $mech->get( $url );
+    
+    my $tree = HTML::TreeBuilder->new_from_content( $mech->content );
+
+    my $first_stop = $tree->look_down( '_tag', 'li', 'id', 'result0' );
+    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;
+    print "Ticket type: $ticket_type\n";
+    print "Cost: $ticket_price\n";
+    print "stopping points URL: $details_url\n";
+}