pageCount method implemented

This commit is contained in:
milarin 2023-03-06 11:41:29 +01:00
parent ad5b38c838
commit 391e6f7295
2 changed files with 27 additions and 4 deletions

29
api.go
View File

@ -141,13 +141,13 @@ func (api *Api) GetChapterEntriesByNovelID(novelID NovelID) *Cursor[NovelChapter
return return
} }
pageCount, err := strconv.ParseInt(doc.Find(".digg_pagination a:nth-child(5)").Text(), 10, 64) pageCount, err := api.getPageCount(doc)
if err != nil { if err != nil {
adverr.Println(ErrApiElementNotFound.Wrap(err, ".digg_pagination a:nth-child(5)")) fmt.Fprintln(os.Stderr, err)
return return
} }
for pageIndex := int(pageCount); pageIndex > 0; pageIndex-- { for pageIndex := pageCount; pageIndex > 0; pageIndex-- {
if ctx.Err() != nil { if ctx.Err() != nil {
break break
} }
@ -172,6 +172,29 @@ func (api *Api) GetChapterEntriesByNovelID(novelID NovelID) *Cursor[NovelChapter
} }
} }
func (api *Api) getPageCount(doc *goquery.Document) (int, error) {
pagination := doc.Find(".digg_pagination")
if pagination.Children().Length() >= 6 {
// more than 4 pages (next_page + last_page + collapsed_pages buttons included)
pageCount, err := strconv.ParseInt(pagination.Find("a:nth-child(5)").Text(), 10, 64)
if err != nil {
adverr.Println(ErrApiElementNotFound.Wrap(err, ".digg_pagination a:nth-child(5)"))
return 0, err
}
return int(pageCount), nil
} else {
pageCount, err := strconv.ParseInt(pagination.Find(".next_page").Text(), 10, 64)
if err != nil {
adverr.Println(ErrApiElementNotFound.Wrap(err, ".digg_pagination a:nth-child(5)"))
return 0, err
}
return int(pageCount), nil
}
}
func (api *Api) getChapterEntriesByPageIndex(ctx context.Context, novelID NovelID, pageIndex int) ([]NovelChapterEntry, error) { func (api *Api) getChapterEntriesByPageIndex(ctx context.Context, novelID NovelID, pageIndex int) ([]NovelChapterEntry, error) {
doc, err := api.Get(ctx, fmt.Sprintf("https://www.novelupdates.com/series/%s/?pg=%d", novelID, pageIndex)) doc, err := api.Get(ctx, fmt.Sprintf("https://www.novelupdates.com/series/%s/?pg=%d", novelID, pageIndex))
if err != nil { if err != nil {

View File

@ -7,5 +7,5 @@ var (
ErrCurlRequestFailed = adverr.NewErrTmpl("ErrCurlRequestFailed", "curl request failed for url: '%s'") ErrCurlRequestFailed = adverr.NewErrTmpl("ErrCurlRequestFailed", "curl request failed for url: '%s'")
ErrInvalidGzipData = adverr.NewErrTmpl("ErrInvalidGzipData", "gzip encoded data expected") ErrInvalidGzipData = adverr.NewErrTmpl("ErrInvalidGzipData", "gzip encoded data expected")
ErrApiRequestFailed = adverr.NewErrTmpl("ErrApiRequestFailed", "Data retrieval from NU failed") ErrApiRequestFailed = adverr.NewErrTmpl("ErrApiRequestFailed", "Data retrieval from NU failed")
ErrApiElementNotFound = adverr.NewErrTmpl("ErrApiElementNotFound", "element not found: '%ss'") ErrApiElementNotFound = adverr.NewErrTmpl("ErrApiElementNotFound", "element not found: '%s'")
) )