From 3390a05c70d34e93484333b39c6f8d51927c2fff Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 11:35:04 +0200 Subject: [PATCH] feat: user favorite Add function to get all favorite submission IDs for a user --- pkg/furaffinaty/client.go | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/pkg/furaffinaty/client.go b/pkg/furaffinaty/client.go index f97ee1d..23f00e2 100644 --- a/pkg/furaffinaty/client.go +++ b/pkg/furaffinaty/client.go @@ -90,6 +90,52 @@ func (c *Client) GetSubmissionIDs() ([]string, error) { return c.submissionIDs, nil } +func (c *Client) GetSubmissionIDsForFavorites(username string) ([]string, error) { + submissionURL := fmt.Sprintf("%s/favorites/%s", c.RequestContext.Host, username) + nextSubmissionURL := "" + + // Set up the OnHTML callback for the "Next" button + c.RequestContext.Client.OnHTML("a.standard", func(e *colly.HTMLElement) { + nextURL := e.Attr("href") + if nextURL != "" { + nextSubmissionURL = fmt.Sprintf("%s%s", c.RequestContext.Host, nextURL) + } + }) + + for { + newSubmissionIDs := []string{} + + // Parse the HTML and get everything inside the figure tags into a struct + c.RequestContext.Client.OnHTML("figure", func(e *colly.HTMLElement) { + id := strings.Split(e.Attr("id"), "sid-") + if len(id) > 1 { + newSubmissionIDs = append(newSubmissionIDs, id[1]) + } + }) + + err := c.RequestContext.Client.Visit(submissionURL) + if err != nil { + return nil, err + } + + log.Printf("Next Submission URL: %s", nextSubmissionURL) + + if len(newSubmissionIDs) == 0 { + break // Exit loop if no new IDs found + } + + c.submissionIDs = append(c.submissionIDs, newSubmissionIDs...) + + if nextSubmissionURL == "" { + break // Exit loop if no next page found + } + + submissionURL = nextSubmissionURL // Update to next page + } + + return c.submissionIDs, nil +} + // GetSubmissions // Set limit to 0 if you want all submissions func (c *Client) GetSubmissions(limit int) ([]models.Submission, error) {