2006-12-27

Perl helped me sign up for my physics class

I used the following perl script to check the page that showed the status of my Physics 1A discussion. If it finds certain words on the page indicating that any discussion has an open lecture or waitlist, it will e-mail whatever e-mail address it is given. I set the e-mail address to my cell phone's so I would receive a text message whenever there was an opening.

The script is currently fairly primitive in the sense that it checks every discussion and lecture for an opening in either the waitlist or the class itself. The script could fairly easily be modified for a different or more specific use.

I used a cron job to run the script on my web server every two minutes. One annoyance that this created was that until the opening was filled in the course I would continue receiving e-mails to my phone every two minutes. I had to turn off my phone during my Computer Science final because of that. Tonight the script finally proved itself useful after I successfully signed up for the physics course I needed.

#!/usr/bin/perl

#########################
# CHANGE THESE VARIABLES
#########################
$url = 'http://www.registrar.ucla.edu/schedule/detselect.asp?termsel=07W&subareasel=PHYSICS&idxcrs=0001A+++';
$email = "youremail@ucla.edu";
$subject = "Physics Opened";
$body = "The Physics 1A waitlist opened so go sign up.";
#########################

# Prints the page header
print "Content-Type: text/html; charset=utf-8\n\n";

# Grabs the page content
use LWP::Simple;
$content = get $url;
die "Couldn't get $url" unless defined $content;

# Defines the words to look for on the page
$str_open = '<span style="color: rgb(0, 57, 0);">Open</span>';
$str_wait = '<span style="color: rgb(0, 0, 0);">W-List</span>';

# Checks to see if either word was found on the page and attempts to send the e-mail if the words were found
if($content =~ m/$str_open|$str_wait/i) {
unless(open (MAIL, "|/usr/sbin/sendmail -t")) {
print "error.\n";
warn "Error starting sendmail: $!";
}
else {
print MAIL "From: $email\n";
print MAIL "To: $email\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$body\n\n";
close(MAIL) || warn "Error closing mail: $!";
print "Mail sent.\n";
}
}


(Insert Michael's comment about UCLA vs UCSD hehe)

Labels:


Comments:
Nice job. I needs to learn me some perl. And some python, too, while I'm at it.
 
glad it finally paid off.
 
Thats why UCSD sucks. To sign up for classes we just hit a button and everything is automated. To train good engineers you have to make every step of their journey difficult and if they fail taser them ^^
 
Post a Comment





<< Home

Subscribe to Posts [Atom]