feat: user favorite

Add function to get all favorite submission IDs for a user
This commit is contained in:
SoXX 2024-08-09 11:35:04 +02:00
parent 6244396954
commit 3390a05c70

View File

@ -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) {