2023-10-24 13:25:50 +00:00
|
|
|
package e621
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder"
|
|
|
|
"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-24 13:25:50 +00:00
|
|
|
_ "github.com/joho/godotenv/autoload"
|
|
|
|
"golang.org/x/time/rate"
|
2023-11-06 12:46:54 +00:00
|
|
|
"log"
|
|
|
|
"math"
|
2023-10-24 13:25:50 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
RequestContext model.RequestContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewE621Client(username string, apikey string) Client {
|
|
|
|
return Client{
|
|
|
|
RequestContext: model.RequestContext{
|
|
|
|
Client: http.Client{},
|
|
|
|
RateLimiter: rate.NewLimiter(1, 2),
|
|
|
|
Host: "https://e621.net",
|
|
|
|
UserAgent: fmt.Sprintf("Go-e621-SDK used by %s | (made by the Anthrove Team)", username),
|
|
|
|
Username: username,
|
|
|
|
APIKey: apikey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetHost(host string) *Client {
|
|
|
|
c.RequestContext.Host = host
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetAgentName(userAgent string) *Client {
|
|
|
|
c.RequestContext.UserAgent = userAgent
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetUserByName(username string) builder.UserBuilder {
|
|
|
|
return builder.NewGetUserBuilder(c.RequestContext).SetUsername(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetUserByID(userID model.UserID) builder.UserBuilder {
|
|
|
|
return builder.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, error) {
|
|
|
|
user, err := builder.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
favoritesBuilder := builder.NewGetFavoritesBuilder(c.RequestContext).SetUserID(user.ID)
|
|
|
|
return favoritesBuilder, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder {
|
|
|
|
return builder.NewGetPostBuilder(c.RequestContext).SetPostID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetPosts() builder.PostsBuilder {
|
|
|
|
return builder.NewGetPostsBuilder(c.RequestContext)
|
|
|
|
}
|
2023-11-06 11:42:40 +00:00
|
|
|
|
|
|
|
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 {
|
2023-11-06 12:46:54 +00:00
|
|
|
lastPostID := posts[len(posts)-1].ID
|
|
|
|
postBuilder.PageBefore(lastPostID)
|
|
|
|
postBuilder.SetLimit(n - len(posts))
|
2023-11-06 11:42:40 +00:00
|
|
|
newPosts, err := postBuilder.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-06 12:46:54 +00:00
|
|
|
if len(newPosts) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2023-11-06 11:42:40 +00:00
|
|
|
posts = append(posts, newPosts...)
|
2023-11-06 12:46:54 +00:00
|
|
|
log.Printf("Post ID: %d | Post Size: %d", lastPostID, len(posts))
|
2023-11-06 11:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return posts, nil
|
|
|
|
}
|
2023-11-06 12:46:54 +00:00
|
|
|
|
|
|
|
func (c *Client) GetAllPosts(postBuilder builder.PostsBuilder) ([]model.Post, error) {
|
|
|
|
return c.GetNPosts(math.MaxInt, postBuilder)
|
|
|
|
}
|