Skip to content

Commit

Permalink
exam room query completed (#15)
Browse files Browse the repository at this point in the history
* exam room query completed

* fix importing
  • Loading branch information
SchwarzSail authored Nov 23, 2024
1 parent fb90665 commit 28065f6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
CreditQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xyzk/xftj/CreditStatistics.aspx"
GPAQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xyzk/jdpm/GPA_sheet.aspx"
VerifyCodeURL = "https://jwcjwxt1.fzu.edu.cn/plus/verifycode.asp"
ExamRoomQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xkjg/examination/exam_list.aspx"

JwchPrefix = "https://jwcjwxt2.fzu.edu.cn:81"
JwchReferer = "https://jwcjwxt1.fzu.edu.cn/"
Expand Down
10 changes: 10 additions & 0 deletions jwch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,13 @@ func TestGetUnifiedExam(t *testing.T) {

// 不允许输出考试成绩信息
}

// 考场信息
func TestGetExamRoomInfo(t *testing.T) {
_, err := stu.GetExamRoom(ExamRoomReq{
Term: "202401",
})
if err != nil {
t.Error(err)
}
}
13 changes: 13 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,16 @@ type UnifiedExam struct {
Score string
Term string
}

type ExamRoomReq struct {
Term string
}

type ExamRoomInfo struct {
CourseName string // 课程名称
Credit string // 学分
Teacher string // 任课教师
Date string // 考试日期
Time string // 考试时间
Location string // 考试地点
}
68 changes: 64 additions & 4 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ 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.getEmptyRoomState()
viewStateMap, err := s.getState(constants.ClassroomQueryURL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,7 +97,7 @@ func (s *Student) GetEmptyRoom(req EmptyRoomReq) ([]string, error) {
}

func (s *Student) GetQiShanEmptyRoom(req EmptyRoomReq) ([]string, error) {
viewStateMap, err := s.getEmptyRoomState()
viewStateMap, err := s.getState(constants.ClassroomQueryURL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -182,8 +184,9 @@ func (s *Student) GetQiShanEmptyRoom(req EmptyRoomReq) ([]string, error) {
}

// 获取VIEWSTATE和EVENTVALIDATION
func (s *Student) getEmptyRoomState() (map[string]string, error) {
resp, err := s.GetWithIdentifier(constants.ClassroomQueryURL)
// 抽象成一个函数, 因为基本上每个请求都需要这两个参数
func (s *Student) getState(url string) (map[string]string, error) {
resp, err := s.GetWithIdentifier(url)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -251,3 +254,60 @@ func parseEmptyRoom(doc *html.Node) ([]string, error) {
}
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
}

0 comments on commit 28065f6

Please sign in to comment.