feat: added getting n posts
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
517c7f595e
commit
f446823abd
@ -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))
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
5
pkg/e621/utils/constants.go
Normal file
5
pkg/e621/utils/constants.go
Normal file
@ -0,0 +1,5 @@
|
||||
package utils
|
||||
|
||||
const (
|
||||
E621_MAX_POST_COUNT = 320
|
||||
)
|
Reference in New Issue
Block a user