From 391e6f7295ba4e98014b599b61b41f25c1896a80 Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 6 Mar 2023 11:41:29 +0100 Subject: [PATCH] pageCount method implemented --- api.go | 29 ++++++++++++++++++++++++++--- errors.go | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 25f9615..a313262 100644 --- a/api.go +++ b/api.go @@ -141,13 +141,13 @@ func (api *Api) GetChapterEntriesByNovelID(novelID NovelID) *Cursor[NovelChapter return } - pageCount, err := strconv.ParseInt(doc.Find(".digg_pagination a:nth-child(5)").Text(), 10, 64) + pageCount, err := api.getPageCount(doc) if err != nil { - adverr.Println(ErrApiElementNotFound.Wrap(err, ".digg_pagination a:nth-child(5)")) + fmt.Fprintln(os.Stderr, err) return } - for pageIndex := int(pageCount); pageIndex > 0; pageIndex-- { + for pageIndex := pageCount; pageIndex > 0; pageIndex-- { if ctx.Err() != nil { 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) { doc, err := api.Get(ctx, fmt.Sprintf("https://www.novelupdates.com/series/%s/?pg=%d", novelID, pageIndex)) if err != nil { diff --git a/errors.go b/errors.go index 2f1a7dd..ddd2df7 100644 --- a/errors.go +++ b/errors.go @@ -7,5 +7,5 @@ var ( ErrCurlRequestFailed = adverr.NewErrTmpl("ErrCurlRequestFailed", "curl request failed for url: '%s'") ErrInvalidGzipData = adverr.NewErrTmpl("ErrInvalidGzipData", "gzip encoded data expected") 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'") )