Skip to content

Instantly share code, notes, and snippets.

@sczizzo
Created August 26, 2011 21:08
Show Gist options
  • Save sczizzo/1174448 to your computer and use it in GitHub Desktop.
Save sczizzo/1174448 to your computer and use it in GitHub Desktop.
Classesdev Lookup
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
require 'yaml'
# Course, username, password
# e.g. ./classesdev-lookup.rb AFAM20005 mycnetid 'mysecretpassword'
exit(1) unless ARGV.length == 3
course = ARGV[0]
course_php = 'https://classesdev.uchicago.edu/courseDetail.php?courseName='
course_url = course_php + "#{course}"
course_doc = open( course_url, :http_basic_authentication => [ARGV[1], ARGV[2]] )
document = Nokogiri::HTML course_doc
results = []
document.xpath('//table[@class = "coursetable"]//tr').each do |r|
result = {
:course => r.xpath('td[@class="one"]/span[@class="name"]').text.strip,
:location => r.xpath('td[@class="one"]/span[@class="location"]').text.strip,
:title => r.xpath('td[@class="two"]').text.strip,
:schedule => r.xpath('td[@class="four"]').text.strip,
:enrollment => r.xpath('td[@class="five"]').text.strip
}
unless result[:title].empty?
course = result.delete :course
enrollment = result.delete :enrollment
# Parse enrollment for enrolled and maximum
# TODO encode strings as negative integers
m = /\A(?<enrolled>.*?)\/(?<maximum>.*?)\z/x.match enrollment
result[:enrolled] = m[:enrolled]
result[:maximum] = m[:maximum]
# TODO parse schedule and encode with ice_cube
result[:schedule].gsub! /\s+/, ' '
if m = /\A(?<department>[A-Z]{4})\s*(?<number>\d{5})\s*\/\s*(?<section>\w{2})\z/x.match(course)
# Parse course identifier for department, number, and section
result[:department] = m[:department]
result[:number] = m[:number].to_i
result[:section] = m[:section]
result[:activities] = []
result[:title] = result[:title][/\A(.*?) - Evaluations/i, 1]
results << result
elsif m = /\A(?<activity>.*?)\z/x.match(course)
result[:activity] = m[:activity].strip
result[:title] = result[:title][/\A(.*?)\r\n/, 1].strip
results.last[:activities] << result
end
end
end
puts results.to_yaml.strip
@sczizzo
Copy link
Author

sczizzo commented Aug 26, 2011

$ ./classes-lookup.rb CMSC12100 mycnetid 'mysecretpassword'
--- 
- :location: RY-251
  :title: Computer Science with Applications-1
  :schedule: M W F 10:30AM-11:20AM
  :enrolled: "64"
  :maximum: "69"
  :department: CMSC
  :number: 12100
  :section: "00"
  :activities: 
  - :location: JRL-A01C
    :title: Lab
    :schedule: TU 12:00PM-01:20PM
    :enrolled: "23"
    :maximum: "23"
    :activity: Activity 01
  - :location: JRL-A01C
    :title: Lab
    :schedule: TU 03:00PM-04:20PM
    :enrolled: "22"
    :maximum: "23"
    :activity: Activity 02
  - :location: JRL-A01C
    :title: Lab
    :schedule: TU 04:30PM-05:50PM
    :enrolled: "19"
    :maximum: "23"
    :activity: Activity 03
- :location: RY-251
  :title: Computer Science with Applications-1
  :schedule: M W F 01:30PM-02:20PM
  :enrolled: "17"
  :maximum: "20"
  :department: CMSC
  :number: 12100
  :section: "01"
  :activities: 
  - :location: JRL-A01C
    :title: Lab
    :schedule: TU 07:00PM-08:20PM
    :enrolled: "17"
    :maximum: "20"
    :activity: ""

@sczizzo
Copy link
Author

sczizzo commented Aug 26, 2011

Tip: Leave out the course number when you pass it into the script, and you'll get back results for the whole department.

$ ./classes-lookup.rb CMSC mycnetid 'mysecretpassword'

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