feat: added getting n posts

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-11-06 12:42:40 +01:00
parent 517c7f595e
commit f446823abd
4 changed files with 45 additions and 7 deletions

View File

@ -28,4 +28,11 @@ func main() {
} }
log.Printf("Post 1337 has following tags: %s", post.Tags) 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))
} }

View File

@ -4,22 +4,25 @@ import (
"context" "context"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" "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/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"strconv" "strconv"
) )
type PostsBuilder interface { type PostsBuilder interface {
Tags(tags string) PostsBuilder Tags(tags string) PostsBuilder
PageAfter(postID int) PostsBuilder PageAfter(postID model.PostID) PostsBuilder
pageBefore(postID int) PostsBuilder pageBefore(postID model.PostID) PostsBuilder
SetLimit(limitUser int) PostsBuilder SetLimit(limitUser int) PostsBuilder
Execute() ([]model.Post, error) Execute() ([]model.Post, error)
} }
func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder { func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder {
return &getPosts{ var postBuilder PostsBuilder = &getPosts{
requestContext: requestContext, requestContext: requestContext,
query: make(map[string]string), query: make(map[string]string),
} }
return postBuilder.SetLimit(utils.E621_MAX_POST_COUNT)
} }
type getPosts struct { type getPosts struct {
@ -32,13 +35,13 @@ func (g *getPosts) Tags(tags string) PostsBuilder {
return g return g
} }
func (g *getPosts) PageAfter(postID int) PostsBuilder { func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder {
g.query["page"] = "a" + strconv.Itoa(postID) g.query["page"] = "a" + strconv.Itoa(int(postID))
return g return g
} }
func (g *getPosts) pageBefore(postID int) PostsBuilder { func (g *getPosts) pageBefore(postID model.PostID) PostsBuilder {
g.query["page"] = "b" + strconv.Itoa(postID) g.query["page"] = "b" + strconv.Itoa(int(postID))
return g return g
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "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/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"net/http" "net/http"
@ -66,3 +67,25 @@ func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder {
func (c *Client) GetPosts() builder.PostsBuilder { func (c *Client) GetPosts() builder.PostsBuilder {
return builder.NewGetPostsBuilder(c.RequestContext) 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
}

View File

@ -0,0 +1,5 @@
package utils
const (
E621_MAX_POST_COUNT = 320
)