From f446823abdab193321aa6ea3aa3cac5fb5fdb6c6 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 6 Nov 2023 12:42:40 +0100 Subject: [PATCH] feat: added getting n posts Signed-off-by: SoXX --- example/highlevel/post.go | 7 +++++++ pkg/e621/builder/posts.go | 17 ++++++++++------- pkg/e621/client.go | 23 +++++++++++++++++++++++ pkg/e621/utils/constants.go | 5 +++++ 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 pkg/e621/utils/constants.go diff --git a/example/highlevel/post.go b/example/highlevel/post.go index 79839ad..338c035 100644 --- a/example/highlevel/post.go +++ b/example/highlevel/post.go @@ -28,4 +28,11 @@ func main() { } log.Printf("Post 1337 has following tags: %s", post.Tags) + postBuilder := client.GetPosts() + posts, err = client.GetNPosts(230, postBuilder) + if err != nil { + log.Panic(err) + } + log.Println(len(posts)) + } diff --git a/pkg/e621/builder/posts.go b/pkg/e621/builder/posts.go index 92ab64a..0ba5736 100644 --- a/pkg/e621/builder/posts.go +++ b/pkg/e621/builder/posts.go @@ -4,22 +4,25 @@ import ( "context" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "strconv" ) type PostsBuilder interface { Tags(tags string) PostsBuilder - PageAfter(postID int) PostsBuilder - pageBefore(postID int) PostsBuilder + PageAfter(postID model.PostID) PostsBuilder + pageBefore(postID model.PostID) PostsBuilder SetLimit(limitUser int) PostsBuilder Execute() ([]model.Post, error) } func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder { - return &getPosts{ + var postBuilder PostsBuilder = &getPosts{ requestContext: requestContext, query: make(map[string]string), } + + return postBuilder.SetLimit(utils.E621_MAX_POST_COUNT) } type getPosts struct { @@ -32,13 +35,13 @@ func (g *getPosts) Tags(tags string) PostsBuilder { return g } -func (g *getPosts) PageAfter(postID int) PostsBuilder { - g.query["page"] = "a" + strconv.Itoa(postID) +func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder { + g.query["page"] = "a" + strconv.Itoa(int(postID)) return g } -func (g *getPosts) pageBefore(postID int) PostsBuilder { - g.query["page"] = "b" + strconv.Itoa(postID) +func (g *getPosts) pageBefore(postID model.PostID) PostsBuilder { + g.query["page"] = "b" + strconv.Itoa(int(postID)) return g } diff --git a/pkg/e621/client.go b/pkg/e621/client.go index afc6a47..64c589e 100644 --- a/pkg/e621/client.go +++ b/pkg/e621/client.go @@ -4,6 +4,7 @@ import ( "fmt" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" _ "github.com/joho/godotenv/autoload" "golang.org/x/time/rate" "net/http" @@ -66,3 +67,25 @@ func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder { func (c *Client) GetPosts() builder.PostsBuilder { return builder.NewGetPostsBuilder(c.RequestContext) } + +func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Post, error) { + if n < utils.E621_MAX_POST_COUNT { + postBuilder.SetLimit(n) + } + + posts, err := postBuilder.Execute() + if err != nil { + return nil, err + } + + for len(posts) < n { + postBuilder.PageAfter(posts[len(posts)-1].ID).SetLimit(n - len(posts)) + newPosts, err := postBuilder.Execute() + if err != nil { + return nil, err + } + posts = append(posts, newPosts...) + } + + return posts, nil +} diff --git a/pkg/e621/utils/constants.go b/pkg/e621/utils/constants.go new file mode 100644 index 0000000..8322e6b --- /dev/null +++ b/pkg/e621/utils/constants.go @@ -0,0 +1,5 @@ +package utils + +const ( + E621_MAX_POST_COUNT = 320 +)