Skip to content

Instantly share code, notes, and snippets.

@tynovsky
Last active August 29, 2015 14:00
Show Gist options
  • Save tynovsky/11063900 to your computer and use it in GitHub Desktop.
Save tynovsky/11063900 to your computer and use it in GitHub Desktop.
Student Agency free seat watcher
#!/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 = 'noreply-sa-watcher@example.com';
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;
}
@tynovsky
Copy link
Author

pQuery is leaking, so for long runs, it is necessary to apply this patch: ingydotnet/pquery-pm#6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment