Last active
August 29, 2015 14:00
-
-
Save tynovsky/11063900 to your computer and use it in GitHub Desktop.
Student Agency free seat watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use WWW::Mechanize; | |
use Getopt::Long; | |
use pQuery; | |
use Data::Dumper; | |
use Mail::Sendmail; | |
GetOptions( | |
'from=s' => \my $from, | |
'to=s' => \my $to, | |
'date=s' => \my $date, | |
'time=s@' => \my @times, | |
'mail=s' => \my $to_mail, | |
) or die 'Wrong opts'; | |
die 'Arguments are all mandatory' | |
unless $from && $to && $date && @times && $to_mail; | |
warn "From: $from, To: $to, Date: $date\n"; | |
warn "Time: $_\n" for @times; | |
my $from_mail = '[email protected]'; | |
my $url = 'https://jizdenky.studentagency.cz/m/Booking' | |
. '/from/' . uc($from) | |
. '/to/' . uc($to) | |
. '/departure/' . $date | |
. '/retdep/' . $date | |
. '/return/false/credit/false'; | |
warn "$url\n"; | |
my $mech = WWW::Mechanize->new(stack_depth => 0); | |
while (1) { | |
$mech->get( $url ); | |
my $msg; | |
pQuery( $mech->content )->find('.line-link')->each(sub { | |
my $line = pQuery($_); | |
my $time = $line->find('.departure')->text; | |
my $free = $line->find('.free' )->text; | |
$msg .= "\n$time: $free" if $free && grep {$_ eq $time} @times); | |
}); | |
if ($msg) { | |
sendmail( To => $to_mail, From => $from_mail, | |
Subject => 'free seat', Message => $url . $msg); | |
warn $Mail::Sendmail::log; | |
exit | |
} | |
sleep 20; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pQuery is leaking, so for long runs, it is necessary to apply this patch: ingydotnet/pquery-pm#6