Created
June 23, 2012 21:39
-
-
Save aspender/2980132 to your computer and use it in GitHub Desktop.
Perl script to check if tickets for a specific London 2012 Olympics session are currently available
This file contains hidden or 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 -w | |
| use Getopt::Std; | |
| # Define the address the alerts should come from | |
| $monitor_email = "monitor@monitor.com"; | |
| $monitor_url = "http://www.tickets.london2012.com/browse?form=session&tab=oly&sessionCode="; | |
| # Define global variables | |
| $curl_executable = "/usr/bin/curl"; | |
| $mail_executable = "/usr/sbin/sendmail"; | |
| $OPT_STRING = 'he:'; | |
| # Start program | |
| &main(); | |
| sub main { | |
| # Read in the options | |
| getopts( "$OPT_STRING", \%opt ) or usage(); | |
| # Set variables | |
| my $url_header = ""; | |
| my $url_body = ""; | |
| my $email_address = ""; | |
| my $session_code = $ARGV[0]; | |
| # Determine options | |
| usage() if $opt{h}; | |
| usage() if !$opt{e}; | |
| # Define more variables | |
| $email_address = $opt{e}; | |
| # Monitor URL | |
| my $session_url = $monitor_url . $session_code; | |
| $url_body = `$curl_executable -s "$session_url"`; | |
| if ($url_body =~ /Continue1/) { &send_alert($email_address, $session_url, $session_code);} | |
| } | |
| sub send_alert { | |
| my ($email_address,$url,$session_code) = @_; | |
| # Create timestamp | |
| my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); | |
| $year = $year + 1900; | |
| $mon = $mon + 1; | |
| my $time_stamp = $year."-".$mon."-".$mday." ".$hour.":".$min.":".$sec; | |
| my $email_content = $session_code ." tickets available at ". $time_stamp . "\n" . $url . "\n"; | |
| unless(open (MAIL, "| /usr/sbin/sendmail -t")) { | |
| print "error.\n"; | |
| warn "Error starting sendmail: $!"; | |
| } | |
| else{ | |
| print MAIL "From: " . $monitor_email . "\n"; | |
| print MAIL "To: " . $email_address . "\n"; | |
| print MAIL "Subject: " . $session_code . " AVAILABLE!\n\n"; | |
| print MAIL $email_content; | |
| close(MAIL) || warn "Error closing mail: $!"; | |
| print ("$email_content"); | |
| } | |
| } | |
| sub usage { | |
| print "Usage: ticket_watch.pl -e <email address> <session code>\n\n"; | |
| exit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment