Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: complete getting notice info #19

Merged
merged 9 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ limitations under the License.
package constants

const (
ClassroomQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/kkgl/kbcx/kbcx_kjs.aspx"
CourseURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xkjg/wdxk/xkjg_list.aspx"
MarksQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xyzk/cjyl/score_sheet.aspx"
CETQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/glbm/cet/cet_cszt.aspx"
JSQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/glbm/computer/jsj_cszt.aspx"
UserInfoURL = "https://jwcjwxt2.fzu.edu.cn:81/jcxx/xsxx/StudentInformation.aspx"
SSOLoginURL = "https://jwcjwxt2.fzu.edu.cn/Sfrz/SSOLogin"
SchoolCalendarURL = "https://jwcjwxt2.fzu.edu.cn:82/xl.asp"
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"
CultivatePlanURL = "https://jwcjwxt2.fzu.edu.cn:81/pyfa/pyjh/pyjh_list.aspx"
ClassroomQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/kkgl/kbcx/kbcx_kjs.aspx"
CourseURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xkjg/wdxk/xkjg_list.aspx"
MarksQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/xyzk/cjyl/score_sheet.aspx"
CETQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/glbm/cet/cet_cszt.aspx"
JSQueryURL = "https://jwcjwxt2.fzu.edu.cn:81/student/glbm/computer/jsj_cszt.aspx"
UserInfoURL = "https://jwcjwxt2.fzu.edu.cn:81/jcxx/xsxx/StudentInformation.aspx"
SSOLoginURL = "https://jwcjwxt2.fzu.edu.cn/Sfrz/SSOLogin"
SchoolCalendarURL = "https://jwcjwxt2.fzu.edu.cn:82/xl.asp"
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"
NoticeInfoQueryURL = "https://jwch.fzu.edu.cn/jxtz.htm"
JwchNoticeURLPrefix = "https://jwch.fzu.edu.cn/"
CultivatePlanURL = "https://jwcjwxt2.fzu.edu.cn:81/pyfa/pyjh/pyjh_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 @@ -278,6 +278,16 @@ func TestGetExamRoomInfo(t *testing.T) {
}
}

func TestGetNoticesInfo(t *testing.T) {
content, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2})
if err != nil {
t.Error(err)
}
if content == nil {
t.Error("content is nil")
}
}

func TestGetCultivatePlan(t *testing.T) {
_, err := stu.GetCultivatePlan()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,13 @@ type ExamRoomInfo struct {
Time string // 考试时间
Location string // 考试地点
}

type NoticeInfo struct {
Title string // 通知标题
URL string // 通知链接
Date string // 通知日期
}

type NoticeInfoReq struct {
PageNum int // 获取第几页的数据,从 1 开始
}
121 changes: 121 additions & 0 deletions notice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
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 (
"fmt"
"strings"

"github.com/antchfx/htmlquery"
"golang.org/x/net/html"

"github.com/west2-online/jwch/constants"
)

func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, err error) {
// 获取通知公告页面的总页数
res, err := s.PostWithIdentifier(constants.NoticeInfoQueryURL, map[string]string{})
if err != nil {
return nil, err
}
// 首页直接爬取
if req.PageNum == 1 {
list, err = parseNoticeInfo(res)
if err != nil {
return nil, err
}
return list, nil
}
// 分页需要根据页数计算 url
lastPageNum, err := getTotalPages(res)
if err != nil {
return nil, err
}
// 判断是否超出总页数
if req.PageNum > lastPageNum {
return nil, fmt.Errorf("超出总页数")
}
// 根据总页数计算 url
num := lastPageNum - req.PageNum + 1
url := fmt.Sprintf("https://jwch.fzu.edu.cn/jxtz/%d.htm", num)
doc, err := s.PostWithIdentifier(url, map[string]string{})
if err != nil {
return nil, err
}
list, err = parseNoticeInfo(doc)
if err != nil {
return nil, err
}
// 3. 返回结果
return list, nil
}

// 获取当前页面的所有数据信息
func parseNoticeInfo(doc *html.Node) ([]*NoticeInfo, error) {
// 解析通知公告页面
var list []*NoticeInfo

sel := htmlquery.FindOne(doc, "//div[@class='box-gl clearfix']")
if sel == nil {
return nil, fmt.Errorf("cannot find the notice list")
}

rows := htmlquery.Find(sel, ".//ul[@class='list-gl']/li")

for _, row := range rows {
// 提取日期
dateNode := htmlquery.FindOne(row, ".//span[@class='doclist_time']")
if dateNode == nil {
return nil, fmt.Errorf("cannot find the date")
}
date := strings.TrimSpace(htmlquery.InnerText(dateNode))
SchwarzSail marked this conversation as resolved.
Show resolved Hide resolved

// 提取标题
titleNode := htmlquery.FindOne(row, ".//a")

title := strings.TrimSpace(htmlquery.SelectAttr(titleNode, "title"))

// 提取 URL
url := strings.TrimSpace(htmlquery.SelectAttr(titleNode, "href"))
url = constants.JwchNoticeURLPrefix + url

noticeInfo := &NoticeInfo{
Title: title,
URL: url,
Date: date,
}
list = append(list, noticeInfo)
}

return list, nil
}

// 获取总页数
func getTotalPages(doc *html.Node) (int, error) {
totalPagesNode := htmlquery.FindOne(doc, "//span[@class='p_pages']//a[@href='jxtz/1.htm']")
if totalPagesNode == nil {
return 0, fmt.Errorf("未找到总页数")
}

totalPagesStr := htmlquery.InnerText(totalPagesNode)
var totalPages int
_, err := fmt.Sscanf(totalPagesStr, "%d", &totalPages)
if err != nil {
return 0, fmt.Errorf("解析总页数失败: %v", err)
}
return totalPages, nil
}
Loading