forked from Yukaii/CrawlerMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsu_course_crawler.rb
163 lines (141 loc) · 5.42 KB
/
tsu_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
# 台灣首府大學
# 課程查詢網址:http://erp1.tsu.edu.tw/dwu/
module CourseCrawler::Crawlers
class TsuCourseCrawler < CourseCrawler::Base
# PERIODS = {
# "1" => 1,
# "2" => 2,
# "3" => 3,
# "4" => 4,
# "N" => 5,
# "5" => 6,
# "6" => 7,
# "7" => 8,
# "8" => 9,
# "9" => 10,
# "10" => 11,
# "11" => 12,
# "12" => 13,
# "13" => 14
# }
def initialize year: nil, term: nil, update_progress: nil, after_each: nil
@year = year
@term = term
@update_progress_proc = update_progress
@after_each_proc = after_each
@query_url = 'http://erp1.tsu.edu.tw/dwu/'
end
def courses
@courses = []
course_id = 0
cookie = RestClient.get(@query_url+"choice.jsp").cookies
r = %x(curl -s '#{@query_url}perchk.jsp' -H 'Cookie: JSESSIONID=#{cookie["JSESSIONID"]}' --data 'uid=GUEST&pwd=123123&ls_chochk=N&myway=yes&sys_name=web&myway=yes&sys_name=web' --compressed)
r = %x(curl -s '#{@query_url}ag_pro/ag304_01.jsp' -H 'Cookie: JSESSIONID=#{cookie["JSESSIONID"]}' --compressed)
doc = Nokogiri::HTML(r)
doc.css('select[name="rtxt_untid"] option:nth-child(n+2)').map{|opt| [opt[:value],opt.text]}.each do |dept_v,dept_n|
r = %x(curl -s '#{@query_url}ag_pro/ag304_02.jsp' -H 'Cookie: JSESSIONID=#{cookie["JSESSIONID"]}' --data 'yms_yms=#{@year-1911}%23#{@term}&rtxt_untid=#{dept_v}&unit_serch=%E6%9F%A5+%E8%A9%A2' --compressed)
doc = Nokogiri::HTML(r)
next if doc.css('table td[width="50%"]').count == 0 # 沒有課程就略過
doc.css('table td[width="50%"]').map{|td| td[:onclick].split("'")[1]}.each do |cla_url|
# 進入課表
r = %x(curl -s '#{@query_url}ag_pro/#{cla_url}' -H 'Cookie: JSESSIONID=#{cookie["JSESSIONID"]}' --compressed)
doc = Nokogiri::HTML(r)
courses_data_temp = course_time_table(doc.css('table:nth-child(5) tr:nth-child(n+2) td:nth-child(n+2)').map{|table| table.text})
courses_data_temp.each do |course_code,data|
# puts "#{course_code},#{@query_url}ag_pro/#{cla_url}"
course_id += 1
course_days, course_periods, course_locations = [], [], []
data['time'].each do |day, period, loc|
course_days << day
course_periods << period
course_locations << loc
end
course = {
year: @year, # 西元年
term: @term, # 學期 (第一學期=1,第二學期=2)
name: data['name'], # 課程名稱
lecturer: data['teacher'], # 授課教師
credits: data['credits'], # 學分數
code: "#{@year}-#{@term}-#{course_id}_#{course_code}",
general_code: course_code, # 選課代碼
url: nil, # 課程大綱之類的連結
required: nil, #course_type.include?('必'), # 必修或選修
department: dept_n, # 開課系所
# department_code: dept_v,
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
# 分析課表
def course_time_table table_data
day = 0
period = 1
courses_data_temp = {}
table_data.each do |block|
if day < 7
day += 1
else
day = 1
period += 1
period = 6 if period == 5
period = 5 if period == 15
end
next if block == " "
course_temp = block.split("選課代碼 - ")
course_temp.each do |cor|
next if cor == ""
cor.scan(/(\d\d\d\d)(.*?) (.+)/).each do |course_code,name,loc|
if courses_data_temp[course_code] == nil
if name.scan(/([\W\w\(\)]+\d\d)(\W+)/)[0] != nil
c_name = name.scan(/([\W\w\(\)]+\d\d)(\W+)/)[0][0]
t_name = name.scan(/([\W\w\(\)]+\d\d)(\W+)/)[0][1]
else
c_name = name
t_name = nil
end
courses_data_temp[course_code] = {}
courses_data_temp[course_code]['time'] = []
courses_data_temp[course_code]['teacher'] = t_name
courses_data_temp[course_code]['name'] = c_name
courses_data_temp[course_code]['credits'] = 0
end
courses_data_temp[course_code]['time'] << [day,period,loc]
courses_data_temp[course_code]['credits'] += 1
end
end
end
courses_data_temp
end
end
end