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
|
|
|
"math"
|
2023-10-24 13:25:50 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// Client is the main client for interacting with the e621 API.
|
2023-10-24 13:25:50 +00:00
|
|
|
type Client struct {
|
|
|
|
RequestContext model.RequestContext
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// NewE621Client creates a new e621 client with the provided username and API key.
|
|
|
|
func NewE621Client(username string, apiKey string) Client {
|
|
|
|
// Create a new e621 client with the given username and API key.
|
2023-10-24 13:25:50 +00:00
|
|
|
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,
|
2023-11-06 14:56:36 +00:00
|
|
|
APIKey: apiKey,
|
2023-10-24 13:25:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetHost sets the API host for the client.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) SetHost(host string) *Client {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Set the API host for the client.
|
2023-10-24 13:25:50 +00:00
|
|
|
c.RequestContext.Host = host
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetAgentName sets the user agent name for the client.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) SetAgentName(userAgent string) *Client {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Set the user agent name for the client.
|
2023-10-24 13:25:50 +00:00
|
|
|
c.RequestContext.UserAgent = userAgent
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetUserByName returns a user builder for a given username.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) GetUserByName(username string) builder.UserBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Returns a user builder for the specified username.
|
2023-10-24 13:25:50 +00:00
|
|
|
return builder.NewGetUserBuilder(c.RequestContext).SetUsername(username)
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetUserByID returns a user builder for a given user ID.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) GetUserByID(userID model.UserID) builder.UserBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Returns a user builder for the specified user ID.
|
2023-10-24 13:25:50 +00:00
|
|
|
return builder.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10))
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetFavoritesForUser returns a favorites builder for a given username.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, error) {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Returns a favorites builder for the specified username.
|
2023-10-24 13:25:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetNFavoritesForUser retrieves a specified number of favorites for a user.
|
2023-11-06 14:26:47 +00:00
|
|
|
func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Retrieves a specified number of favorite posts for a user.
|
2023-11-06 14:26:47 +00:00
|
|
|
if n < utils.E621_MAX_POST_COUNT {
|
|
|
|
favoriteBuilder.SetLimit(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
var favorites []model.Post
|
|
|
|
var page = 1
|
|
|
|
for len(favorites) < n {
|
|
|
|
favoriteBuilder.Page(page)
|
|
|
|
favoriteBuilder.SetLimit(n - len(favorites))
|
|
|
|
newFavorites, err := favoriteBuilder.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(newFavorites) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
favorites = append(favorites, newFavorites...)
|
|
|
|
page = page + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return favorites, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetAllFavoritesForUser retrieves all favorites for a user.
|
2023-11-06 14:26:47 +00:00
|
|
|
func (c *Client) GetAllFavoritesForUser(favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Retrieves all favorite posts for a user.
|
2023-11-06 14:26:47 +00:00
|
|
|
return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder)
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) GetFavoritesForUserWithTags(username string, tags string) builder.PostsBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Returns a posts builder for a user's favorites with specific tags.
|
2023-10-24 13:25:50 +00:00
|
|
|
favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags))
|
|
|
|
return favoritesBuilder
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetPostByID returns a post builder for a specific post ID.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Returns a post builder for a specific post ID.
|
2023-10-24 13:25:50 +00:00
|
|
|
return builder.NewGetPostBuilder(c.RequestContext).SetPostID(id)
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetPosts returns a posts builder for general post queries.
|
2023-10-24 13:25:50 +00:00
|
|
|
func (c *Client) GetPosts() builder.PostsBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Returns a posts builder for general post queries.
|
2023-10-24 13:25:50 +00:00
|
|
|
return builder.NewGetPostsBuilder(c.RequestContext)
|
|
|
|
}
|
2023-11-06 11:42:40 +00:00
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetNPosts retrieves a specified number of posts.
|
2023-11-06 11:42:40 +00:00
|
|
|
func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Post, error) {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Retrieves a specified number of posts using the provided post builder.
|
2023-11-06 11:42:40 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts, nil
|
|
|
|
}
|
2023-11-06 12:46:54 +00:00
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// GetAllPosts retrieves all available posts using the provided post builder.
|
2023-11-06 12:46:54 +00:00
|
|
|
func (c *Client) GetAllPosts(postBuilder builder.PostsBuilder) ([]model.Post, error) {
|
2023-11-06 14:56:36 +00:00
|
|
|
// Retrieves all available posts using the provided post builder.
|
2023-11-06 12:46:54 +00:00
|
|
|
return c.GetNPosts(math.MaxInt, postBuilder)
|
|
|
|
}
|