Scripting SDSU Class Registraion

Alex Egg,

Background

This is little script I wrote to help me register for my classes at school. The problem is that they are all full by the time my reg. time comes up. Now I have to play this game were I’m franticly checking back every 5 min to see if somebody dropped it and there’s a seat available. I then had the epiphany that this would be a lot easier if I could automate this. Thus this script was borne.

The script

https://github.com/eggie5/seatcheck

It uses mechanize to log into my schools web portal and and runs an xpath the page:

Snippet:

agent = Mechanize.new
page = agent.get('http://sdsu.edu/')
form = page.forms.first
form.user_iden=yaml["redid"]
page = agent.submit(form)
page = agent.page.link_with(:text => 'My Registration').click
page = agent.page.link_with(:text => 'Fall 2012').click
page = agent.page.link_with(:text => 'My Wish List').click

courses=[]
[2,3,4].each do |i|
name=page.search("//tr/td/form/table/tr[#{i}]/td[2]/a").inner_text
seats=page.search("//tr/td/form/table/tr[#{i}]/td[10]").inner_text
c=Course.new(name, seats)
courses.push c
end

courses.each do |course|
if course.has_seats?
m="#{Time.now.to_i} - #{course.name} has #{course.seats} seats"
log m
else
log "no seats..."
end
end


If there is a seat available, it will send me and GF a twitter DM, which also emails me. It will also go ahead and register it for me. The reg. form looks like this:

That 4 digit number is plane-text so it’s trivial to submit the form and add the class automatically.

Setting up cron job

More harder than developing the script was setting up the cron job. I added this to my ec2 instance I have running:

*/5 * * * * /usr/local/bin/ruby /home/ubuntu/seatcheck/seatcheck.rb

which says run the seat check every 5 minutes. However, when cron runs it’s in a different env so the path is all diff and doesn’t have ruby. It took me a while to debug that. But this was a handy way to do it:

1. Add * * * * * env > /tmp/env.output to your cron
2. check /tmp/env.output to see what it prints compare to your env command

I wound up putting the full path to ruby to get around the path issue.