-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcourse.go
212 lines (180 loc) · 6.3 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
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
package yjsy
import (
"strconv"
"strings"
"github.com/antchfx/htmlquery"
"github.com/west2-online/yjsy/constants"
)
func (s *Student) GetTerms() (*Term, error) {
resp, err := s.GetWithIdentifier(constants.TermURL, map[string]string{})
if err != nil {
return nil, err
}
rows := htmlquery.Find(resp, `//div[@id='divContent']//table//tr[position()>1]`)
terms := new(Term)
for _, row := range rows {
cells := htmlquery.Find(row, `td`)
term := strings.TrimSpace(htmlquery.InnerText(cells[0]))
terms.Terms = append(terms.Terms, term)
}
return terms, nil
}
func (s *Student) GetSemesterCourses(term string) ([]*Course, error) {
allCourses := make([]*Course, 0)
// 递归解析当前页数据
currentURL := constants.CourseURL
courses, nextPageURL, err := s.parseSinglePageByXNXQ(currentURL, term)
if err != nil {
return nil, err
}
// 追加当前页课程
allCourses = append(allCourses, courses...)
// 如果没有下一页,结束循环
for nextPageURL != "" {
currentURL = nextPageURL
courses, nextPageURL, err = s.parseNextPage(currentURL)
if err != nil {
return nil, err
}
allCourses = append(allCourses, courses...)
}
return allCourses, nil
}
func (s *Student) parseSinglePageByXNXQ(url string, term string) ([]*Course, string, error) {
resp, err := s.GetWithIdentifier(url, map[string]string{
"strwhere": strings.Join([]string{"XNXQ='", term, "'"}, ""),
})
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`)
if len(cells) < 8 {
continue
}
// 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) parseNextPage(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
}