#!/usr/bin/perl
# Reservation watchdog for Student Agency bus lines.
# Automaticka rezervace mist na spojich Student Agency.
# (c) Petr Baudis <pasky@cpan.org> 2006
# Distribute under the same terms as Perl itself. Free software.

use warnings;
use strict;

# Usage: cislo_karty heslo_karty YYYY-MM-DD HH:MM
# Cykli, dokud se neuvolni misto na spoji odjizdejicim v >= HH:MM
# Spoj Jihlava -> Praha, jinak je treba upravit id zastavek nize.

my ($km_login, $km_heslo, $date, $min_dpt) = @ARGV;
$0 = "reserve.pl [$km_login $date $min_dpt]";

use WWW::Mechanize;
my $mech = WWW::Mechanize->new();

my ($lasturl, $lastdpt);

while(1) {

$mech->get( 'http://www.studentagency.cz/mainpage.php?switch=101422' );
my $c = $mech->content;

eval {
$mech->submit_form(
	form_name => 'vyber',
	fields    => {
		f_id_zastavka1 => 4,
		f_id_zastavka2 => 2,
		f_odjezd => $date,
	},
	button    => 'odeslano'
);
}; if ($?) {
	print STDERR "$?";
	sleep 15;
	next;
}

use HTML::TableExtract;
my $te = HTML::TableExtract->new( headers => ['Odjezd', 'Příjezd', '<img alt="Economy" src="express/praha_brno/pictures/economy.jpg" alt="" border="0" height="15" width="15">'], keep_html => 1, strip_html_on_match => 0, slice_columns => 0 );
$te->parse($mech->content);

# Latest is best
foreach my $ts ($te->tables) {
	print "Available connections:\n";
	foreach my $row ($ts->rows) {
		my ($dpt) = ($row->[0] =~ /(\d\d:\d\d)/);
		my ($arr) = ($row->[1] =~ /(\d\d:\d\d)/);
		my ($seats) = ($row->[3] =~ /(\d+)/);
		my $url;
		if ($row->[8]) {
			($url) = ($row->[8] =~ /href="(.*?)"/);
			$url = "http://www.studentagency.cz/$url";
			print "[$dpt] [$arr] $seats free, reg: $url\n";
			$lasturl = $url; $lastdpt = $dpt;
		} else {
			print "[$dpt] [$arr] $seats free, no reg\n";
		}
	}
}

if ($lastdpt ge $min_dpt) {
	print "*** Chosen [$lastdpt] $lasturl\n";
	last;
} else {
	print "--- Passing [$lastdpt] (lt $min_dpt)\n";
}

sleep 90;

}

$mech->get($lasturl);
#print $mech->content;
$mech->submit_form(
	form_number => 3,
	fields    => {
		km_login => $km_login,
		km_heslo => $km_heslo,
	},
);
$mech->get('http://www.studentagency.cz/mainpage.php?switch=101424');

=xx

 1    2         3    4
 5    6         7    8
 9   10        11   12
13   14        15   16
17   18        19   20
21   22        23   24
25   26        27   28
29   30        31   32
33   34
35   36        37   38
39   40        41   42
43   44        45   46
47   48        49   50
51   52        53   54
55   56        57   58
59   60   61   62   63

Middle is best
=cut

#my @dislike = qw(1 2 3 4 59 60 61 62 63);

my @urls; my @seats;

print "Seat list:\n";
foreach my $seat ($mech->find_all_links(text_regex => qr/^\d+$/)) {
	$urls[$seat->text] = $seat->url;
	push @seats, $seat->text;
	print $seat->text." -- ".$seat->url."\n";
}

my $mid = 32;
my $seati; for ($seati = 0; $seati < @seats; $seati++) { last if $seats[$seati] > $mid; }
my $seat;
if ($seati > 0) {
	$seat = $seats[$seati] - $mid <= $mid - $seats[$seati-1] ? $seats[$seati] : $seats[$seati-1];
} else {
	$seat = $seats[0];
}

my $url = 'http://www.studentagency.cz/'.$urls[$seat];
print "Best: $seat -- $url\n";
$mech->get($url);
open MAIL, "|mail -s '"."reserved seat $seat ($urls[$seat])"."' pasky";
print MAIL $mech->content;
close MAIL;
