From 322bb41a978da7b03b2dc4847cafce5af792fc17 Mon Sep 17 00:00:00 2001 From: soxx Date: Mon, 27 Nov 2023 22:37:36 +0100 Subject: [PATCH] refactor: basic implementation of favorites Signed-off-by: soxx --- example/highlevel/favorites.go | 41 ++++++++++++++++++++-------------- pkg/e621/builder/favorite.go | 2 ++ pkg/e621/client.go | 20 ++++++----------- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/example/highlevel/favorites.go b/example/highlevel/favorites.go index f41037b..f3cb1a4 100644 --- a/example/highlevel/favorites.go +++ b/example/highlevel/favorites.go @@ -2,6 +2,7 @@ package main import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "os" @@ -10,44 +11,50 @@ import ( func main() { client := e621.NewClient(os.Getenv("API_USER"), os.Getenv("API_KEY")) { - favorites, err := client.GetFavoritesForUser("selloo") + username := "selloo" + + favoritesBuilder := client.NewFavoritesBuilder().FromUsername(username) + posts, err := favoritesBuilder.Execute() if err != nil { log.Panic(err) } - posts, err := favorites.Execute() - if err != nil { - log.Panic(err) - } - log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL) + log.Printf("URL of favorite post 0 for user with name %s is: %s", username, posts[0].File.URL) } { - favoritesBuilder, _ := client.GetFavoritesForUser("selloo") - favorites, err := client.GetNFavoritesForUser(10, favoritesBuilder) + userID := 123456 + + favoritesBuilder := client.NewFavoritesBuilder().FromUserID(model.UserID(userID)) + posts, err := favoritesBuilder.Execute() if err != nil { log.Panic(err) } - - log.Println(len(favorites)) - + log.Printf("URL of favorite post 0 for user with id %d is: %s", userID, posts[0].File.URL) } { - favoritesBuilder, _ := client.GetFavoritesForUser("selloo") - favorites, err := client.GetAllFavoritesForUser(favoritesBuilder) + username := "selloo" + tags := []string{"fennec", "male", "solo"} + + favoritesBuilder := client.NewFavoritesBuilder().FromUsername(username).WithTags(tags) + posts, err := favoritesBuilder.Execute() if err != nil { log.Panic(err) } - log.Println(len(favorites)) + log.Printf("Found %d favorites in %s users favorites", len(posts), username) } { - favoritesWithTags := client.GetFavoritesForUserWithTags("selloo", "fennec male solo") - posts, err := favoritesWithTags.Execute() + userID := 123456 + tags := []string{"fennec", "male", "solo"} + limit := 5 + + favoritesBuilder := client.NewFavoritesBuilder().FromUserID(model.UserID(userID)).WithTags(tags).Limit(limit) + posts, err := favoritesBuilder.Execute() if err != nil { log.Panic(err) } - log.Printf("URL of favorite post 0 with tags is: %s", posts[0].File.URL) + log.Printf("returning %d posts with tags %s for user with id %d", len(posts), tags, userID) } } diff --git a/pkg/e621/builder/favorite.go b/pkg/e621/builder/favorite.go index 6d9d38e..bff5b65 100644 --- a/pkg/e621/builder/favorite.go +++ b/pkg/e621/builder/favorite.go @@ -82,6 +82,8 @@ func (g getFavorites) Execute() ([]model.Post, error) { if g.tags != "" { // TODO: implement with tags (channels) + } else { + return basicFavoritesBuilder.Execute() } return userFavorites, err diff --git a/pkg/e621/client.go b/pkg/e621/client.go index 0660cd8..9e43c90 100644 --- a/pkg/e621/client.go +++ b/pkg/e621/client.go @@ -1,23 +1,12 @@ package e621 import ( - "bytes" - "compress/gzip" - "encoding/csv" "fmt" - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" + "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/gocarina/gocsv" _ "github.com/joho/godotenv/autoload" "golang.org/x/time/rate" - "log" - "math" "net/http" - "regexp" - "strconv" - "strings" - "time" ) // Client is the main client for interacting with the e621 API. @@ -40,7 +29,11 @@ func NewClient(username string, apiKey string) Client { } } -// GetUserBuilder returns a UserBuilder instance for creating and executing requests to retrieve user information from the e621 API. +func (c *Client) NewFavoritesBuilder() builder.FavoritesBuilder { + return builder.NewGetFavoritesBuilder(c.RequestContext) +} + +/*// GetUserBuilder returns a UserBuilder instance for creating and executing requests to retrieve user information from the e621 API. // // Returns: // - builder.UserBuilder: An instance of the UserBuilder. @@ -376,3 +369,4 @@ func (c *Client) GetLatestPostsDBExportDataAsStruct() ([]*model.Post, error) { return post, nil } +*/