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

37 lines
831 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"
"log"
"time"
)
// GetFavorites retrieves all favorites from the e621 API.
func (c *Client) GetFavorites(user models.E621User) ([]models.Post, error) {
time.Sleep(1 * time.Second)
2023-06-17 18:51:04 +00:00
var page int64
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 {
2023-06-17 18:51:04 +00:00
URIPath = fmt.Sprintf("favorites.json?user_id=%d&page=%d", user.ID, page)
favorite, err := ExecuteGetAPIRequest[models.APIe621Post](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
}
}