feat: added getting n & all favs & some fixes
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
4c2677dc58
commit
06ba87d1f2
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY"))
|
client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY"))
|
||||||
|
{
|
||||||
favorites, err := client.GetFavoritesForUser("selloo")
|
favorites, err := client.GetFavoritesForUser("selloo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
@ -18,12 +19,35 @@ func main() {
|
|||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL)
|
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")
|
favoritesWithTags := client.GetFavoritesForUserWithTags("selloo", "fennec male solo")
|
||||||
posts, err = favoritesWithTags.Execute()
|
posts, err := favoritesWithTags.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("URL of favorite post 0 with tags is: %s", posts[0].File.URL)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
6
go.mod
6
go.mod
@ -5,9 +5,5 @@ go 1.21.3
|
|||||||
require (
|
require (
|
||||||
github.com/jarcoal/httpmock v1.3.1
|
github.com/jarcoal/httpmock v1.3.1
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
)
|
golang.org/x/time v0.3.0
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
|
||||||
golang.org/x/time v0.3.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
type FavoritesBuilder interface {
|
type FavoritesBuilder interface {
|
||||||
SetUserID(userID model.UserID) FavoritesBuilder
|
SetUserID(userID model.UserID) FavoritesBuilder
|
||||||
SetLimit(limitFavorites int) FavoritesBuilder
|
SetLimit(limitFavorites int) FavoritesBuilder
|
||||||
|
Page(number int) FavoritesBuilder
|
||||||
Execute() ([]model.Post, error)
|
Execute() ([]model.Post, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,17 +26,22 @@ type getFavorites struct {
|
|||||||
requestContext model.RequestContext
|
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))
|
g.query["user_id"] = strconv.Itoa(int(userID))
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
|
func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
|
||||||
g.query["limit"] = strconv.Itoa(limitFavorites)
|
g.query["limit"] = strconv.Itoa(limitFavorites)
|
||||||
return g
|
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 {
|
if g.requestContext.RateLimiter != nil {
|
||||||
err := g.requestContext.RateLimiter.Wait(context.Background())
|
err := g.requestContext.RateLimiter.Wait(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
|
||||||
_ "github.com/joho/godotenv/autoload"
|
_ "github.com/joho/godotenv/autoload"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
"log"
|
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -57,6 +56,35 @@ func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder,
|
|||||||
return favoritesBuilder, nil
|
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 {
|
func (c *Client) GetFavoritesForUserWithTags(username string, tags string) builder.PostsBuilder {
|
||||||
favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags))
|
favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags))
|
||||||
return favoritesBuilder
|
return favoritesBuilder
|
||||||
@ -92,7 +120,6 @@ func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Pos
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
posts = append(posts, newPosts...)
|
posts = append(posts, newPosts...)
|
||||||
log.Printf("Post ID: %d | Post Size: %d", lastPostID, len(posts))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return posts, nil
|
return posts, nil
|
||||||
|
Reference in New Issue
Block a user