2023-10-23 11:15:55 +00:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2023-10-24 13:10:39 +00:00
|
|
|
"context"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
2023-11-06 11:42:40 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
|
2023-10-23 11:15:55 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// PostsBuilder represents a builder for constructing queries to retrieve posts.
|
2023-10-23 11:15:55 +00:00
|
|
|
type PostsBuilder interface {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Tags sets the query to search for posts with specific tags.
|
2023-10-23 11:15:55 +00:00
|
|
|
Tags(tags string) PostsBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// PageAfter sets the query to retrieve posts after a specific post ID.
|
2023-11-06 11:42:40 +00:00
|
|
|
PageAfter(postID model.PostID) PostsBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// PageBefore sets the query to retrieve posts before a specific post ID.
|
2023-11-06 12:46:54 +00:00
|
|
|
PageBefore(postID model.PostID) PostsBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// PageNumber sets the query to retrieve posts on a specific page number.
|
2023-11-06 12:46:54 +00:00
|
|
|
PageNumber(number int) PostsBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetLimit sets the maximum number of posts to retrieve.
|
2023-10-23 11:15:55 +00:00
|
|
|
SetLimit(limitUser int) PostsBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// Execute sends the constructed query and returns a slice of posts and an error, if any.
|
2023-10-23 11:15:55 +00:00
|
|
|
Execute() ([]model.Post, error)
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// NewGetPostsBuilder creates a new PostsBuilder with the provided RequestContext.
|
2023-10-23 11:15:55 +00:00
|
|
|
func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
postBuilder := &getPosts{
|
2023-10-23 11:15:55 +00:00
|
|
|
requestContext: requestContext,
|
|
|
|
query: make(map[string]string),
|
|
|
|
}
|
2023-11-06 11:42:40 +00:00
|
|
|
|
|
|
|
return postBuilder.SetLimit(utils.E621_MAX_POST_COUNT)
|
2023-10-23 11:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type getPosts struct {
|
|
|
|
requestContext model.RequestContext
|
|
|
|
query map[string]string
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// Tags sets the query to search for posts with specific tags.
|
2023-10-23 11:15:55 +00:00
|
|
|
func (g *getPosts) Tags(tags string) PostsBuilder {
|
|
|
|
g.query["tags"] = tags
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// PageAfter sets the query to retrieve posts after a specific post ID.
|
2023-11-06 11:42:40 +00:00
|
|
|
func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder {
|
|
|
|
g.query["page"] = "a" + strconv.Itoa(int(postID))
|
2023-10-23 11:15:55 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// PageBefore sets the query to retrieve posts before a specific post ID.
|
2023-11-06 12:46:54 +00:00
|
|
|
func (g *getPosts) PageBefore(postID model.PostID) PostsBuilder {
|
2023-11-06 11:42:40 +00:00
|
|
|
g.query["page"] = "b" + strconv.Itoa(int(postID))
|
2023-10-23 11:15:55 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// PageNumber sets the query to retrieve posts on a specific page number.
|
2023-11-06 12:46:54 +00:00
|
|
|
func (g *getPosts) PageNumber(number int) PostsBuilder {
|
|
|
|
g.query["page"] = strconv.Itoa(number)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetLimit sets the maximum number of posts to retrieve.
|
2023-10-23 11:15:55 +00:00
|
|
|
func (g *getPosts) SetLimit(limitUser int) PostsBuilder {
|
|
|
|
g.query["limit"] = strconv.Itoa(limitUser)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// Execute sends the constructed query and returns a slice of posts and an error, if any.
|
2023-10-23 11:15:55 +00:00
|
|
|
func (g *getPosts) Execute() ([]model.Post, error) {
|
2023-10-24 13:10:39 +00:00
|
|
|
if g.requestContext.RateLimiter != nil {
|
|
|
|
err := g.requestContext.RateLimiter.Wait(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 11:15:55 +00:00
|
|
|
posts, err := endpoints.GetPosts(g.requestContext, g.query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return posts, err
|
|
|
|
}
|