forked from Yukaii/CrawlerMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kuas_course_crawler.rb
196 lines (175 loc) · 7.13 KB
/
kuas_course_crawler.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
##
# 高應大課程爬蟲
# 課程查詢:http://140.127.113.224/kuas/
#
module CourseCrawler::Crawlers
class KuasCourseCrawler < CourseCrawler::Base
DAYS = {
"一" => 1,
"二" => 2,
"三" => 3,
"四" => 4,
"五" => 5,
"六" => 6,
"日" => 7,
}
# PERIODS = {
# "M" => 1,
# "1" => 2,
# "2" => 3,
# "3" => 4,
# "4" => 5,
# "A" => 6,
# "5" => 7,
# "6" => 8,
# "7" => 9,
# "8" => 10,
# "B" => 11,
# "11" => 12,
# "12" => 13,
# "13" => 14,
# "14" => 15,
# }
PERIODS = CoursePeriod.find('KUAS').code_map
def initialize year: nil, term: nil, update_progress: nil, after_each: nil, params: nil
@year = year || current_year
@term = term || current_term
@update_progress_proc = update_progress
@after_each_proc = after_each
@query_url = "http://140.127.113.224/kuas/"
end
def courses detail: false
@courses = []
@threads = []
puts "get url ...."
r = RestClient.post(@query_url + "perchk.jsp", {"uid" => "guest", "pwd" => "123"})
cookies = r.cookies
r = RestClient.get(@query_url + "f_index.html", cookies: cookies)
doc = Nokogiri::HTML(r)
hidden = Hash[doc.css('input[type="hidden"]').map{|hidden| [hidden[:name], hidden[:value]]}]
r = RestClient.post(@query_url + "fnc.jsp", hidden.merge({"fncid" => "AG304"}), cookies: cookies )
doc = Nokogiri::HTML(r)
hidden = Hash[doc.css('input[type="hidden"]').map{|hidden| [hidden[:name], hidden[:value]]}]
r = RestClient.post(@query_url + "ag_pro/ag304.jsp", hidden.merge({}), cookies: cookies )
r = RestClient.get(@query_url + "ag_pro/ag304_01.jsp", cookies: cookies )
doc = Nokogiri::HTML(r)
doc.css('select[name="dgr_id"] option').map{|opt| [opt[:value], opt.text]}.each do |dgr_c, dgr_n|
r = RestClient.post(@query_url + "ag_pro/ag304_02.jsp", {
"yms_yms" => "#{@year - 1911}##{@term}",
"dgr_id" => dgr_c,
"unit_serch" => "查 詢",
# "ls_dvs_id" => "",
"ls_yn" => "N",
}, cookies: cookies )
doc = Nokogiri::HTML(r)
puts "Department : " + dgr_n
doc.css('table tr:nth-child(n+2) div').map{|td| [td[:onclick].scan(/\'(\w+)/)[0][0], td.text]}.each do |dep_c, dep_n|
r = RestClient.post(@query_url + "ag_pro/ag304_03.jsp", {
"arg01" => @year - 1911,
"arg02" => @term,
"arg" => dep_c,
}, cookies: cookies )
doc = Nokogiri::HTML(r)
doc.css('table[border="1"]:nth-child(2) tr:nth-child(n+2)').map{|tr| tr}.each do |tr|
data = tr.css('td:not(:last-child)').map{|td| td.text}
#scan那邊有問題,先註解
#syllabus_url = "http://140.127.113.224/kuas/ag_pro/ag451.jsp?year=#{@year - 1911}&sms=#{@term}&dvs=all&dgr=all&unt=all&cls=#{dep_c}&sub=#{tr.css('td:last-child').map{|a| a[:onclick]}[0].scan(/\,(\w+)\./)[0][0]}&empidno=all"
syllabus_url = "url : error"
time_period_regex = /(?<day>[一二三四五六日])\)(?<period>(\w+)\-?\,?(\w+)?\,?\-?(\w+)?\-?(\w+)?)/
course_time_location = data[7].scan(time_period_regex).map{|day, period| [day, period]}
course_days, course_periods, course_locations = [], [], []
p1, p2, p3, p4 = nil, nil, nil, nil
course_time_location.each do |day, period|
if period.include?(',')
if period.split(',')[0].include?('-')
p1, p2 = period.scan(/\w+/)[0], period.scan(/\w+/)[1]
else
p1 = p2 = period.scan(/\w+/)[0]
end
if period.split(',')[1].include?('-')
p3, p4 = period.scan(/\w+/)[-2], period.scan(/\w+/)[-1]
else
p3 = p4 = period.scan(/\w+/)[-1]
end
else
if period.include?('-')
p1, p2 = period.scan(/\w+/)[0], period.scan(/\w+/)[1]
else
p1 = p2 = period.scan(/\w+/)[0]
end
end
if p1 != nil
(PERIODS[p1]..PERIODS[p2]).each do |p|
course_days << DAYS[day]
course_periods << p
course_locations << data[9]
end
end
if p3 != nil
(PERIODS[p3]..PERIODS[p4]).each do |p|
course_days << DAYS[day]
course_periods << p
course_locations << power_strip(data[9])
end
end
end
general_code = data[0] && power_strip(data[0]);
next if general_code.nil?
course = {
:year => @year, # 西元年
:term => @term, # 學期 (第一學期=1,第二學期=2)
:name => power_strip(data[1]), # 課程名稱
:lecturer => power_strip(data[8]), # 授課教師
:credits => data[3][0].to_i, # 學分數
:code => "#{@year}-#{@term}-#{dep_c}-#{general_code}",
# :general_code => data[0], # 選課代碼
:general_code => general_code,
:url => syllabus_url, # 課程大綱之類的連結(如果有的話)
:required => data[5].include?('必'), # 必修或選修
:department => dep_n, # 開課系所
:department_code => dep_c,
:day_1 => course_days[0],
:day_2 => course_days[1],
:day_3 => course_days[2],
:day_4 => course_days[3],
:day_5 => course_days[4],
:day_6 => course_days[5],
:day_7 => course_days[6],
:day_8 => course_days[7],
:day_9 => course_days[8],
:period_1 => course_periods[0],
:period_2 => course_periods[1],
:period_3 => course_periods[2],
:period_4 => course_periods[3],
:period_5 => course_periods[4],
:period_6 => course_periods[5],
:period_7 => course_periods[6],
:period_8 => course_periods[7],
:period_9 => course_periods[8],
:location_1 => course_locations[0],
:location_2 => course_locations[1],
:location_3 => course_locations[2],
:location_4 => course_locations[3],
:location_5 => course_locations[4],
:location_6 => course_locations[5],
:location_7 => course_locations[6],
:location_8 => course_locations[7],
:location_9 => course_locations[8],
}
sleep(1) until (
@threads.delete_if { |t| !t.status }; # remove dead (ended) threads
@threads.count < ( (ENV['MAX_THREADS'] && ENV['MAX_THREADS'].to_i) || 30)
)
@threads << Thread.new do
@after_each_proc.call(course: course) if @after_each_proc
end
@courses << course
end
end
end
ThreadsWait.all_waits(*@threads)
puts "Project finished !!!"
@courses
end
end
end