Compare commits

...

2 Commits

Author SHA1 Message Date
322bb41a97 refactor: basic implementation of favorites
Signed-off-by: soxx <soxx@fenpa.ws>
2023-11-27 22:37:36 +01:00
be44a669d9 refactor: start rework of the high level API
Signed-off-by: soxx <soxx@fenpa.ws>
2023-11-27 08:12:58 +01:00
33 changed files with 306 additions and 173 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
"os" "os"
@ -10,44 +11,50 @@ import (
func main() { func main() {
client := e621.NewClient(os.Getenv("API_USER"), os.Getenv("API_KEY")) 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 { if err != nil {
log.Panic(err) log.Panic(err)
} }
posts, err := favorites.Execute() log.Printf("URL of favorite post 0 for user with name %s is: %s", username, posts[0].File.URL)
if err != nil {
log.Panic(err)
}
log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL)
} }
{ {
favoritesBuilder, _ := client.GetFavoritesForUser("selloo") userID := 123456
favorites, err := client.GetNFavoritesForUser(10, favoritesBuilder)
favoritesBuilder := client.NewFavoritesBuilder().FromUserID(model.UserID(userID))
posts, err := favoritesBuilder.Execute()
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
log.Printf("URL of favorite post 0 for user with id %d is: %s", userID, posts[0].File.URL)
log.Println(len(favorites))
} }
{ {
favoritesBuilder, _ := client.GetFavoritesForUser("selloo") username := "selloo"
favorites, err := client.GetAllFavoritesForUser(favoritesBuilder) tags := []string{"fennec", "male", "solo"}
favoritesBuilder := client.NewFavoritesBuilder().FromUsername(username).WithTags(tags)
posts, err := favoritesBuilder.Execute()
if err != nil { if err != nil {
log.Panic(err) 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") userID := 123456
posts, err := favoritesWithTags.Execute() 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 { if err != nil {
log.Panic(err) 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)
} }
} }

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"log" "log"
"net/http" "net/http"
@ -19,7 +19,7 @@ func main() {
} }
log.Println("Getting a list of DB Exports: ") log.Println("Getting a list of DB Exports: ")
getDBExportList := builder.NewGetDBExportListBuilder(requestContext) getDBExportList := basic.NewGetDBExportListBuilder(requestContext)
dbExportFiles, err := getDBExportList.Execute() dbExportFiles, err := getDBExportList.Execute()
if err != nil { if err != nil {
panic(err) panic(err)
@ -35,7 +35,7 @@ func main() {
exportFileName := dbExportFiles[0] exportFileName := dbExportFiles[0]
log.Println("Downloading DB export") log.Println("Downloading DB export")
log.Printf("File to download: %s", exportFileName) log.Printf("File to download: %s", exportFileName)
getDBExportFile := builder.NewGetDBExportFileBuilder(requestContext) getDBExportFile := basic.NewGetDBExportFileBuilder(requestContext)
rawFile, err := getDBExportFile.SetFile(exportFileName).Execute() rawFile, err := getDBExportFile.SetFile(exportFileName).Execute()

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
@ -23,7 +23,7 @@ func main() {
log.Println("Getting favorites API user: ") log.Println("Getting favorites API user: ")
// Call the GetFavorites function to retrieve favorite posts. // Call the GetFavorites function to retrieve favorite posts.
getFavorites := builder.NewGetFavoritesBuilder(requestContext) getFavorites := basic.NewGetFavoritesBuilder(requestContext)
posts, err := getFavorites.SetLimit(5).Execute() posts, err := getFavorites.SetLimit(5).Execute()
if err != nil { if err != nil {

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
@ -27,7 +27,7 @@ func main() {
noteID := 36957 // Replace with the desired note's ID. noteID := 36957 // Replace with the desired note's ID.
// Call the GetNote function to retrieve the specified note. // Call the GetNote function to retrieve the specified note.
getNote := builder.NewGetNoteBuilder(requestContext) getNote := basic.NewGetNoteBuilder(requestContext)
note, err := getNote.SetNoteID(noteID).Execute() note, err := getNote.SetNoteID(noteID).Execute()
if err != nil { if err != nil {
@ -42,7 +42,7 @@ func main() {
log.Println("Getting a list of notes: ") log.Println("Getting a list of notes: ")
// Call the GetNotes function to retrieve a list of notes based on the query parameters. // Call the GetNotes function to retrieve a list of notes based on the query parameters.
getNotes := builder.NewGetNotesBuilder(requestContext) getNotes := basic.NewGetNotesBuilder(requestContext)
notes, err := getNotes.SetLimit(5).Active(true).SearchInBody("furry").Execute() notes, err := getNotes.SetLimit(5).Active(true).SearchInBody("furry").Execute()
if err != nil { if err != nil {

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
@ -26,7 +26,7 @@ func main() {
poolID := 36957 // Replace with the desired pool's ID. poolID := 36957 // Replace with the desired pool's ID.
// Call the GetPool function to retrieve the specified pool. // Call the GetPool function to retrieve the specified pool.
getPool := builder.NewGetPoolBuilder(requestContext) getPool := basic.NewGetPoolBuilder(requestContext)
pool, err := getPool.ID(poolID).Execute() pool, err := getPool.ID(poolID).Execute()
if err != nil { if err != nil {
@ -41,7 +41,7 @@ func main() {
log.Println("Getting a list of pools: ") log.Println("Getting a list of pools: ")
// Call the GetPools function to retrieve a list of pools based on the query parameters. // Call the GetPools function to retrieve a list of pools based on the query parameters.
getPools := builder.NewGetPoolsBuilder(requestContext) getPools := basic.NewGetPoolsBuilder(requestContext)
pools, err := getPools.SetLimit(5).Active(true).SearchDescription("mass effect").Execute() pools, err := getPools.SetLimit(5).Active(true).SearchDescription("mass effect").Execute()
if err != nil { if err != nil {

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
@ -26,7 +26,7 @@ func main() {
var postID model.PostID = 4353480 // Replace with the desired post's ID. var postID model.PostID = 4353480 // Replace with the desired post's ID.
// Call the GetPost function to retrieve the specified post. // Call the GetPost function to retrieve the specified post.
getPost := builder.NewGetPostBuilder(requestContext) getPost := basic.NewGetPostBuilder(requestContext)
post, err := getPost.SetPostID(postID).Execute() post, err := getPost.SetPostID(postID).Execute()
if err != nil { if err != nil {
@ -41,7 +41,7 @@ func main() {
log.Println("Getting a list of posts: ") log.Println("Getting a list of posts: ")
// Call the GetPosts function to retrieve a list of posts based on the query parameters. // Call the GetPosts function to retrieve a list of posts based on the query parameters.
getPosts := builder.NewGetPostsBuilder(requestContext) getPosts := basic.NewGetPostsBuilder(requestContext)
posts, err := getPosts.SetLimit(5).Execute() posts, err := getPosts.SetLimit(5).Execute()
if err != nil { if err != nil {

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
@ -26,7 +26,7 @@ func main() {
tagID := 1530 // Replace with the desired tag's ID. tagID := 1530 // Replace with the desired tag's ID.
// Call the GetTag function to retrieve the specified tag. // Call the GetTag function to retrieve the specified tag.
getTag := builder.NewGetTagBuilder(requestContext) getTag := basic.NewGetTagBuilder(requestContext)
tag, err := getTag.SetTagID(tagID).Execute() tag, err := getTag.SetTagID(tagID).Execute()
if err != nil { if err != nil {
@ -41,7 +41,7 @@ func main() {
log.Println("Getting a list of tags: ") log.Println("Getting a list of tags: ")
// Call the GetTags function to retrieve a list of tags based on the query parameters. // Call the GetTags function to retrieve a list of tags based on the query parameters.
getTags := builder.NewGetTagsBuilder(requestContext) getTags := basic.NewGetTagsBuilder(requestContext)
tagList, err := getTags.SetLimit(5).Artist(false).Execute() tagList, err := getTags.SetLimit(5).Artist(false).Execute()
if err != nil { if err != nil {

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"log" "log"
@ -26,7 +26,7 @@ func main() {
username := "selloo" // Replace with the desired username. username := "selloo" // Replace with the desired username.
// Call the GetUser function to retrieve the specified user. // Call the GetUser function to retrieve the specified user.
userBuilder := builder.NewGetUserBuilder(requestContext) userBuilder := basic.NewGetUserBuilder(requestContext)
user, err := userBuilder.SetUsername(username).Execute() user, err := userBuilder.SetUsername(username).Execute()
if err != nil { if err != nil {
@ -41,7 +41,7 @@ func main() {
log.Println("Getting a list of users: ") log.Println("Getting a list of users: ")
// Call the GetUsers function to retrieve a list of users based on the query parameters. // Call the GetUsers function to retrieve a list of users based on the query parameters.
usersBuilder := builder.NewGetUsersBuilder(requestContext) usersBuilder := basic.NewGetUsersBuilder(requestContext)
userList, err := usersBuilder.SetLimit(5).Execute() userList, err := usersBuilder.SetLimit(5).Execute()
if err != nil { if err != nil {

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"

View File

@ -0,0 +1,95 @@
package basic
import (
"context"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// FavoritesBuilder represents a builder for constructing queries to retrieve user's favorite posts.
type FavoritesBuilder interface {
// SetUserID sets the user ID for the query.
SetUserID(userID model.UserID) FavoritesBuilder
// SetLimit sets the maximum number of favorite posts to retrieve.
SetLimit(limitFavorites int) FavoritesBuilder
// Page sets the page number for paginated results.
Page(pageNumber int) FavoritesBuilder
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
Execute() ([]model.Post, error)
}
// NewGetFavoritesBuilder creates a new instance of FavoritesBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - FavoritesBuilder: An instance of the FavoritesBuilder interface.
func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder {
return &getFavorites{
requestContext: requestContext,
query: make(map[string]string),
}
}
// getFavorites is an implementation of the FavoritesBuilder interface.
type getFavorites struct {
query map[string]string
requestContext model.RequestContext
}
// SetUserID sets the user ID for the query.
//
// Parameters:
// - userID: The ID of the user whose favorite posts are to be retrieved.
//
// Returns:
// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining.
func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder {
g.query["user_id"] = strconv.Itoa(int(userID))
return g
}
// SetLimit sets the maximum number of favorite posts to retrieve.
//
// Parameters:
// - limitFavorites: The maximum number of favorite posts to retrieve.
//
// Returns:
// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining.
func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
g.query["limit"] = strconv.Itoa(limitFavorites)
return g
}
// Page sets the page number for paginated results.
//
// Parameters:
// - pageNumber: The page number for paginated results.
//
// Returns:
// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining.
func (g *getFavorites) Page(pageNumber int) FavoritesBuilder {
g.query["page"] = strconv.Itoa(pageNumber)
return g
}
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
//
// Returns:
// - []model.Post: A slice of favorite posts.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getFavorites) Execute() ([]model.Post, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
favorites, err := endpoints.GetFavorites(g.requestContext, g.query)
if err != nil {
return nil, err
}
return favorites, nil
}

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package builder package basic
import ( import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"

View File

@ -1,95 +1,132 @@
package builder package builder
import ( import (
"context" "fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"strconv" "strings"
) )
// FavoritesBuilder represents a builder for constructing queries to retrieve user's favorite posts.
type FavoritesBuilder interface { type FavoritesBuilder interface {
// SetUserID sets the user ID for the query. FromUsername(username string) FavoritesBuilder
SetUserID(userID model.UserID) FavoritesBuilder FromUserID(userID model.UserID) FavoritesBuilder
// SetLimit sets the maximum number of favorite posts to retrieve. WithTags(tags []string) FavoritesBuilder
SetLimit(limitFavorites int) FavoritesBuilder Limit(amount int) FavoritesBuilder
// Page sets the page number for paginated results.
Page(pageNumber int) FavoritesBuilder
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
Execute() ([]model.Post, error) Execute() ([]model.Post, error)
} }
// NewGetFavoritesBuilder creates a new instance of FavoritesBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - FavoritesBuilder: An instance of the FavoritesBuilder interface.
func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder { func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder {
return &getFavorites{ return &getFavorites{
requestContext: requestContext, requestContext: requestContext,
query: make(map[string]string),
} }
} }
// getFavorites is an implementation of the FavoritesBuilder interface.
type getFavorites struct { type getFavorites struct {
query map[string]string
requestContext model.RequestContext requestContext model.RequestContext
user model.User
tags string
limit int
error error
} }
// SetUserID sets the user ID for the query. func (g *getFavorites) FromUsername(username string) FavoritesBuilder {
// user := model.User{Name: username}
// Parameters: g.user = user
// - userID: The ID of the user whose favorite posts are to be retrieved.
//
// Returns:
// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining.
func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder {
g.query["user_id"] = strconv.Itoa(int(userID))
return g return g
} }
// SetLimit sets the maximum number of favorite posts to retrieve. func (g *getFavorites) FromUserID(userID model.UserID) FavoritesBuilder {
// user := model.User{ID: userID}
// Parameters: g.user = user
// - limitFavorites: The maximum number of favorite posts to retrieve.
//
// Returns:
// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining.
func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
g.query["limit"] = strconv.Itoa(limitFavorites)
return g return g
} }
// Page sets the page number for paginated results. func (g *getFavorites) WithTags(tagList []string) FavoritesBuilder {
// tags := strings.Join(tagList, ",")
// Parameters: g.tags = tags
// - pageNumber: The page number for paginated results.
//
// Returns:
// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining.
func (g *getFavorites) Page(pageNumber int) FavoritesBuilder {
g.query["page"] = strconv.Itoa(pageNumber)
return g return g
} }
// Execute sends the constructed query and returns a slice of favorite posts and an error if any. func (g *getFavorites) Limit(amount int) FavoritesBuilder {
// g.limit = amount
// Returns: return g
// - []model.Post: A slice of favorite posts. }
// - error: An error, if any, encountered during the API request or response handling.
func (g *getFavorites) Execute() ([]model.Post, error) { func (g getFavorites) Execute() ([]model.Post, error) {
if g.requestContext.RateLimiter != nil { var err error
err := g.requestContext.RateLimiter.Wait(context.Background()) basicFavoritesBuilder := basic.NewGetFavoritesBuilder(g.requestContext)
var userFavorites []model.Post
if g.user.ID == 0 && g.user.Name == "" {
return nil, fmt.Errorf("no UserID or UserName given")
}
if g.user.Name != "" {
g.user, err = basic.NewGetUserBuilder(g.requestContext).SetUsername(g.user.Name).Execute()
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
favorites, err := endpoints.GetFavorites(g.requestContext, g.query)
if err != nil { if g.user.ID != 0 {
return nil, err basicFavoritesBuilder.SetUserID(g.user.ID)
} }
if g.limit != 0 {
basicFavoritesBuilder.SetLimit(g.limit)
}
if g.tags == "" {
userFavorites, err = basicFavoritesBuilder.Execute()
}
if g.tags != "" {
// TODO: implement with tags (channels)
} else {
return basicFavoritesBuilder.Execute()
}
return userFavorites, err
}
/*
// GetNFavoritesForUser retrieves a specified number of favorites for a user.
func (c *CLient) GetNFavoritesForUser(n int, favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) {
// Retrieves a specified number of favorite posts for a user.
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 return favorites, nil
} }
// GetAllFavoritesForUser retrieves all favorites for a user.
func (c *Client) GetAllFavoritesForUser(favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) {
// Retrieves all favorite posts for a user.
return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder)
}
// GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags.
func (c *Client) GetFavoritesForUserWithTags(username string, tags string) basic.PostsBuilder {
// Returns a posts builder for a user's favorites with specific tags.
favoritesBuilder := basic.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags))
return favoritesBuilder
}
*/

View File

@ -1,23 +1,12 @@
package e621 package e621
import ( import (
"bytes"
"compress/gzip"
"encoding/csv"
"fmt" "fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" "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/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"github.com/gocarina/gocsv"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"log"
"math"
"net/http" "net/http"
"regexp"
"strconv"
"strings"
"time"
) )
// Client is the main client for interacting with the e621 API. // Client is the main client for interacting with the e621 API.
@ -40,92 +29,96 @@ 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: // Returns:
// - builder.UserBuilder: An instance of the UserBuilder. // - builder.UserBuilder: An instance of the UserBuilder.
func (c *Client) GetUserBuilder() builder.UserBuilder { func (c *Client) GetUserBuilder() basic.UserBuilder {
return builder.NewGetUserBuilder(c.RequestContext) return basic.NewGetUserBuilder(c.RequestContext)
} }
// GetUsersBuilder returns a UsersBuilder instance for creating and executing requests to retrieve multiple users' information from the e621 API. // GetUsersBuilder returns a UsersBuilder instance for creating and executing requests to retrieve multiple users' information from the e621 API.
// //
// Returns: // Returns:
// - builder.UsersBuilder: An instance of the UsersBuilder. // - builder.UsersBuilder: An instance of the UsersBuilder.
func (c *Client) GetUsersBuilder() builder.UsersBuilder { func (c *Client) GetUsersBuilder() basic.UsersBuilder {
return builder.NewGetUsersBuilder(c.RequestContext) return basic.NewGetUsersBuilder(c.RequestContext)
} }
// GetFavoritesBuilder returns a FavoritesBuilder instance for creating and executing requests to retrieve a user's favorite posts from the e621 API. // GetFavoritesBuilder returns a FavoritesBuilder instance for creating and executing requests to retrieve a user's favorite posts from the e621 API.
// //
// Returns: // Returns:
// - builder.FavoritesBuilder: An instance of the FavoritesBuilder. // - builder.FavoritesBuilder: An instance of the FavoritesBuilder.
func (c *Client) GetFavoritesBuilder() builder.FavoritesBuilder { func (c *Client) GetFavoritesBuilder() basic.FavoritesBuilder {
return builder.NewGetFavoritesBuilder(c.RequestContext) return basic.NewGetFavoritesBuilder(c.RequestContext)
} }
// GetPostBuilder returns a PostBuilder instance for creating and executing requests to retrieve post information from the e621 API. // GetPostBuilder returns a PostBuilder instance for creating and executing requests to retrieve post information from the e621 API.
// //
// Returns: // Returns:
// - builder.PostBuilder: An instance of the PostBuilder. // - builder.PostBuilder: An instance of the PostBuilder.
func (c *Client) GetPostBuilder() builder.PostBuilder { func (c *Client) GetPostBuilder() basic.PostBuilder {
return builder.NewGetPostBuilder(c.RequestContext) return basic.NewGetPostBuilder(c.RequestContext)
} }
// GetPostsBuilder returns a PostsBuilder instance for creating and executing requests to retrieve multiple posts' information from the e621 API. // GetPostsBuilder returns a PostsBuilder instance for creating and executing requests to retrieve multiple posts' information from the e621 API.
// //
// Returns: // Returns:
// - builder.PostsBuilder: An instance of the PostsBuilder. // - builder.PostsBuilder: An instance of the PostsBuilder.
func (c *Client) GetPostsBuilder() builder.PostsBuilder { func (c *Client) GetPostsBuilder() basic.PostsBuilder {
return builder.NewGetPostsBuilder(c.RequestContext) return basic.NewGetPostsBuilder(c.RequestContext)
} }
// GetNoteBuilder returns a NoteBuilder instance for creating and executing requests to retrieve note information from the e621 API. // GetNoteBuilder returns a NoteBuilder instance for creating and executing requests to retrieve note information from the e621 API.
// //
// Returns: // Returns:
// - builder.NoteBuilder: An instance of the NoteBuilder. // - builder.NoteBuilder: An instance of the NoteBuilder.
func (c *Client) GetNoteBuilder() builder.NoteBuilder { func (c *Client) GetNoteBuilder() basic.NoteBuilder {
return builder.NewGetNoteBuilder(c.RequestContext) return basic.NewGetNoteBuilder(c.RequestContext)
} }
// GetNotesBuilder returns a NotesBuilder instance for creating and executing requests to retrieve multiple notes' information from the e621 API. // GetNotesBuilder returns a NotesBuilder instance for creating and executing requests to retrieve multiple notes' information from the e621 API.
// //
// Returns: // Returns:
// - builder.NotesBuilder: An instance of the NotesBuilder. // - builder.NotesBuilder: An instance of the NotesBuilder.
func (c *Client) GetNotesBuilder() builder.NotesBuilder { func (c *Client) GetNotesBuilder() basic.NotesBuilder {
return builder.NewGetNotesBuilder(c.RequestContext) return basic.NewGetNotesBuilder(c.RequestContext)
} }
// GetPoolBuilder returns a PoolBuilder instance for creating and executing requests to retrieve pool information from the e621 API. // GetPoolBuilder returns a PoolBuilder instance for creating and executing requests to retrieve pool information from the e621 API.
// //
// Returns: // Returns:
// - builder.PoolBuilder: An instance of the PoolBuilder. // - builder.PoolBuilder: An instance of the PoolBuilder.
func (c *Client) GetPoolBuilder() builder.PoolBuilder { func (c *Client) GetPoolBuilder() basic.PoolBuilder {
return builder.NewGetPoolBuilder(c.RequestContext) return basic.NewGetPoolBuilder(c.RequestContext)
} }
// GetPoolsBuilder returns a PoolsBuilder instance for creating and executing requests to retrieve multiple pools' information from the e621 API. // GetPoolsBuilder returns a PoolsBuilder instance for creating and executing requests to retrieve multiple pools' information from the e621 API.
// //
// Returns: // Returns:
// - builder.PoolsBuilder: An instance of the PoolsBuilder. // - builder.PoolsBuilder: An instance of the PoolsBuilder.
func (c *Client) GetPoolsBuilder() builder.PoolsBuilder { func (c *Client) GetPoolsBuilder() basic.PoolsBuilder {
return builder.NewGetPoolsBuilder(c.RequestContext) return basic.NewGetPoolsBuilder(c.RequestContext)
} }
// GetTagBuilder returns a TagBuilder instance for creating and executing requests to retrieve tag information from the e621 API. // GetTagBuilder returns a TagBuilder instance for creating and executing requests to retrieve tag information from the e621 API.
// //
// Returns: // Returns:
// - builder.TagBuilder: An instance of the TagBuilder. // - builder.TagBuilder: An instance of the TagBuilder.
func (c *Client) GetTagBuilder() builder.TagBuilder { func (c *Client) GetTagBuilder() basic.TagBuilder {
return builder.NewGetTagBuilder(c.RequestContext) return basic.NewGetTagBuilder(c.RequestContext)
} }
// GetTagsBuilder returns a TagsBuilder instance for creating and executing requests to retrieve multiple tags' information from the e621 API. // GetTagsBuilder returns a TagsBuilder instance for creating and executing requests to retrieve multiple tags' information from the e621 API.
// //
// Returns: // Returns:
// - builder.TagsBuilder: An instance of the TagsBuilder. // - builder.TagsBuilder: An instance of the TagsBuilder.
func (c *Client) GetTagsBuilder() builder.TagsBuilder { func (c *Client) GetTagsBuilder() basic.TagsBuilder {
return builder.NewGetTagsBuilder(c.RequestContext) return basic.NewGetTagsBuilder(c.RequestContext)
} }
// SetHost sets the API host for the client. // SetHost sets the API host for the client.
@ -143,30 +136,30 @@ func (c *Client) SetAgentName(userAgent string) *Client {
} }
// GetUserByName returns a user builder for a given username. // GetUserByName returns a user builder for a given username.
func (c *Client) GetUserByName(username string) builder.UserBuilder { func (c *Client) GetUserByName(username string) basic.UserBuilder {
// Returns a user builder for the specified username. // Returns a user builder for the specified username.
return builder.NewGetUserBuilder(c.RequestContext).SetUsername(username) return basic.NewGetUserBuilder(c.RequestContext).SetUsername(username)
} }
// GetUserByID returns a user builder for a given user ID. // GetUserByID returns a user builder for a given user ID.
func (c *Client) GetUserByID(userID model.UserID) builder.UserBuilder { func (c *Client) GetUserByID(userID model.UserID) basic.UserBuilder {
// Returns a user builder for the specified user ID. // Returns a user builder for the specified user ID.
return builder.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10)) return basic.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10))
} }
// GetFavoritesForUser returns a favorites builder for a given username. // GetFavoritesForUser returns a favorites builder for a given username.
func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, error) { func (c *Client) GetFavoritesForUser(username string) (basic.FavoritesBuilder, error) {
// Returns a favorites builder for the specified username. // Returns a favorites builder for the specified username.
user, err := builder.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute() user, err := basic.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute()
if err != nil { if err != nil {
return nil, err return nil, err
} }
favoritesBuilder := builder.NewGetFavoritesBuilder(c.RequestContext).SetUserID(user.ID) favoritesBuilder := basic.NewGetFavoritesBuilder(c.RequestContext).SetUserID(user.ID)
return favoritesBuilder, nil return favoritesBuilder, nil
} }
// GetNFavoritesForUser retrieves a specified number of favorites for a user. // GetNFavoritesForUser retrieves a specified number of favorites for a user.
func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) { func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) {
// Retrieves a specified number of favorite posts for a user. // Retrieves a specified number of favorite posts for a user.
if n < utils.E621_MAX_POST_COUNT { if n < utils.E621_MAX_POST_COUNT {
favoriteBuilder.SetLimit(n) favoriteBuilder.SetLimit(n)
@ -192,32 +185,32 @@ func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBu
} }
// GetAllFavoritesForUser retrieves all favorites for a user. // GetAllFavoritesForUser retrieves all favorites for a user.
func (c *Client) GetAllFavoritesForUser(favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) { func (c *Client) GetAllFavoritesForUser(favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) {
// Retrieves all favorite posts for a user. // Retrieves all favorite posts for a user.
return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder) return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder)
} }
// GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags. // GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags.
func (c *Client) GetFavoritesForUserWithTags(username string, tags string) builder.PostsBuilder { func (c *Client) GetFavoritesForUserWithTags(username string, tags string) basic.PostsBuilder {
// Returns a posts builder for a user's favorites with specific tags. // Returns a posts builder for a user's favorites with specific tags.
favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags)) favoritesBuilder := basic.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags))
return favoritesBuilder return favoritesBuilder
} }
// GetPostByID returns a post builder for a specific post ID. // GetPostByID returns a post builder for a specific post ID.
func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder { func (c *Client) GetPostByID(id model.PostID) basic.PostBuilder {
// Returns a post builder for a specific post ID. // Returns a post builder for a specific post ID.
return builder.NewGetPostBuilder(c.RequestContext).SetPostID(id) return basic.NewGetPostBuilder(c.RequestContext).SetPostID(id)
} }
// GetPosts returns a posts builder for general post queries. // GetPosts returns a posts builder for general post queries.
func (c *Client) GetPosts() builder.PostsBuilder { func (c *Client) GetPosts() basic.PostsBuilder {
// Returns a posts builder for general post queries. // Returns a posts builder for general post queries.
return builder.NewGetPostsBuilder(c.RequestContext) return basic.NewGetPostsBuilder(c.RequestContext)
} }
// GetNPosts retrieves a specified number of posts. // GetNPosts retrieves a specified number of posts.
func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Post, error) { func (c *Client) GetNPosts(n int, postBuilder basic.PostsBuilder) ([]model.Post, error) {
// Retrieves a specified number of posts using the provided post builder. // Retrieves a specified number of posts using the provided post builder.
if n < utils.E621_MAX_POST_COUNT { if n < utils.E621_MAX_POST_COUNT {
postBuilder.SetLimit(n) postBuilder.SetLimit(n)
@ -246,18 +239,18 @@ func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Pos
} }
// GetAllPosts retrieves all available posts using the provided post builder. // GetAllPosts retrieves all available posts using the provided post builder.
func (c *Client) GetAllPosts(postBuilder builder.PostsBuilder) ([]model.Post, error) { func (c *Client) GetAllPosts(postBuilder basic.PostsBuilder) ([]model.Post, error) {
// Retrieves all available posts using the provided post builder. // Retrieves all available posts using the provided post builder.
return c.GetNPosts(math.MaxInt, postBuilder) return c.GetNPosts(math.MaxInt, postBuilder)
} }
func (c *Client) GetLatestPoolsDBExportDataAsBytes() (string, []byte, error) { func (c *Client) GetLatestPoolsDBExportDataAsBytes() (string, []byte, error) {
dbExportFileNameList, err := builder.NewGetDBExportListBuilder(c.RequestContext).Execute() dbExportFileNameList, err := basic.NewGetDBExportListBuilder(c.RequestContext).Execute()
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
getDBExportFile := builder.NewGetDBExportFileBuilder(c.RequestContext) getDBExportFile := basic.NewGetDBExportFileBuilder(c.RequestContext)
filter := func(s string) bool { return strings.HasPrefix(s, "pools") } filter := func(s string) bool { return strings.HasPrefix(s, "pools") }
filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter) filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter)
@ -323,12 +316,12 @@ func (c *Client) GetLatestPoolsDBExportDataAsStruct() ([]*model.Pool, error) {
func (c *Client) GetLatestPostsDBExportDataAsBytes() (string, []byte, error) { func (c *Client) GetLatestPostsDBExportDataAsBytes() (string, []byte, error) {
log.Println("Please wait while the download is in progress for the post export... (file over 1GB) ") log.Println("Please wait while the download is in progress for the post export... (file over 1GB) ")
dbExportFileNameList, err := builder.NewGetDBExportListBuilder(c.RequestContext).Execute() dbExportFileNameList, err := basic.NewGetDBExportListBuilder(c.RequestContext).Execute()
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
getDBExportFile := builder.NewGetDBExportFileBuilder(c.RequestContext) getDBExportFile := basic.NewGetDBExportFileBuilder(c.RequestContext)
filter := func(s string) bool { return strings.HasPrefix(s, "posts") } filter := func(s string) bool { return strings.HasPrefix(s, "posts") }
filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter) filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter)
@ -376,3 +369,4 @@ func (c *Client) GetLatestPostsDBExportDataAsStruct() ([]*model.Post, error) {
return post, nil return post, nil
} }
*/