-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.rb
269 lines (235 loc) · 6.46 KB
/
request.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# build search url from search parameters
def build_url(params)
course = params[:course]
# parse standard search
if params[:course]
dept, num = '', ''
args = params[:course].split().collect() {|term| term.strip}
if args.length > 1
if args.last =~ /\d/
dept = args[0...-1].join(' ')
num = args.last
else
dept = args.join(' ')
end
elsif args.length == 1
index = args.first =~ /\d/
isnum = true if Float(args.first) rescue false
if index == 0 and args.first.length >= 4 and isnum
params[:ccn] = "%05d" % Integer(Float(args.first))
elsif index
if index == 1
num = args.first
else
dept = args.first[0...index]
num = args.first[index..-1]
end
else
dept = args.first
end
end
params[:dept] = dept
params[:course_num] = num
end
# params[:dept].gsub! /\s/, '+'
term = 'p_term=' + params.fetch(:semester, '').strip
deptname= 'p_deptname=' + 'Choose+a+Department+Name+--'
classif = 'p_classif=' + '--+Choose+a+Course+Classification+--'
presuf = 'p_presuf=' + '--+Choose+a+Course+Prefix%2fSuffix+--'
day = 'p_day=' + params.fetch(:days, '').strip
fields = [term, deptname, classif, presuf, day]
url = 'https://osoc.berkeley.edu/OSOC/osoc?' + fields.join('&')
puts url
# dept = params[:dept].strip().upcase()
# num = params[:course_num].strip().upcase()
# if dept == '' and num == ''
# course = 'RESULTS'
# else
# course = (dept + " " + num).strip()
# end
return url, course
end
# fetch html results from search
def get_search_html(url)
# fetch html for base page
doc = open(url).read()
# fetch next result pages
row = 101
nextdoc = doc
while nextdoc.include?('see next results')
nextdoc = open(url + '&p_start_row=' + row.to_s()).read()
doc += nextdoc
row += 100
end
return doc
end
# split html lines into sections
def group_lines(doc, partition_string)
partitions = []
group = []
doc.each_line do |line|
if line.include?(partition_string)
if not group.empty?
partitions << group
group = []
end
group << line
else
if not group.empty?
group << line
end
end
end
partitions << group
return partitions
end
# fetch live enrollment for a section
def schedule(ccn, section_info)
numbers = []
nums = []
codes = {'FL' => '12D2', 'SP' => '13B4', 'SU' => '13C1'}
base = 'https://telebears.berkeley.edu/enrollment-osoc/osc?_InField1=RESTRIC'
ccn = '&_InField2=' + ccn
sem = '&_InField3=' + codes[params[:semester]]
url = base + ccn + sem
doc = open(url).read()
doc.each_line do |line|
if line.include?('limit')
a = line.scan(Regexp.new(/([0-9]+)/))
nums += a[0] + a[1]
end
end
nums += ['0']*4
enrolled, limit, wait_list, wait_limit = nums.collect {|x| x.strip}
section_info[:enrolled] = enrolled + '/' + limit
section_info[:waitlist] = wait_list + '/' + wait_limit
section_info[:open] = Integer(enrolled) < Integer(limit)
section_info[:url] = url
if doc.include?('Error')
section_info[:enrolled] = 'see link'
section_info[:waitlist] = 'N/A'
section_info[:open] = true
end
end
# execute search
def live_data(params)
require 'thread/pool'
require 'open-uri'
url, course = build_url(params)
html = get_search_html(url)
partition_string = '<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0>'
html_sections = group_lines(html, partition_string)
# parse each section for relevant information
sections = []
info = {}
ccn_regex = Regexp.new(/name="_InField2" value="([0-9]*)"/)
sem_regex = Regexp.new(/name="_InField3" value="([0-9A-Z]*)/)
html_sections.each do |section_lines|
d = []
lookup_ccn = -1
section_lines.each do |line|
if line.include?(': ')
raw = line.scan(Regexp.new(/>([^<]+)/))
d << (raw[1][0] + ' ').split('&#')[0].split(' ')[0].split.join(' ')
end
match = line.match(ccn_regex)
if match
lookup_ccn = match.captures[0]
end
end
if lookup_ccn == -1
next
end
sec = Hash.new
name = d[0]
sec = { course: name, title: d[1], location: d[2],
instructor: d[3], status: d[4], ccn: d[5], units: d[6],
final: d[7], restrictions:d[8], note: d[9],
enrollment: d[10], lookup_ccn: lookup_ccn }
if sec[:location].include? 'UNSCHED'
time = 'UNSCHED'
place = sec[:location].split('UNSCHED')[1].strip
else
time_place = sec[:location].split(',')*2
time = time_place[0]
place = time_place[1].split('(')[0].strip
end
if sec[:ccn].include?('SEE NOTE') or sec[:ccn].strip == '' or sec[:ccn].include?('SEE DEPT')
sec[:ccn] = "%05d" % lookup_ccn
end
sec[:time] = time
sec[:place] = place
info[name] = sec
sections << name
end
if sections.length > 60
num_threads = 60
elsif sections.length > 30
num_threads = 30
else
num_threads = 15
end
# fetch live data for each section
pool = Thread::Pool.new(num_threads)
stats = Hash.new
sections.each do |section|
lookup_ccn = info[section][:lookup_ccn]
pool.process {
schedule(lookup_ccn, info[section])
}
end
pool.shutdown
# group by lecture
lectures = []
lec = []
last_lec = ''
lec_was_last = false
sections.reverse.each do |name|
if name.include?(' S ')
if lec_was_last
lectures << lec
lec = []
end
lec.unshift(name)
lec_was_last = false
next
end
title = name.split[0...-3].join(' ')
if not lec_was_last
lec.unshift(name)
lec_was_last = true
last_lec = title
next
end
if title == last_lec and lec.length > 1
lec.unshift(name)
else
lectures << lec
lec = [name]
end
lec_was_last = true
last_lec = title
end
if lec.length > 0
lectures << lec
end
lectures.reverse!
return lectures, info, url, course, params[:semester]
end
maps = [
{:days => "M", :semester => "SP"},
{:days => "Tu", :semester => "SP"},
{:days => "W", :semester => "SP"},
{:days => "Th", :semester => "SP"},
{:days => "F", :semester => "SP"},
{:days => "MW", :semester => "SP"},
{:days => "WF", :semester => "SP"},
{:days => "MF", :semester => "SP"},
{:days => "TuTh", :semester => "SP"},
{:days => "MWF", :semester => "SP"},
{:days => "MTWTF", :semester => "SP"}
]
maps.each { |map|
@lectures, @info, @url, @course, @semester = live_data(map)
puts @lectures
}