From 06ba87d1f2d20a334f9d679344b88fb42c6409d3 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 6 Nov 2023 15:26:47 +0100 Subject: [PATCH] feat: added getting n & all favs & some fixes Signed-off-by: SoXX --- example/highlevel/favorites.go | 50 +++++++++++++++++++++++++--------- go.mod | 6 +--- pkg/e621/builder/favorite.go | 12 ++++++-- pkg/e621/client.go | 31 +++++++++++++++++++-- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/example/highlevel/favorites.go b/example/highlevel/favorites.go index c28edc8..4e87698 100644 --- a/example/highlevel/favorites.go +++ b/example/highlevel/favorites.go @@ -9,21 +9,45 @@ import ( func main() { client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY")) - favorites, err := client.GetFavoritesForUser("selloo") - if err != nil { - log.Panic(err) + { + favorites, err := client.GetFavoritesForUser("selloo") + if err != nil { + log.Panic(err) + } + posts, err := favorites.Execute() + if err != nil { + log.Panic(err) + } + log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL) } - posts, err := favorites.Execute() - if err != nil { - log.Panic(err) - } - log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL) - favoritesWithTags := client.GetFavoritesForUserWithTags("selloo", "fennec male solo") - posts, err = favoritesWithTags.Execute() - if err != nil { - log.Panic(err) + { + favoritesBuilder, _ := client.GetFavoritesForUser("selloo") + favorites, err := client.GetNFavoritesForUser(10, favoritesBuilder) + if err != nil { + log.Panic(err) + } + + log.Println(len(favorites)) + + } + + { + favoritesBuilder, _ := client.GetFavoritesForUser("selloo") + favorites, err := client.GetAllFavoritesForUser(favoritesBuilder) + if err != nil { + log.Panic(err) + } + log.Println(len(favorites)) + } + + { + favoritesWithTags := client.GetFavoritesForUserWithTags("selloo", "fennec male solo") + posts, err := favoritesWithTags.Execute() + if err != nil { + log.Panic(err) + } + log.Printf("URL of favorite post 0 with tags is: %s", posts[0].File.URL) } - log.Printf("URL of favorite post 0 with tags is: %s", posts[0].File.URL) } diff --git a/go.mod b/go.mod index 074481c..68099d1 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,5 @@ go 1.21.3 require ( github.com/jarcoal/httpmock v1.3.1 github.com/joho/godotenv v1.5.1 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - golang.org/x/time v0.3.0 // indirect + golang.org/x/time v0.3.0 ) diff --git a/pkg/e621/builder/favorite.go b/pkg/e621/builder/favorite.go index 5802608..0395129 100644 --- a/pkg/e621/builder/favorite.go +++ b/pkg/e621/builder/favorite.go @@ -10,6 +10,7 @@ import ( type FavoritesBuilder interface { SetUserID(userID model.UserID) FavoritesBuilder SetLimit(limitFavorites int) FavoritesBuilder + Page(number int) FavoritesBuilder Execute() ([]model.Post, error) } @@ -25,17 +26,22 @@ type getFavorites struct { requestContext model.RequestContext } -func (g getFavorites) SetUserID(userID model.UserID) FavoritesBuilder { +func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder { g.query["user_id"] = strconv.Itoa(int(userID)) return g } -func (g getFavorites) SetLimit(limitFavorites int) FavoritesBuilder { +func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder { g.query["limit"] = strconv.Itoa(limitFavorites) return g } -func (g getFavorites) Execute() ([]model.Post, error) { +func (g *getFavorites) Page(pageNumber int) FavoritesBuilder { + g.query["page"] = strconv.Itoa(pageNumber) + return g +} + +func (g *getFavorites) Execute() ([]model.Post, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) if err != nil { diff --git a/pkg/e621/client.go b/pkg/e621/client.go index 4732d19..ad86132 100644 --- a/pkg/e621/client.go +++ b/pkg/e621/client.go @@ -7,7 +7,6 @@ import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" _ "github.com/joho/godotenv/autoload" "golang.org/x/time/rate" - "log" "math" "net/http" "strconv" @@ -57,6 +56,35 @@ func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, return favoritesBuilder, nil } +func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) { + if n < utils.E621_MAX_POST_COUNT { + favoriteBuilder.SetLimit(n) + } + + var favorites []model.Post + var page = 1 + for len(favorites) < n { + favoriteBuilder.Page(page) + favoriteBuilder.SetLimit(n - len(favorites)) + newFavorites, err := favoriteBuilder.Execute() + if err != nil { + return nil, err + } + if len(newFavorites) == 0 { + break + } + favorites = append(favorites, newFavorites...) + page = page + 1 + } + + return favorites, nil + +} + +func (c *Client) GetAllFavoritesForUser(favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) { + return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder) +} + func (c *Client) GetFavoritesForUserWithTags(username string, tags string) builder.PostsBuilder { favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags)) return favoritesBuilder @@ -92,7 +120,6 @@ func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Pos break } posts = append(posts, newPosts...) - log.Printf("Post ID: %d | Post Size: %d", lastPostID, len(posts)) } return posts, nil