Skip to content

Commit

Permalink
Merge pull request #21 from SchwarzSail/add_page_num
Browse files Browse the repository at this point in the history
add pageNum as value returned
  • Loading branch information
SchwarzSail authored Jan 11, 2025
2 parents dbb4149 + 34ddea1 commit 5898c0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
5 changes: 3 additions & 2 deletions jwch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ func TestGetExamRoomInfo(t *testing.T) {
}

func TestGetNoticesInfo(t *testing.T) {
content, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2})
content, totalPages, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2})
fmt.Println(totalPages)
if err != nil {
t.Error(err)
}
if content == nil {
if content == nil || totalPages == 0 {
t.Error("content is nil")
}
}
Expand Down
32 changes: 16 additions & 16 deletions notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,42 @@ import (
"github.com/west2-online/jwch/constants"
)

func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, err error) {
func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, totalPages int, err error) {
// 获取通知公告页面的总页数
res, err := s.PostWithIdentifier(constants.NoticeInfoQueryURL, map[string]string{})
if err != nil {
return nil, err
return nil, 0, 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
return nil, 0, err
}
// 判断是否超出总页数
if req.PageNum > lastPageNum {
return nil, fmt.Errorf("超出总页数")
return nil, lastPageNum, fmt.Errorf("超出总页数")
}
// 首页直接爬取
if req.PageNum == 1 {
list, err = parseNoticeInfo(res)
if err != nil {
return nil, lastPageNum, err
}
return list, lastPageNum, nil
}
// 根据总页数计算 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
return nil, lastPageNum, err
}
list, err = parseNoticeInfo(doc)
if err != nil {
return nil, err
return nil, lastPageNum, err
}
// 3. 返回结果
return list, nil
return list, lastPageNum, nil
}

// 获取当前页面的所有数据信息
Expand Down

0 comments on commit 5898c0e

Please sign in to comment.