-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.rb
71 lines (58 loc) · 2.01 KB
/
parse.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'net/http'
require 'rexml/document'
require 'rubygems'
# gem install xmlsimple
require 'xmlsimple'
def ParseSemester(year, season)
# for the loops (they are mostly hash iterators) , k stands for key and v stand for value
# Build the URLs
# ex: semester = "2011/spring"
semester = year + "/" + season
base_url = "http://courses.illinois.edu/cis/" + semester
url = base_url + "/schedule/index.xml"
# Grab the response from the URL as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
xml_data = "spring2011.xml"
# Turn the string into a hash of data
catalog = XmlSimple.xml_in(xml_data)
# Iterate through the subjects found in the hash
catalog['subject'].each do |subject|
print "-------\nSubject\n-------\n\n"
# Build a url based off of the current subject code
subjectURL = base_url + "/schedule/" + subject['subjectCode'].to_s + "/index.xml"
# Fetch the courses for the subject and decrypt the data from the url
subjectXML_data = Net::HTTP.get_response(URI.parse(subjectURL)).body
subjectCourses = XmlSimple.xml_in(subjectXML_data)
# Iterate through the courses offered in the class
subjectCourses['subject'][0].each do |k, v|
# Found a course tag, get all the information about the course
if k == "course"
v.each do |course|
print "-------\nCourse START\n-------\n\n"
course.each do |k, v|
if k == "section"
print "-------\nSection\n-------\n\n"
v.each do |section|
section.each do |k, v|
print "\t\t\t<" + k + ">"+ v.to_s + "</"+ k+ ">\n"
end
end
print "-------\nSection END\n-------\n\n"
else
print "\t\t<" + k + ">"+ v.to_s + "</"+ k+ ">\n"
end
end
print "-------\nCourse END\n-------\n\n"
end
end
end
# iterate through the elements of the subject
# -General information about each subject/major
subject.each do |k,v|
print "<" + k + ">"+ v.to_s + "</"+ k+ ">\n"
end
print "\n"
# puts subject['subjectCode']
end
end
ParseSemester( "2011", "spring")