forked from Yukaii/CrawlerMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqu_course_crawler.rb
141 lines (120 loc) · 4.65 KB
/
nqu_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
##
# 金門課程爬蟲
# http://select1.nqu.edu.tw/kmkuas/index_main.html
#
module CourseCrawler::Crawlers
class NquCourseCrawler < CourseCrawler::Base
DAYS = {
"一" => 1,
"二" => 2,
"三" => 3,
"四" => 4,
"五" => 5,
"六" => 6,
"日" => 7,
}
def initialize year: nil, term: nil, update_progress: nil, after_each: nil
@year = year || current_year
@term = term || current_term
@update_progress_proc = update_progress
@after_each_proc = after_each
@query_url = 'http://select2.nqu.edu.tw/kmkuas/perchk.jsp'
@query_url2 = "http://select2.nqu.edu.tw/kmkuas/ag_pro/ag304_02.jsp"
@query_url3 = "http://select2.nqu.edu.tw/kmkuas/ag_pro/ag304_03.jsp"
end
def courses
@courses = []
http_client.receive_timeout = 300
r = http_client.post(@query_url, {
"uid" => "guest",
"pwd" => "123",
})
@query_url = "http://select2.nqu.edu.tw/kmkuas/ag_pro/ag304_01.jsp"
r = http_client.get(@query_url)
doc = Nokogiri::HTML(r.body)
dep = Hash[doc.css('select[name="unit_id"] option').map{|opt| [opt[:value],opt.text]}]
dep.each do |dep_c, dep_n|
r = http_client.post(@query_url2, {
"yms_year" => @year - 1911,
"yms_sms" => @term,
"unit_id" => dep_c,
"unit_serch" => "%E6%9F%A5+%E8%A9%A2",
})
doc = Nokogiri::HTML(r.body)
degree = Hash[doc.css('table tr:not(:first-child) td[style="font-size: 9pt;color:blue;"] div').map{|td| [td[:onclick].split('\'')[1], td.text]}]
degree.each do |degree_c, degree_n|
r = http_client.post(@query_url3, {
"arg01" => @year - 1911,
"arg02" => @term,
"arg" => degree_c,
})
doc = Nokogiri::HTML(r.body)
next if doc.css('table')[0] == nil
doc.css('table')[0].css('tr:not(:first-child)').map{|tr| tr}.each do |tr|
data = tr.css('td').map{|td| td.text}
time_period_regex = /\((?<day>[一二三四五六日])\)(?<period>(\d-?\d?,?)+)/
course_time = Hash[ data[7].scan(time_period_regex) ]
course_days, course_periods, course_locations = [], [], []
course_time.each do |k, v|
v.split(',').each do |period_temp|
(period_temp.split('-')[0].to_i..period_temp.split('-')[-1].to_i).each do |period|
period += 9 if degree_n.include?('進') && k != "六" && k != "日"
course_days << DAYS[k]
course_periods << period
course_locations << data[9]
end
end
end
course = {
year: @year, # 西元年
term: @term, # 學期 (第一學期=1,第二學期=2)
name: data[1], # 課程名稱
lecturer: data[8], # 授課教師
credits: data[3].to_i, # 學分數
code: "#{@year}-#{@term}-#{dep_c}-#{data[0].scan(/\w+/)[0]}",
general_code: data[0].scan(/\w+/)[0],
# general_code: data[0], # 選課代碼
required: data[5].include?('必'), # 必修或選修
department: dep_n, # 開課系所
# department_code: dep_c,
# note: data[10],
# group: data[2],
# hours: data[4],
# department_term: data[6],
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],
}
@after_each_proc.call(course: course) if @after_each_proc
@courses << course
end
end
end
@courses
end
end
end