Created
February 13, 2011 13:38
-
-
Save shawntan/824692 to your computer and use it in GitHub Desktop.
Scrapes module info from CORS and converts it into iCal. Not working right
This file contains 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/ruby | |
# | |
require 'mechanize' | |
require 'icalendar' | |
include Icalendar | |
def create_datetime(day_str,time_str) | |
if(time_str.length<4) | |
time_str = "0"+time_str | |
end | |
return DateTime.strptime("#{day_str} #{time_str}","%A %H%M") | |
end | |
def create_exam(cal,module_name,date_str) | |
exam_date = Date.strptime(date_str,"%d-%m-%Y") | |
event = Event.new | |
event.start = exam_date | |
# event.end = exam_date.next_day | |
event.summary = "#{module_name} Exam" | |
cal.add_event(event) | |
return exam_date | |
end | |
def create_event(cal,module_name,slot_name,arr,end_date) | |
day = arr[0] | |
start_time = arr[1] | |
end_time = arr[2] | |
venue = arr[3] | |
repeats = arr[4] | |
event = Event.new | |
event.start=create_datetime(day,start_time) | |
event.end=create_datetime(day,end_time) | |
event.location = venue | |
event.recurrence_rules = ["FREQ=WEEKLY;INTERVAL=1"] | |
event.tzid="Asia/Singapore" | |
if(end_date) | |
event.recurrence_rules[0] = "#{event.recurrence_rules[0]};UNTIL=#{end_date.to_ical}" | |
end | |
event.summary="#{module_name} #{slot_name}" | |
cal.add_event(event) | |
end | |
exam_xpath = "//div[@id='wrapper']/table[1]/tr[2]/td[1]/table[1]/tr[3]/td[1]/table[1]/tr[6]/td[2]" | |
lecture_xpath = "//div[@id='wrapper']/table[1]/tr[2]/td[1]/table[1]/tr[3]/td[1]/table[3]/tr[2]/td[1]/div[1]/table[1]/tr[1]/td[1]" | |
tutorial_xpath = "//div[@id='wrapper']/table[1]/tr[2]/td[1]/table[1]/tr[3]/td[1]/table[4]/tr[3]/td[1]/div[1]/table/tr[1]/td[1]" | |
agent = Mechanize.new | |
cal = Calendar.new | |
cal.timezone do | |
timezone_id "Asia/Singapore" | |
standard do | |
timezone_offset_from "+0800" | |
timezone_offset_to "+0800" | |
timezone_name "SGT" | |
dtstart "19700101T000000" | |
end | |
end | |
ARGV.each do|module_name| | |
url = "https://aces01.nus.edu.sg/cors/jsp/report/ModuleDetailedInfo.jsp?acad_y=2010/2011&sem_c=2&mod_c=#{module_name}" | |
puts "#Downloading..." | |
page = agent.get(url) | |
puts "#DONE" | |
exam_element = page.search(exam_xpath) | |
if(exam_element[0]) | |
exam_str = exam_element[0].to_str | |
exam_date = nil | |
if(/\s*(\d{2}-\d{2}-\d{4})\s+(AM|PM)/.match(exam_str)) | |
exam_date = create_exam(cal,module_name,$1) | |
end_date = exam_date.prev_day(exam_date.wday).prev_day(7) | |
end | |
end | |
lecture_elements = page.search(lecture_xpath) | |
lecture_elements.each do |e| | |
schedule = e.to_str | |
if (/(.*)[\n\r](.*)/.match(schedule)) | |
slot_name = $1 | |
schedule_string = $2 | |
matches = schedule_string.scan(/\s*(\w+) From (\d+) hrs to (\d+) hrs in (\w+), Week\(s\): ([\s\w\d]*)\./) | |
matches.each do |arr| | |
create_event(cal,module_name,slot_name,arr,end_date) | |
end | |
end | |
end | |
tutorial_elements = page.search(tutorial_xpath) | |
tutorial_elements.each do |e| | |
schedule = e.to_str | |
if (/(.*)[\n\r](.*)/.match(schedule)) | |
# puts $1 | |
slot_name = $1 | |
schedule_string = $2 | |
matches = schedule_string.scan(/(\w+) From (\d+) hrs to (\d+) hrs in (.*), Week\(s\): ([\s\w\d]*)\./) | |
arr = matches[0]; | |
if(arr) | |
create_event(cal,module_name,slot_name,arr,end_date) | |
end | |
end | |
end | |
end | |
puts cal.to_ical |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment