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:

My Unicorn

Snippet:

agent = Mechanize.new
page = agent.get('http://sdsu.edu/')
page = agent.page.link_with(:text => 'WebPortal').click
form = page.forms.first
form.user_iden=yaml["redid"]
form.password=yaml["pass"]
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
    Twitter.direct_message_create('eggie5', m)
    Twitter.direct_message_create('withlovecassee', 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:

reg form

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.


Permalink: scripting-sdsu-class-registration

Tags: programming, mechanize

Last edited by Alex Egg, 2012-08-01 19:20:23
View Revision History