This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/e621/favorite.go

40 lines
975 B
Go
Raw Normal View History

2023-05-24 14:05:27 +00:00
package e621
2023-05-22 11:08:08 +00:00
import (
2023-05-24 14:05:27 +00:00
"e621_to_neo4j/e621/models"
2023-05-22 11:08:08 +00:00
"fmt"
2023-06-21 11:29:23 +00:00
log "github.com/sirupsen/logrus"
2023-05-22 11:08:08 +00:00
)
// GetFavorites retrieves all favorites from the e621 API.
func (c *Client) GetFavorites(user models.E621User) ([]models.Post, error) {
page := 1
2023-05-22 11:08:08 +00:00
var allFavorites []models.Post
2023-06-17 18:51:04 +00:00
var URIPath string
2023-05-22 11:08:08 +00:00
for {
URIPath = fmt.Sprintf("favorites.json?limit=320?user_id=%d&page=%d", user.ID, page)
2023-06-21 11:29:23 +00:00
log.WithFields(log.Fields{
"id": user.ID,
"fav_page": page,
"uri": URIPath,
}).Debug("Requesting API for favorites")
favorite, err := ExecuteGetAPIRequest[models.PostResponseWrapper](c, URIPath)
2023-05-22 11:08:08 +00:00
if err != nil {
log.Printf(err.Error())
}
2023-06-17 18:51:04 +00:00
2023-05-22 11:08:08 +00:00
// Append the fetched posts to the result slice
2023-06-17 18:51:04 +00:00
allFavorites = append(allFavorites, favorite.Posts...)
2023-05-22 11:08:08 +00:00
// If no more posts are returned, return the accumulated favorites
2023-06-17 18:51:04 +00:00
if len(favorite.Posts) == 0 {
2023-05-22 11:08:08 +00:00
return allFavorites, nil
}
// Update the last post ID for the next page request
2023-06-17 18:51:04 +00:00
page += 1
2023-05-22 11:08:08 +00:00
}
}