Skip to content

Commit

Permalink
fix infinite pagination loop for last page
Browse files Browse the repository at this point in the history
  • Loading branch information
jvatic committed Aug 21, 2021
1 parent 22e0bb5 commit ffbc0df
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions audible/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,24 @@ outer:
break outer
}

for _, u := range visited {
if page.NextPageURL == u {
nextPageURL, err := url.Parse(page.NextPageURL)
if err != nil {
break outer
}

pageNumber := nextPageURL.Query().Get("page")
if pageNumber == "" {
break outer
}

for _, n := range visited {
if pageNumber == n {
break outer
}
}

visited = append(visited, page.NextPageURL)
visited = append(visited, pageNumber)

page, err = c.getLibraryPage(ctx, page.NextPageURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -161,7 +172,7 @@ func (c *Client) getLibraryPage(ctx context.Context, pageURL string) (*Page, err

page := &Page{}

paginationAnchors := htmlquery.Find(doc, "//a[@data-name = 'page']")
paginationAnchors := htmlquery.Find(doc, "//s/a[@data-name = 'page']")
if len(paginationAnchors) > 0 {
page.NextPageURL = htmlquery.SelectAttr(paginationAnchors[len(paginationAnchors)-1], "href")
}
Expand Down

0 comments on commit ffbc0df

Please sign in to comment.