-
Notifications
You must be signed in to change notification settings - Fork 3
/
room.go
313 lines (292 loc) · 9.39 KB
/
room.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package jwch
import (
"strings"
"github.com/antchfx/htmlquery"
"golang.org/x/net/html"
"github.com/west2-online/jwch/constants"
)
func (s *Student) GetEmptyRoom(req EmptyRoomReq) ([]string, error) {
viewStateMap, err := s.getState(constants.ClassroomQueryURL)
if err != nil {
return nil, err
}
roomTypes, emptyRoomState, err := s.getEmptyRoomTypes(viewStateMap, "", req)
if err != nil {
return nil, err
}
// 按照教室类型进行并发访问
channels := make([]chan struct {
res []string
err error
}, len(roomTypes))
var rooms []string
for i, t := range roomTypes {
channels[i] = make(chan struct {
res []string
err error
})
go func(t string, ch chan struct {
res []string
err error
},
) {
res, err := s.PostWithIdentifier(constants.ClassroomQueryURL,
map[string]string{
"__VIEWSTATE": emptyRoomState["VIEWSTATE"],
"__EVENTVALIDATION": emptyRoomState["EVENTVALIDATION"],
"ctl00$TB_rq": req.Time,
"ctl00$qsjdpl": req.Start,
"ctl00$zzjdpl": req.End,
"ctl00$jslxdpl": t,
"ctl00$xqdpl": req.Campus,
"ctl00$xz1": ">=",
"ctl00$jsrldpl": "0",
"ctl00$xz2": ">=",
"ctl00$ksrldpl": "0",
"ctl00$ContentPlaceHolder1$BT_search": "查询",
})
if err != nil {
ch <- struct {
res []string
err error
}{res: nil, err: err}
return
}
rooms, err := parseEmptyRoom(res)
if err != nil {
ch <- struct {
res []string
err error
}{res: nil, err: err}
return
}
ch <- struct {
res []string
err error
}{res: rooms, err: nil}
}(t, channels[i])
}
for _, ch := range channels {
temp := <-ch
if temp.err != nil {
return nil, temp.err
}
rooms = append(rooms, temp.res...)
}
return rooms, err
}
func (s *Student) GetQiShanEmptyRoom(req EmptyRoomReq) ([]string, error) {
viewStateMap, err := s.getState(constants.ClassroomQueryURL)
if err != nil {
return nil, err
}
var rooms []string
// 这里按照building的顺序进行并发爬取
// 创建channel数组
channels := make([]chan struct {
res []string
err error
}, len(constants.BuildingArray))
for i, building := range constants.BuildingArray {
channels[i] = make(chan struct {
res []string
err error
})
go func(index int, building string, ch chan struct {
res []string
err error
},
) {
roomTypes, emptyRoomState, err := s.getEmptyRoomTypes(viewStateMap, building, req)
if err != nil {
ans := struct {
res []string
err error
}{res: nil, err: err}
ch <- ans
}
var rooms []string
for _, t := range roomTypes {
res, err := s.PostWithIdentifier(constants.ClassroomQueryURL,
map[string]string{
"__VIEWSTATE": emptyRoomState["VIEWSTATE"],
"__EVENTVALIDATION": emptyRoomState["EVENTVALIDATION"],
"ctl00$TB_rq": req.Time,
"ctl00$qsjdpl": req.Start,
"ctl00$zzjdpl": req.End,
"ctl00$jxldpl": building,
"ctl00$jslxdpl": t,
"ctl00$xqdpl": req.Campus,
"ctl00$xz1": ">=",
"ctl00$jsrldpl": "0",
"ctl00$xz2": ">=",
"ctl00$ksrldpl": "0",
"ctl00$ContentPlaceHolder1$BT_search": "查询",
})
if err != nil {
ch <- struct {
res []string
err error
}{res: nil, err: err}
return
}
roomList, err := parseEmptyRoom(res)
if err != nil {
ch <- struct {
res []string
err error
}{res: roomList, err: err}
return
}
rooms = append(rooms, roomList...)
}
ch <- struct {
res []string
err error
}{res: rooms, err: nil}
}(i, building, channels[i])
}
// 按顺序合并结果
for _, ch := range channels {
temp := <-ch
if temp.err != nil {
return nil, temp.err
}
rooms = append(rooms, temp.res...)
}
return rooms, nil
}
// 获取VIEWSTATE和EVENTVALIDATION
// 抽象成一个函数, 因为基本上每个请求都需要这两个参数
func (s *Student) getState(url string) (map[string]string, error) {
resp, err := s.GetWithIdentifier(url)
if err != nil {
return nil, err
}
viewState := htmlquery.SelectAttr(htmlquery.FindOne(resp, `//*[@id="__VIEWSTATE"]`), "value")
eventValidation := htmlquery.SelectAttr(htmlquery.FindOne(resp, `//*[@id="__EVENTVALIDATION"]`), "value")
return map[string]string{
"VIEWSTATE": viewState,
"EVENTVALIDATION": eventValidation,
}, nil
}
// 获取教室类型
func (s *Student) getEmptyRoomTypes(viewStateMap map[string]string, building string, req EmptyRoomReq) ([]string, map[string]string, error) {
var res *html.Node
if building != "" {
res, _ = s.PostWithIdentifier(constants.ClassroomQueryURL, map[string]string{
"__VIEWSTATE": viewStateMap["VIEWSTATE"],
"__EVENTVALIDATION": viewStateMap["EVENTVALIDATION"],
"ctl00$TB_rq": req.Time,
"ctl00$qsjdpl": req.Start,
"ctl00$zzjdpl": req.End,
"ctl00$jxldpl": building,
"ctl00$xqdpl": req.Campus,
"ctl00$xz1": ">=",
"ctl00$jsrldpl": "0",
"ctl00$xz2": ">=",
"ctl00$ksrldpl": "0",
"ctl00$ContentPlaceHolder1$BT_search": "查询",
})
} else {
res, _ = s.PostWithIdentifier(constants.ClassroomQueryURL, map[string]string{
"__VIEWSTATE": viewStateMap["VIEWSTATE"],
"__EVENTVALIDATION": viewStateMap["EVENTVALIDATION"],
"ctl00$TB_rq": req.Time,
"ctl00$qsjdpl": req.Start,
"ctl00$zzjdpl": req.End,
"ctl00$xqdpl": req.Campus,
"ctl00$xz1": ">=",
"ctl00$jsrldpl": "0",
"ctl00$xz2": ">=",
"ctl00$ksrldpl": "0",
"ctl00$ContentPlaceHolder1$BT_search": "查询",
})
}
sel := htmlquery.Find(res, "//*[@id='jslxdpl']//option")
var types []string
for _, opt := range sel {
types = append(types, htmlquery.InnerText(opt))
}
viewState := htmlquery.SelectAttr(htmlquery.FindOne(res, `//*[@id="__VIEWSTATE"]`), "value")
eventValidation := htmlquery.SelectAttr(htmlquery.FindOne(res, `//*[@id="__EVENTVALIDATION"]`), "value")
return types, map[string]string{
"VIEWSTATE": viewState,
"EVENTVALIDATION": eventValidation,
}, nil
}
func parseEmptyRoom(doc *html.Node) ([]string, error) {
sel := htmlquery.Find(doc, "//*[@id='jsdpl']//option")
var res []string
for _, opt := range sel {
res = append(res, htmlquery.InnerText(opt))
}
return res, nil
}
// 考场查询
func (s *Student) GetExamRoom(req ExamRoomReq) ([]*ExamRoomInfo, error) {
viewStateMap, err := s.getState(constants.ExamRoomQueryURL)
if err != nil {
return nil, err
}
res, err := s.PostWithIdentifier(constants.ExamRoomQueryURL, map[string]string{
"__VIEWSTATE": viewStateMap["VIEWSTATE"],
"__EVENTVALIDATION": viewStateMap["EVENTVALIDATION"],
"ctl00$ContentPlaceHolder1$DDL_xnxq": req.Term,
"ctl00$ContentPlaceHolder1$BT_submit": "确定",
})
if err != nil {
return nil, err
}
examInfos, err := parseExamRoom(res)
if err != nil {
return nil, err
}
return examInfos, nil
}
func parseExamRoom(doc *html.Node) ([]*ExamRoomInfo, error) {
var examInfos []*ExamRoomInfo
sel := htmlquery.FindOne(doc, "//*[@id=\"ContentPlaceHolder1_DataList_xxk\"]")
rows := htmlquery.Find(sel, ".//tr[@onmouseover]")
for _, row := range rows {
// 提取单元格内容
cells := htmlquery.Find(row, "./td")
// 获取每一列的内容
courseName := strings.TrimSpace(htmlquery.InnerText(cells[0]))
credit := strings.TrimSpace(htmlquery.InnerText(cells[1]))
teacher := strings.TrimSpace(htmlquery.InnerText(cells[2]))
dateTimeAndLocation := strings.TrimSpace(htmlquery.InnerText(cells[3]))
// 如果为空,说明目前没有安排考试
if dateTimeAndLocation == "" {
continue
}
// example: 2024年11月17日 12:30-17:30 旗山数计3-404
array := strings.Fields(dateTimeAndLocation)
date := array[0]
time := array[1]
location := array[2]
// 将数据存入结构体
examInfo := &ExamRoomInfo{
CourseName: courseName,
Credit: credit,
Teacher: teacher,
Date: date,
Time: time,
Location: location,
}
examInfos = append(examInfos, examInfo)
}
return examInfos, nil
}