feat: added getting n & all favs & some fixes

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-11-06 15:26:47 +01:00
parent 4c2677dc58
commit 06ba87d1f2
4 changed files with 76 additions and 23 deletions

View File

@ -9,6 +9,7 @@ import (
func main() {
client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY"))
{
favorites, err := client.GetFavoritesForUser("selloo")
if err != nil {
log.Panic(err)
@ -18,12 +19,35 @@ func main() {
log.Panic(err)
}
log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL)
}
{
favoritesBuilder, _ := client.GetFavoritesForUser("selloo")
favorites, err := client.GetNFavoritesForUser(10, favoritesBuilder)
if err != nil {
log.Panic(err)
}
log.Println(len(favorites))
}
{
favoritesBuilder, _ := client.GetFavoritesForUser("selloo")
favorites, err := client.GetAllFavoritesForUser(favoritesBuilder)
if err != nil {
log.Panic(err)
}
log.Println(len(favorites))
}
{
favoritesWithTags := client.GetFavoritesForUserWithTags("selloo", "fennec male solo")
posts, err = favoritesWithTags.Execute()
posts, err := favoritesWithTags.Execute()
if err != nil {
log.Panic(err)
}
log.Printf("URL of favorite post 0 with tags is: %s", posts[0].File.URL)
}
}

6
go.mod
View File

@ -5,9 +5,5 @@ go 1.21.3
require (
github.com/jarcoal/httpmock v1.3.1
github.com/joho/godotenv v1.5.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/time v0.3.0
)

View File

@ -10,6 +10,7 @@ import (
type FavoritesBuilder interface {
SetUserID(userID model.UserID) FavoritesBuilder
SetLimit(limitFavorites int) FavoritesBuilder
Page(number int) FavoritesBuilder
Execute() ([]model.Post, error)
}
@ -25,17 +26,22 @@ type getFavorites struct {
requestContext model.RequestContext
}
func (g getFavorites) SetUserID(userID model.UserID) FavoritesBuilder {
func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder {
g.query["user_id"] = strconv.Itoa(int(userID))
return g
}
func (g getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
g.query["limit"] = strconv.Itoa(limitFavorites)
return g
}
func (g getFavorites) Execute() ([]model.Post, error) {
func (g *getFavorites) Page(pageNumber int) FavoritesBuilder {
g.query["page"] = strconv.Itoa(pageNumber)
return g
}
func (g *getFavorites) Execute() ([]model.Post, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {

View File

@ -7,7 +7,6 @@ import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
_ "github.com/joho/godotenv/autoload"
"golang.org/x/time/rate"
"log"
"math"
"net/http"
"strconv"
@ -57,6 +56,35 @@ func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder,
return favoritesBuilder, nil
}
func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) {
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
}
func (c *Client) GetAllFavoritesForUser(favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) {
return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder)
}
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
@ -92,7 +120,6 @@ func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Pos
break
}
posts = append(posts, newPosts...)
log.Printf("Post ID: %d | Post Size: %d", lastPostID, len(posts))
}
return posts, nil