-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcourse.go
138 lines (115 loc) · 3.86 KB
/
course.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
package yjsy
import (
"github.com/antchfx/htmlquery"
"github.com/west2-online/yjsy/constants"
"strconv"
"strings"
)
func (s *Student) parseSinglePage(url string) ([]*Course, string, error) {
resp, err := s.GetWithIdentifier(url, map[string]string{})
if err != nil {
return nil, "", err
}
// Locate the rows in the course table
rows := htmlquery.Find(resp, `//div[@id='divContent']//table//tr[position()>1]`)
courses := make([]*Course, 0)
for _, row := range rows {
cells := htmlquery.Find(row, `td`)
// Parse fields
term := strings.TrimSpace(htmlquery.InnerText(cells[0]))
name := strings.TrimSpace(htmlquery.InnerText(cells[2]))
teacher := strings.TrimSpace(htmlquery.InnerText(cells[5]))
rawScheduleHTML := htmlquery.OutputHTML(cells[6], false) // Extract full HTML for schedule rules to recognize multiple lessons in one week
remark := strings.TrimSpace(htmlquery.InnerText(cells[8]))
lessonPlan := ""
lessonPlanLink := htmlquery.FindOne(cells[7], `.//a[@href]`)
if lessonPlanLink != nil {
lessonPlan = htmlquery.SelectAttr(lessonPlanLink, "href")
lessonPlan = strings.Join([]string{constants.YjsyPrefix, strings.TrimPrefix(lessonPlan, "..")}, "")
}
// Parse schedule rules
scheduleRules := parseScheduleRulesFromHTML(rawScheduleHTML)
// Append to the result
courses = append(courses, &Course{
Name: name,
Teacher: teacher,
ScheduleRules: scheduleRules,
Remark: remark,
LessonPlan: lessonPlan,
RawScheduleRules: rawScheduleHTML,
RawAdjust: "",
Term: term,
})
}
nextPage := htmlquery.FindOne(resp, `//div[@id='divPage']//a[contains(text(), '下一页')]`)
var nextPageURL string
if nextPage != nil {
href := htmlquery.SelectAttr(nextPage, "href")
nextPageURL = strings.Join([]string{constants.CourseURL, href}, "")
}
return courses, nextPageURL, nil
}
// Function to parse schedule rules from HTML
func parseScheduleRulesFromHTML(rawScheduleHTML string) []CourseScheduleRule {
// Replace <br> tags with newlines
rawScheduleHTML = strings.ReplaceAll(rawScheduleHTML, "<br>", "\n")
rawScheduleHTML = strings.ReplaceAll(rawScheduleHTML, "<br/>", "\n")
return parseScheduleRules(rawScheduleHTML)
}
// Existing parseScheduleRules function
func parseScheduleRules(rawScheduleRules string) []CourseScheduleRule {
lines := strings.Split(rawScheduleRules, "\n")
var rules []CourseScheduleRule
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Example: "1-8周 星期3:9-11节 东3-109"
parts := strings.Fields(line)
if len(parts) < 3 {
continue
}
// Parsing week, day, and location
weekInfo := strings.Split(parts[0], "-")
dayInfo := strings.Split(parts[1], ":")
classInfo := strings.Split(strings.TrimSuffix(dayInfo[1], "节"), "-")
startWeek, _ := strconv.Atoi(strings.TrimSuffix(weekInfo[0], "周"))
endWeek, _ := strconv.Atoi(strings.TrimSuffix(weekInfo[1], "周"))
weekday, _ := strconv.Atoi(strings.TrimPrefix(dayInfo[0], "星期"))
startClass, _ := strconv.Atoi(classInfo[0])
endClass, _ := strconv.Atoi(classInfo[1])
location := parts[2]
rules = append(rules, CourseScheduleRule{
Location: location,
StartClass: startClass,
EndClass: endClass,
StartWeek: startWeek,
EndWeek: endWeek,
Weekday: weekday,
Single: true,
Double: true,
Adjust: false,
})
}
return rules
}
func (s *Student) GetSemesterCourses() ([]*Course, error) {
allCourses := make([]*Course, 0)
// 递归解析当前页数据
currentURL := constants.CourseURL
for {
courses, nextPageURL, err := s.parseSinglePage(currentURL)
if err != nil {
return nil, err
}
// 追加当前页课程
allCourses = append(allCourses, courses...)
// 如果没有下一页,结束循环
if nextPageURL == "" {
break
}
currentURL = nextPageURL
}
return allCourses, nil
}