Compare commits

..

8 Commits

Author SHA1 Message Date
8c662f78fd docs: added more & unified documentation format
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:41:16 +01:00
9cee99ecb5 docs: added more & unified documentation format
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:37:44 +01:00
c6be78419c refactor: added docs & limit types
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:18:51 +01:00
208b9a3baa refactor: added more accurate types
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:05:21 +01:00
95e1db6ce6 refactor: removed unused files
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:02:47 +01:00
9669eb8ddf refactor: removed code duplication
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:02:15 +01:00
8e76fa709d feat: added partial implementation for low, mid and high level api for db exports
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 13:21:13 +01:00
6124a3fc61 feat: added low level API for Database exports
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-13 11:45:39 +01:00
34 changed files with 1109 additions and 591 deletions

View File

@ -0,0 +1,46 @@
package main
import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621"
_ "github.com/joho/godotenv/autoload"
"log"
"os"
)
func main() {
client := e621.NewClient(os.Getenv("API_USER"), os.Getenv("API_KEY"))
{
fileName, _, err := client.GetLatestPoolsDBExportDataAsBytes()
if err != nil {
panic(err)
}
log.Println(fileName)
}
{
latestDBPoolExport, err := client.GetLatestPoolsDBExportDataAsStruct()
if err != nil {
panic(err)
}
log.Println(latestDBPoolExport[0])
}
{
fileName, _, err := client.GetLatestPostsDBExportDataAsBytes()
if err != nil {
panic(err)
}
log.Println(fileName)
}
{
latestDBPoolExport, err := client.GetLatestPostsDBExportDataAsStruct()
if err != nil {
panic(err)
}
log.Println(latestDBPoolExport[0])
}
}

View File

@ -0,0 +1,50 @@
package main
import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"log"
"net/http"
"os"
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: os.Getenv("API_USER"), // Replace with your username
APIKey: os.Getenv("API_KEY"), // Replace with your API key
}
log.Println("Getting a list of DB Exports: ")
dbExportFiles, err := endpoints.GetDBExportList(requestContext)
if err != nil {
panic(err)
}
log.Printf("%d files found", len(dbExportFiles))
for _, v := range dbExportFiles {
log.Printf("File found: %s", v)
}
exportFileName := dbExportFiles[0]
log.Println("Downloading DB export")
log.Printf("File to download: %s", exportFileName)
rawFile, err := endpoints.GetDBExportFile(requestContext, exportFileName)
file, err := os.Create(exportFileName)
if err != nil {
panic(err)
}
defer file.Close()
err = os.WriteFile(exportFileName, rawFile, 0644)
if err != nil {
panic(err)
}
log.Printf("File %s downloaded", exportFileName)
}

View File

@ -0,0 +1,55 @@
package main
import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"log"
"net/http"
"os"
)
func main() {
// Define the request context with essential information.
requestContext := model.RequestContext{
Client: http.Client{},
Host: "https://e621.net",
UserAgent: "Go-e621-SDK (@username)",
Username: os.Getenv("API_USER"), // Replace with your username
APIKey: os.Getenv("API_KEY"), // Replace with your API key
}
log.Println("Getting a list of DB Exports: ")
getDBExportList := builder.NewGetDBExportListBuilder(requestContext)
dbExportFiles, err := getDBExportList.Execute()
if err != nil {
panic(err)
}
log.Printf("%d files found", len(dbExportFiles))
for _, v := range dbExportFiles {
log.Printf("File found: %s", v)
}
log.Println(dbExportFiles)
exportFileName := dbExportFiles[0]
log.Println("Downloading DB export")
log.Printf("File to download: %s", exportFileName)
getDBExportFile := builder.NewGetDBExportFileBuilder(requestContext)
rawFile, err := getDBExportFile.SetFile(exportFileName).Execute()
file, err := os.Create(exportFileName)
if err != nil {
panic(err)
}
defer file.Close()
err = os.WriteFile(exportFileName, rawFile, 0644)
if err != nil {
panic(err)
}
log.Printf("File %s downloaded", exportFileName)
}

5
go.mod
View File

@ -8,4 +8,7 @@ require (
golang.org/x/time v0.3.0
)
require golang.org/x/net v0.18.0 // indirect
require (
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d // indirect
golang.org/x/net v0.18.0 // indirect
)

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d h1:KbPOUXFUDJxwZ04vbmDOc3yuruGvVO+LOa7cVER3yWw=
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=

View File

@ -1,4 +0,0 @@
package main
func main() {
}

View File

@ -0,0 +1,54 @@
package builder
import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
)
// DBExportFileBuilder defines an interface for building and executing requests to retrieve a specific exported database file.
type DBExportFileBuilder interface {
// SetFile sets the name of the file to be retrieved.
SetFile(fileName string) DBExportFileBuilder
// Execute sends the request and returns the content of the exported database file or an error if the request fails.
Execute() ([]byte, error)
}
// NewGetDBExportFileBuilder creates a new instance of DBExportFileBuilder.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - DBExportFileBuilder: An instance of the DBExportFileBuilder interface.
func NewGetDBExportFileBuilder(requestContext model.RequestContext) DBExportFileBuilder {
return &getDBExportFile{
requestContext: requestContext,
}
}
// getDBExportFile is an implementation of the DBExportFileBuilder interface.
type getDBExportFile struct {
requestContext model.RequestContext
fileName string
}
// SetFile sets the name of the file to be retrieved.
//
// Parameters:
// - fileName: The name of the exported database file.
//
// Returns:
// - DBExportFileBuilder: The instance of the DBExportFileBuilder for method chaining.
func (g *getDBExportFile) SetFile(fileName string) DBExportFileBuilder {
g.fileName = fileName
return g
}
// Execute sends the API request to retrieve the content of a specific exported database file.
//
// Returns:
// - []byte: The content of the exported database file.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getDBExportFile) Execute() ([]byte, error) {
return endpoints.GetDBExportFile(g.requestContext, g.fileName)
}

View File

@ -0,0 +1,39 @@
package builder
import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
)
// DBExportListBuilder defines an interface for building and executing requests to retrieve a list of exported databases.
type DBExportListBuilder interface {
// Execute sends the request and returns a list of exported databases or an error if the request fails.
Execute() ([]string, error)
}
// NewGetDBExportListBuilder creates a new instance of DBExportListBuilder.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - DBExportListBuilder: An instance of the DBExportListBuilder interface.
func NewGetDBExportListBuilder(requestContext model.RequestContext) DBExportListBuilder {
return &getDBExportList{
requestContext: requestContext,
}
}
// getDBExportList is an implementation of the DBExportListBuilder interface.
type getDBExportList struct {
requestContext model.RequestContext
}
// Execute sends the API request to retrieve a list of exported databases.
//
// Returns:
// - []string: A list of exported databases.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getDBExportList) Execute() ([]string, error) {
return endpoints.GetDBExportList(g.requestContext)
}

View File

@ -19,7 +19,13 @@ type FavoritesBuilder interface {
Execute() ([]model.Post, error)
}
// NewGetFavoritesBuilder creates a new FavoritesBuilder with the provided RequestContext.
// 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,
@ -27,30 +33,53 @@ func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilde
}
}
// 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())

View File

@ -15,23 +15,40 @@ type NoteBuilder interface {
Execute() (*model.Note, error)
}
// NewGetNoteBuilder creates a new NoteBuilder with the provided RequestContext.
// NewGetNoteBuilder creates a new instance of NoteBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - NoteBuilder: An instance of the NoteBuilder interface.
func NewGetNoteBuilder(requestContext model.RequestContext) NoteBuilder {
return &getNote{requestContext: requestContext}
}
// getNote is an implementation of the NoteBuilder interface.
type getNote struct {
requestContext model.RequestContext
noteID int
}
// SetNoteID sets the note ID for the query.
//
// Parameters:
// - noteID: The ID of the note to be retrieved.
//
// Returns:
// - NoteBuilder: The instance of the NoteBuilder for method chaining.
func (g *getNote) SetNoteID(noteID int) NoteBuilder {
g.noteID = noteID
return g
}
// Execute sends the constructed query and returns the requested note and an error, if any.
//
// Returns:
// - *model.Note: The requested note.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getNote) Execute() (*model.Note, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -27,7 +27,13 @@ type NotesBuilder interface {
Execute() ([]model.Note, error)
}
// NewGetNotesBuilder creates a new NotesBuilder with the provided RequestContext.
// NewGetNotesBuilder creates a new instance of NotesBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - NotesBuilder: An instance of the NotesBuilder interface.
func NewGetNotesBuilder(requestContext model.RequestContext) NotesBuilder {
return &getNotes{
requestContext: requestContext,
@ -35,54 +41,101 @@ func NewGetNotesBuilder(requestContext model.RequestContext) NotesBuilder {
}
}
// getNotes is an implementation of the NotesBuilder interface.
type getNotes struct {
requestContext model.RequestContext
query map[string]string
}
// SearchInBody sets the query to search for notes containing a specific text in their body.
//
// Parameters:
// - body: The text to search for in the notes' body.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) SearchInBody(body string) NotesBuilder {
g.query["search[body_matches]"] = body
return g
}
// NoteID sets the query to search for a note with a specific note ID.
//
// Parameters:
// - noteID: The ID of the note to be retrieved.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) NoteID(noteID int) NotesBuilder {
g.query["search[post_id]"] = strconv.Itoa(noteID)
return g
}
// SearchTags sets the query to search for notes containing specific tags.
//
// Parameters:
// - tags: The tags to search for in the notes.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) SearchTags(tags string) NotesBuilder {
g.query["search[post_tags_match]"] = tags
return g
}
// SearchCreatorID sets the query to search for notes created by a specific user (by their user ID).
//
// Parameters:
// - creatorID: The ID of the user who created the notes.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) SearchCreatorID(creatorID int) NotesBuilder {
g.query["search[creator_id]"] = strconv.Itoa(creatorID)
return g
}
// SearchCreatorName sets the query to search for notes created by a specific user (by their username).
//
// Parameters:
// - creatorName: The username of the user who created the notes.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) SearchCreatorName(creatorName string) NotesBuilder {
g.query["search[creator_name]"] = creatorName
return g
}
// Active sets whether to search for active or inactive notes.
//
// Parameters:
// - isActive: A boolean indicating whether to search for active notes (true) or inactive notes (false).
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) Active(isActive bool) NotesBuilder {
g.query["search[is_active]"] = strconv.FormatBool(isActive)
return g
}
// SetLimit sets the maximum number of notes to retrieve.
//
// Parameters:
// - limitNotes: The maximum number of notes to retrieve.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
func (g *getNotes) SetLimit(limitNotes int) NotesBuilder {
g.query["limit"] = strconv.Itoa(limitNotes)
return g
}
// Execute sends the constructed query and returns a slice of notes and an error, if any.
//
// Returns:
// - []model.Note: A slice of notes.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getNotes) Execute() ([]model.Note, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -15,23 +15,40 @@ type PoolBuilder interface {
Execute() (model.Pool, error)
}
// NewGetPoolBuilder creates a new PoolBuilder with the provided RequestContext.
// NewGetPoolBuilder creates a new instance of PoolBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - PoolBuilder: An instance of the PoolBuilder interface.
func NewGetPoolBuilder(requestContext model.RequestContext) PoolBuilder {
return &getPool{requestContext: requestContext}
}
// getPool is an implementation of the PoolBuilder interface.
type getPool struct {
requestContext model.RequestContext
id int
}
// ID sets the pool ID for the query.
//
// Parameters:
// - poolID: The ID of the pool to be retrieved.
//
// Returns:
// - PoolBuilder: The instance of the PoolBuilder for method chaining.
func (g *getPool) ID(poolID int) PoolBuilder {
g.id = poolID
return g
}
// Execute sends the constructed query and returns the requested pool and an error, if any.
//
// Returns:
// - model.Pool: The requested pool.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getPool) Execute() (model.Pool, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -31,7 +31,13 @@ type PoolsBuilder interface {
Execute() ([]model.Pool, error)
}
// NewGetPoolsBuilder creates a new PoolsBuilder with the provided RequestContext.
// NewGetPoolsBuilder creates a new instance of PoolsBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - PoolsBuilder: An instance of the PoolsBuilder interface.
func NewGetPoolsBuilder(requestContext model.RequestContext) PoolsBuilder {
return &getPools{
requestContext: requestContext,
@ -39,66 +45,125 @@ func NewGetPoolsBuilder(requestContext model.RequestContext) PoolsBuilder {
}
}
// getPools is an implementation of the PoolsBuilder interface.
type getPools struct {
requestContext model.RequestContext
query map[string]string
}
// SearchName sets the query to search for pools with a specific name.
//
// Parameters:
// - name: The name to search for in the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchName(name string) PoolsBuilder {
g.query["search[name_matches]"] = name
return g
}
// SearchID sets the query to search for pools with specific pool IDs.
//
// Parameters:
// - poolIDs: The pool IDs to search for, separated by commas.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchID(poolIDs string) PoolsBuilder {
g.query["search[id]"] = poolIDs
return g
}
// SearchDescription sets the query to search for pools with specific descriptions.
//
// Parameters:
// - description: The description to search for in the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchDescription(description string) PoolsBuilder {
g.query["search[description_matches]"] = description
return g
}
// SearchCreatorID sets the query to search for pools created by a specific user (by their user ID).
//
// Parameters:
// - creatorID: The ID of the user who created the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchCreatorID(creatorID int) PoolsBuilder {
g.query["search[creator_id]"] = strconv.Itoa(creatorID)
return g
}
// SearchCreatorName sets the query to search for pools created by a specific user (by their username).
//
// Parameters:
// - creatorName: The username of the user who created the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchCreatorName(creatorName string) PoolsBuilder {
g.query["search[creator_name]"] = creatorName
return g
}
// Active sets whether to search for active or inactive pools.
//
// Parameters:
// - isActive: A boolean indicating whether to search for active pools (true) or inactive pools (false).
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) Active(isActive bool) PoolsBuilder {
g.query["search[is_active]"] = strconv.FormatBool(isActive)
return g
}
// SearchCategory sets the query to search for pools in a specific category.
//
// Parameters:
// - category: The category to search for in the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchCategory(category model.PoolCategory) PoolsBuilder {
g.query["search[category]"] = string(category)
return g
}
// Order sets the ordering of the retrieved pools.
//
// Parameters:
// - order: The order to apply to the retrieved pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) Order(order model.PoolOrder) PoolsBuilder {
g.query["search[order]"] = string(order)
return g
}
// SetLimit sets the maximum number of pools to retrieve.
//
// Parameters:
// - limitPools: The maximum number of pools to retrieve.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SetLimit(limitPools int) PoolsBuilder {
g.query["limit"] = strconv.Itoa(limitPools)
return g
}
// Execute sends the constructed query and returns a slice of pools and an error, if any.
//
// Returns:
// - []model.Pool: A slice of pools.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getPools) Execute() ([]model.Pool, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -15,23 +15,40 @@ type PostBuilder interface {
Execute() (*model.Post, error)
}
// NewGetPostBuilder creates a new PostBuilder with the provided RequestContext.
// NewGetPostBuilder creates a new instance of PostBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - PostBuilder: An instance of the PostBuilder interface.
func NewGetPostBuilder(requestContext model.RequestContext) PostBuilder {
return &getPost{requestContext: requestContext}
}
// getPost is an implementation of the PostBuilder interface.
type getPost struct {
requestContext model.RequestContext
postID model.PostID
}
// SetPostID sets the post ID for the query.
//
// Parameters:
// - postID: The ID of the post to be retrieved.
//
// Returns:
// - PostBuilder: The instance of the PostBuilder for method chaining.
func (g *getPost) SetPostID(postID model.PostID) PostBuilder {
g.postID = postID
return g
}
// Execute sends the constructed query and returns the requested post and an error, if any.
//
// Returns:
// - *model.Post: The requested post.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getPost) Execute() (*model.Post, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -24,7 +24,13 @@ type PostsBuilder interface {
Execute() ([]model.Post, error)
}
// NewGetPostsBuilder creates a new PostsBuilder with the provided RequestContext.
// NewGetPostsBuilder creates a new instance of PostsBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - PostsBuilder: An instance of the PostsBuilder interface.
func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder {
postBuilder := &getPosts{
requestContext: requestContext,
@ -34,42 +40,77 @@ func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder {
return postBuilder.SetLimit(utils.E621_MAX_POST_COUNT)
}
// getPosts is an implementation of the PostsBuilder interface.
type getPosts struct {
requestContext model.RequestContext
query map[string]string
}
// Tags sets the query to search for posts with specific tags.
//
// Parameters:
// - tags: The tags to search for in the posts.
//
// Returns:
// - PostsBuilder: The instance of the PostsBuilder for method chaining.
func (g *getPosts) Tags(tags string) PostsBuilder {
g.query["tags"] = tags
return g
}
// PageAfter sets the query to retrieve posts after a specific post ID.
//
// Parameters:
// - postID: The ID of the post after which to retrieve other posts.
//
// Returns:
// - PostsBuilder: The instance of the PostsBuilder for method chaining.
func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder {
g.query["page"] = "a" + strconv.Itoa(int(postID))
return g
}
// PageBefore sets the query to retrieve posts before a specific post ID.
//
// Parameters:
// - postID: The ID of the post before which to retrieve other posts.
//
// Returns:
// - PostsBuilder: The instance of the PostsBuilder for method chaining.
func (g *getPosts) PageBefore(postID model.PostID) PostsBuilder {
g.query["page"] = "b" + strconv.Itoa(int(postID))
return g
}
// PageNumber sets the query to retrieve posts on a specific page number.
//
// Parameters:
// - number: The page number to retrieve posts from.
//
// Returns:
// - PostsBuilder: The instance of the PostsBuilder for method chaining.
func (g *getPosts) PageNumber(number int) PostsBuilder {
g.query["page"] = strconv.Itoa(number)
return g
}
// SetLimit sets the maximum number of posts to retrieve.
//
// Parameters:
// - limitUser: The maximum number of posts to retrieve.
//
// Returns:
// - PostsBuilder: The instance of the PostsBuilder for method chaining.
func (g *getPosts) SetLimit(limitUser int) PostsBuilder {
g.query["limit"] = strconv.Itoa(limitUser)
return g
}
// Execute sends the constructed query and returns a slice of posts and an error, if any.
//
// Returns:
// - []model.Post: A slice of posts.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getPosts) Execute() ([]model.Post, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -15,23 +15,40 @@ type TagBuilder interface {
Execute() (model.Tag, error)
}
// NewGetTagBuilder creates a new TagBuilder with the provided RequestContext.
// NewGetTagBuilder creates a new instance of TagBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - TagBuilder: An instance of the TagBuilder interface.
func NewGetTagBuilder(requestContext model.RequestContext) TagBuilder {
return &getTag{requestContext: requestContext}
}
// getTag is an implementation of the TagBuilder interface.
type getTag struct {
requestContext model.RequestContext
tagID int
}
// SetTagID sets the tag ID for the query.
//
// Parameters:
// - tagID: The ID of the tag to be retrieved.
//
// Returns:
// - TagBuilder: The instance of the TagBuilder for method chaining.
func (g *getTag) SetTagID(tagID int) TagBuilder {
g.tagID = tagID
return g
}
// Execute sends the constructed query and returns the requested tag and an error, if any.
//
// Returns:
// - model.Tag: The requested tag.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getTag) Execute() (model.Tag, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -29,65 +29,124 @@ type TagsBuilder interface {
Execute() ([]model.Tag, error)
}
// NewGetTagsBuilder creates a new TagsBuilder with the provided RequestContext.
// NewGetTagsBuilder creates a new instance of TagsBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - TagsBuilder: An instance of the TagsBuilder interface.
func NewGetTagsBuilder(requestContext model.RequestContext) TagsBuilder {
return &getTags{requestContext: requestContext, query: make(map[string]string)}
}
// getTags is an implementation of the TagsBuilder interface.
type getTags struct {
requestContext model.RequestContext
query map[string]string
}
// SearchName sets the query to search for tags with specific names.
//
// Parameters:
// - name: The name to search for.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) SearchName(name string) TagsBuilder {
g.query["search[name_matches]"] = name
return g
}
// SearchCategory sets the query to search for tags in a specific category.
//
// Parameters:
// - category: The category of tags to search for.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) SearchCategory(category model.TagCategory) TagsBuilder {
g.query["search[category]"] = strconv.Itoa(int(category))
return g
}
// Order sets the query to order tags by a specific criterion.
//
// Parameters:
// - order: The criterion to order tags by.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) Order(order string) TagsBuilder {
g.query["search[order]"] = order
return g
}
// HideEmpty sets the query to filter out tags that are empty.
//
// Parameters:
// - hideEmpty: A boolean indicating whether to filter out empty tags.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) HideEmpty(hideEmpty bool) TagsBuilder {
g.query["search[hide_empty]"] = strconv.FormatBool(hideEmpty)
return g
}
// Wiki sets the query to filter tags that have a wiki.
//
// Parameters:
// - hasWiki: A boolean indicating whether to filter tags with a wiki.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) Wiki(hasWiki bool) TagsBuilder {
g.query["search[has_wiki]"] = strconv.FormatBool(hasWiki)
return g
}
// Artist sets the query to filter tags that have an artist page.
//
// Parameters:
// - hasArtistPage: A boolean indicating whether to filter tags with an artist page.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) Artist(hasArtistPage bool) TagsBuilder {
g.query["search[has_artist]"] = strconv.FormatBool(hasArtistPage)
return g
}
// SetPage sets the query to retrieve tags from a specific page number.
//
// Parameters:
// - pageNumber: The page number to retrieve tags from.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) SetPage(pageNumber int) TagsBuilder {
g.query["page"] = strconv.Itoa(pageNumber)
return g
}
// SetLimit sets the maximum number of tags to retrieve.
//
// Parameters:
// - limitUser: The maximum number of tags to retrieve.
//
// Returns:
// - TagsBuilder: The instance of the TagsBuilder for method chaining.
func (g *getTags) SetLimit(limitUser int) TagsBuilder {
g.query["limit"] = strconv.Itoa(limitUser)
return g
}
// Execute sends the constructed query and returns a slice of tags and an error, if any.
//
// Returns:
// - []model.Tag: A slice of tags.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getTags) Execute() ([]model.Tag, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -14,23 +14,40 @@ type UserBuilder interface {
Execute() (model.User, error)
}
// NewGetUserBuilder creates a new UserBuilder with the provided RequestContext.
// NewGetUserBuilder creates a new instance of UserBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - UserBuilder: An instance of the UserBuilder interface.
func NewGetUserBuilder(requestContext model.RequestContext) UserBuilder {
return &getUser{requestContext: requestContext}
}
// getUser is an implementation of the UserBuilder interface.
type getUser struct {
requestContext model.RequestContext
username string
}
// SetUsername sets the username for the query to retrieve user information.
//
// Parameters:
// - username: The username to retrieve information for.
//
// Returns:
// - UserBuilder: The instance of the UserBuilder for method chaining.
func (g *getUser) SetUsername(username string) UserBuilder {
g.username = username
return g
}
// Execute sends the constructed query and returns the requested user information and an error, if any.
//
// Returns:
// - model.User: The user information.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getUser) Execute() (model.User, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -36,82 +36,159 @@ type UsersBuilder interface {
}
// NewGetUsersBuilder creates a new UsersBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - UsersBuilder: An instance of the UsersBuilder interface.
func NewGetUsersBuilder(requestContext model.RequestContext) UsersBuilder {
return &getUsers{requestContext: requestContext, query: make(map[string]string)}
}
// getUsers is an implementation of the UsersBuilder interface.
type getUsers struct {
requestContext model.RequestContext
query map[string]string
}
// SearchByName specifies a username to search for.
//
// Parameters:
// - userName: The username to search for.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByName(userName string) UsersBuilder {
g.query["search[name_matches]"] = userName
return g
}
// SearchByAbout specifies a text to search in the 'about' field of user profiles.
//
// Parameters:
// - about: The text to search for in the 'about' field of user profiles.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByAbout(about string) UsersBuilder {
g.query["search[about_me]"] = about
return g
}
// SearchByAvatarID specifies an avatar ID to search for.
//
// Parameters:
// - avatarID: The avatar ID to search for.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByAvatarID(avatarID model.PostID) UsersBuilder {
g.query["search[avatar_id]"] = strconv.FormatInt(int64(avatarID), 10)
return g
}
// SearchByLevel specifies a user level to filter by.
//
// Parameters:
// - level: The user level to filter by.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByLevel(level model.UserLevel) UsersBuilder {
g.query["search[level]"] = strconv.Itoa(int(level))
return g
}
// SearchByMinLevel specifies the minimum user level to filter by.
//
// Parameters:
// - minLevel: The minimum user level to filter by.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByMinLevel(minLevel model.UserLevel) UsersBuilder {
g.query["search[min_level]"] = strconv.Itoa(int(minLevel))
return g
}
// SearchByMaxLevel specifies the maximum user level to filter by.
//
// Parameters:
// - maxLevel: The maximum user level to filter by.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByMaxLevel(maxLevel model.UserLevel) UsersBuilder {
g.query["search[max_level]"] = strconv.Itoa(int(maxLevel))
return g
}
// SearchByCanUpload specifies whether users can upload free content.
//
// Parameters:
// - canUpload: Whether users can upload free content.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByCanUpload(canUpload bool) UsersBuilder {
g.query["search[can_upload_free]"] = strconv.FormatBool(canUpload)
return g
}
// SearchByIsApprover specifies whether users can approve posts.
//
// Parameters:
// - isApprover: Whether users can approve posts.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByIsApprover(isApprover bool) UsersBuilder {
g.query["search[can_approve_posts]"] = strconv.FormatBool(isApprover)
return g
}
// SearchByOrder specifies the order in which users are retrieved.
//
// Parameters:
// - order: The order in which users are retrieved.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SearchByOrder(order model.Order) UsersBuilder {
g.query["search[order]"] = string(order)
return g
}
// SetPage sets the page number for paginated results.
//
// Parameters:
// - pageNumber: The page number for paginated results.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SetPage(pageNumber int) UsersBuilder {
g.query["page"] = strconv.Itoa(pageNumber)
return g
}
// SetLimit sets the maximum number of users to retrieve.
//
// Parameters:
// - limitUser: The maximum number of users to retrieve.
//
// Returns:
// - UsersBuilder: The instance of the UsersBuilder for method chaining.
func (g *getUsers) SetLimit(limitUser int) UsersBuilder {
g.query["limit"] = strconv.Itoa(limitUser)
return g
}
// Execute sends the constructed query and returns the requested user information and an error, if any.
//
// Returns:
// - []model.User: A slice of user information.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getUsers) Execute() ([]model.User, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())

View File

@ -1,15 +1,23 @@
package e621
import (
"bytes"
"compress/gzip"
"encoding/csv"
"fmt"
"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.
@ -242,3 +250,132 @@ func (c *Client) GetAllPosts(postBuilder builder.PostsBuilder) ([]model.Post, er
// Retrieves all available posts using the provided post builder.
return c.GetNPosts(math.MaxInt, postBuilder)
}
func (c *Client) GetLatestPoolsDBExportDataAsBytes() (string, []byte, error) {
dbExportFileNameList, err := builder.NewGetDBExportListBuilder(c.RequestContext).Execute()
if err != nil {
return "", nil, err
}
getDBExportFile := builder.NewGetDBExportFileBuilder(c.RequestContext)
filter := func(s string) bool { return strings.HasPrefix(s, "pools") }
filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter)
regex, err := regexp.Compile("\\d{4}\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])*")
if err != nil {
return "", nil, err
}
currentDate, err := time.Parse("2006-04-02", time.Now().Format("2006-01-02"))
if err != nil {
return "", nil, err
}
duration := math.MaxFloat64
var fileName string
for _, listFileName := range filteredFileNameList {
if !regex.MatchString(listFileName) {
return "", nil, nil
}
fileDate, err := time.Parse("2006-04-02", regex.FindString(listFileName))
if err != nil {
return "", nil, err
}
if currentDate.Sub(fileDate).Seconds() < duration {
duration = currentDate.Sub(fileDate).Seconds()
fileName = listFileName
}
}
rawFile, err := getDBExportFile.SetFile(fileName).Execute()
if err != nil {
return "", nil, err
}
return fileName, rawFile, nil
}
func (c *Client) GetLatestPoolsDBExportDataAsStruct() ([]*model.Pool, error) {
var pools []*model.Pool
_, data, err := c.GetLatestPoolsDBExportDataAsBytes()
if err != nil {
return nil, err
}
zipReader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
panic(err)
}
defer zipReader.Close()
// Create a CSV reader
reader := csv.NewReader(zipReader)
err = gocsv.UnmarshalCSV(reader, &pools)
if err != nil {
panic(err)
}
return pools, nil
}
func (c *Client) GetLatestPostsDBExportDataAsBytes() (string, []byte, error) {
log.Println("Please wait while the download is in progress for the post export... (file over 1GB) ")
dbExportFileNameList, err := builder.NewGetDBExportListBuilder(c.RequestContext).Execute()
if err != nil {
return "", nil, err
}
getDBExportFile := builder.NewGetDBExportFileBuilder(c.RequestContext)
filter := func(s string) bool { return strings.HasPrefix(s, "posts") }
filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter)
regex, err := regexp.Compile("\\d{4}\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])*")
if err != nil {
return "", nil, err
}
currentDate, err := time.Parse("2006-04-02", time.Now().Format("2006-01-02"))
if err != nil {
return "", nil, err
}
duration := math.MaxFloat64
var fileName string
for _, listFileName := range filteredFileNameList {
if !regex.MatchString(listFileName) {
return "", nil, nil
}
fileDate, err := time.Parse("2006-04-02", regex.FindString(listFileName))
if err != nil {
return "", nil, err
}
if currentDate.Sub(fileDate).Seconds() < duration {
duration = currentDate.Sub(fileDate).Seconds()
fileName = listFileName
}
}
rawFile, err := getDBExportFile.SetFile(fileName).Execute()
if err != nil {
return "", nil, err
}
return fileName, rawFile, nil
}
func (c *Client) GetLatestPostsDBExportDataAsStruct() ([]*model.Post, error) {
var post []*model.Post
// TODO: Implement this function; tags in CSV are just a string with no categories assignment, needs special parsing
return post, nil
}

View File

@ -5,6 +5,7 @@ import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"golang.org/x/net/html"
"io"
"net/http"
"strings"
)
@ -15,7 +16,7 @@ import (
// the HTML content to extract the links to export files with the ".csv.gz" extension.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - model.RequestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - []string: A slice of file names with the ".csv.gz" extension.
@ -79,13 +80,13 @@ func GetDBExportList(requestContext model.RequestContext) ([]string, error) {
// particular file identified by its name.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - model.RequestContext: The context for the API request, including the host, user agent, username, and API key.
// - file: The name of the file to be fetched from the database export.
//
// Returns:
// - *http.Response: The HTTP response containing the requested file (probably a csv.gz).
// - []byte: A byte array representation of the file.
// - error: An error, if any, encountered during the API request or response handling.
func GetDBExportFile(requestContext model.RequestContext, file string) (*http.Response, error) {
func GetDBExportFile(requestContext model.RequestContext, file string) ([]byte, error) {
if file == "" {
return nil, fmt.Errorf("no file specified")
}
@ -112,6 +113,6 @@ func GetDBExportFile(requestContext model.RequestContext, file string) (*http.Re
return nil, utils.StatusCodesToError(resp.StatusCode)
}
return resp, nil
return io.ReadAll(resp.Body)
}

View File

@ -0,0 +1,89 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"net/http"
)
type RequestTypes interface {
model.User |
model.Tag |
model.Post |
model.PostResponse |
model.Pool |
model.Note |
[]model.User |
[]model.Tag |
[]model.Post |
[]model.Pool |
[]model.Note
}
// getRequest performs an HTTP GET request to the specified e621 API endpoint and unmarshals the JSON response into the provided type T.
//
// This generic function is designed to fetch data from the e621 API using an HTTP GET request.
// It supports generic types and can unmarshal the JSON response into the provided type T.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
// - e621Endpoint: The specific e621 API endpoint to be called.
// - query: A map containing additional query parameters for the API request. (Optional)
//
// Type Parameters:
// - T: The type into which the JSON response will be unmarshaled.
// - supported types are (also as slice): model.User, model.Tag, model.Post, model.PostResponse, model.Pool, model.Note
//
// Returns:
// - T: The result of unmarshaling the JSON response into the provided type.
// - error: An error, if any, encountered during the API request, response handling, or JSON unmarshaling.
func getRequest[T RequestTypes](requestContext model.RequestContext, e621Endpoint string, query map[string]string) (T, error) {
var base T
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", requestContext.Host, e621Endpoint), nil)
if err != nil {
// Log the error and return an empty User struct and the error.
return base, err
}
// Append query parameters to the request URL.
if query != nil {
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty User struct and the error.
return base, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return base, utils.StatusCodesToError(resp.StatusCode)
}
// Decode the JSON response into the User struct.
err = json.NewDecoder(resp.Body).Decode(&base)
if err != nil {
// Log the error and return an empty User struct and the error.
return base, err
}
// Return the user information and no error (nil).
return base, nil
}

View File

@ -1,11 +1,7 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"net/http"
)
// GetFavorites retrieves a user's favorite posts from the e621 API.
@ -20,45 +16,7 @@ import (
// - []model.Post: A slice of favorite posts.
// - error: An error, if any, encountered during the API request or response handling.
func GetFavorites(requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/favorites.json", requestContext.Host), nil)
if err != nil {
return nil, err
}
// Append query parameters to the request URL.
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
return nil, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return nil, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a User struct to store the response data.
var favoriteResponse model.PostResponse
// Decode the JSON response into the user struct.
err = json.NewDecoder(resp.Body).Decode(&favoriteResponse)
if err != nil {
// Log the error and return an empty User struct and the error.
return nil, err
}
return favoriteResponse.Posts, nil
endpoint := "favorites.json"
data, err := getRequest[model.PostResponse](requestContext, endpoint, query)
return data.Posts, err
}

View File

@ -1,13 +1,8 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"io"
"net/http"
"strings"
)
// GetNote retrieves a single note by its ID from the e621 API.
@ -20,45 +15,8 @@ import (
// - model.Note: The retrieved note.
// - error: An error, if any, encountered during the API request or response handling.
func GetNote(requestContext model.RequestContext, ID string) (model.Note, error) {
// Create a new HTTP GET request to fetch the note information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Note struct and the error.
return model.Note{}, err
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty Note struct and the error.
return model.Note{}, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return model.Note{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Note struct to store the response data.
var noteResponse model.Note
// Decode the JSON response into the Note struct.
err = json.NewDecoder(resp.Body).Decode(&noteResponse)
if err != nil {
// Log the error and return an empty Note struct and the error.
return model.Note{}, err
}
// Return the note information and no error (nil).
return noteResponse, nil
endpoint := fmt.Sprintf("notes/%s.json", ID)
return getRequest[model.Note](requestContext, endpoint, nil)
}
// GetNotes retrieves a list of notes from the e621 API based on query parameters.
@ -71,51 +29,6 @@ func GetNote(requestContext model.RequestContext, ID string) (model.Note, error)
// - []model.Note: A slice of notes.
// - error: An error, if any, encountered during the API request or response handling.
func GetNotes(requestContext model.RequestContext, query map[string]string) ([]model.Note, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.json", requestContext.Host), nil)
if err != nil {
return nil, err
}
// Append query parameters to the request URL.
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
return nil, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return nil, utils.StatusCodesToError(resp.StatusCode)
}
respBodyBytes, err := io.ReadAll(resp.Body)
if strings.Contains(string(respBodyBytes), "{\"notes\":[]}") {
return nil, nil
}
// Initialize a slice of Note struct to store the response data.
var notesResponse []model.Note
// Decode the JSON response into the slice of Note structs.
err = json.Unmarshal(respBodyBytes, &notesResponse)
if err != nil {
// Log the error and return an empty slice and the error.
return nil, err
}
// Return the list of notes and no error (nil).
return notesResponse, nil
endpoint := "notes.json"
return getRequest[[]model.Note](requestContext, endpoint, query)
}

View File

@ -1,11 +1,8 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"net/http"
)
// GetPool retrieves a pool by its ID from the e621 API.
@ -18,45 +15,8 @@ import (
// - model.Pool: The retrieved pool.
// - error: An error, if any, encountered during the API request or response handling.
func GetPool(requestContext model.RequestContext, ID string) (model.Pool, error) {
// Create a new HTTP GET request to fetch the pool information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Pool struct and the error.
return model.Pool{}, err
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty Pool struct and the error.
return model.Pool{}, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return model.Pool{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Pool struct to store the response data.
var poolResponse model.Pool
// Decode the JSON response into the Pool struct.
err = json.NewDecoder(resp.Body).Decode(&poolResponse)
if err != nil {
// Log the error and return an empty Pool struct and the error.
return model.Pool{}, err
}
// Return the pool information and no error (nil).
return poolResponse, nil
endpoint := fmt.Sprintf("pools/%s.json", ID)
return getRequest[model.Pool](requestContext, endpoint, nil)
}
// GetPools retrieves a list of pools from the e621 API based on query parameters.
@ -69,46 +29,6 @@ func GetPool(requestContext model.RequestContext, ID string) (model.Pool, error)
// - []model.Pool: A slice of pools.
// - error: An error, if any, encountered during the API request or response handling.
func GetPools(requestContext model.RequestContext, query map[string]string) ([]model.Pool, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools.json", requestContext.Host), nil)
if err != nil {
return nil, err
}
// Append query parameters to the request URL.
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
return nil, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return nil, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a slice of Pool struct to store the response data.
var poolsResponse []model.Pool
// Decode the JSON response into the slice of Pool structs.
err = json.NewDecoder(resp.Body).Decode(&poolsResponse)
if err != nil {
// Log the error and return an empty slice and the error.
return nil, err
}
// Return the list of pools and no error (nil).
return poolsResponse, nil
endpoint := "pools.json"
return getRequest[[]model.Pool](requestContext, endpoint, query)
}

View File

@ -1,11 +1,8 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"net/http"
)
// GetPost retrieves a single post by its ID from the e621 API.
@ -17,45 +14,8 @@ import (
// - model.Post: The retrieved post.
// - error: An error, if any, encountered during the API request or response handling.
func GetPost(requestContext model.RequestContext, ID string) (model.Post, error) {
// Create a new HTTP GET request to fetch the post information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Post struct and the error.
return model.Post{}, err
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty Post struct and the error.
return model.Post{}, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return model.Post{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Post struct to store the response data.
var postResponse model.PostResponse
// Decode the JSON response into the PostResponse struct.
err = json.NewDecoder(resp.Body).Decode(&postResponse)
if err != nil {
// Log the error and return an empty Post struct and the error.
return model.Post{}, err
}
// Return the post information and no error (nil).
return postResponse.Post, nil
endpoint := fmt.Sprintf("posts/%s.json", ID)
return getRequest[model.Post](requestContext, endpoint, nil)
}
// GetPosts retrieves a list of posts from the e621 API based on query parameters.
@ -68,46 +28,7 @@ func GetPost(requestContext model.RequestContext, ID string) (model.Post, error)
// - []model.Post: A slice of posts.
// - error: An error, if any, encountered during the API request or response handling.
func GetPosts(requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/posts.json", requestContext.Host), nil)
if err != nil {
return nil, err
}
// Append query parameters to the request URL.
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
return nil, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return nil, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a slice of Post struct to store the response data.
var postResponse model.PostResponse
// Decode the JSON response into the PostResponse struct.
err = json.NewDecoder(resp.Body).Decode(&postResponse)
if err != nil {
// Log the error and return an empty slice and the error.
return nil, err
}
// Return the list of posts and no error (nil).
return postResponse.Posts, nil
endpoint := "posts.json"
data, err := getRequest[model.PostResponse](requestContext, endpoint, query)
return data.Posts, err
}

View File

@ -1,11 +1,8 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"net/http"
)
// GetTag retrieves a tag by its ID from the e621 API.
@ -18,46 +15,8 @@ import (
// - model.Tag: The retrieved tag.
// - error: An error, if any, encountered during the API request or response handling.
func GetTag(requestContext model.RequestContext, ID string) (model.Tag, error) {
// Create a new HTTP GET request to fetch tag information.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/tags/%s.json", requestContext.Host, ID), nil)
if err != nil {
// Log the error and return an empty Tag struct and the error.
return model.Tag{}, err
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application.json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty Tag struct and the error.
return model.Tag{}, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return model.Tag{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a Tag struct to store the response data.
var tag model.Tag
// Decode the JSON response into the Tag struct.
err = json.NewDecoder(resp.Body).Decode(&tag)
if err != nil {
// Log the error and return an empty Tag struct and the error.
return model.Tag{}, err
}
// Return the tag information and no error (nil).
return tag, nil
endpoint := fmt.Sprintf("tags/%s.json", ID)
return getRequest[model.Tag](requestContext, endpoint, nil)
}
// GetTags retrieves a list of tags from the e621 API based on query parameters.
@ -80,46 +39,6 @@ func GetTag(requestContext model.RequestContext, ID string) (model.Tag, error) {
// - []model.Tag: A slice of tags.
// - error: An error, if any, encountered during the API request or response handling.
func GetTags(requestContext model.RequestContext, query map[string]string) ([]model.Tag, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/tags.json", requestContext.Host), nil)
if err != nil {
return nil, err
}
// Append query parameters to the request URL.
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
return nil, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return []model.Tag{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a slice of Tag struct to store the response data.
var tags []model.Tag
// Decode the JSON response into the slice of Tag structs.
err = json.NewDecoder(resp.Body).Decode(&tags)
if err != nil {
// Log the error and return an empty slice and the error.
return []model.Tag{}, err
}
// Return the list of tags and no error (nil).
return tags, nil
endpoint := "tags.json"
return getRequest[[]model.Tag](requestContext, endpoint, query)
}

View File

@ -1,11 +1,8 @@
package endpoints
import (
"encoding/json"
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
"net/http"
)
// GetUser retrieves user information from e621.net based on the provided username.
@ -18,45 +15,8 @@ import (
// - model.User: The retrieved user.
// - error: An error, if any, encountered during the API request or response handling.
func GetUser(requestContext model.RequestContext, username string) (model.User, error) {
// Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/users/%s.json", requestContext.Host, username), nil)
if err != nil {
// Log the error and return an empty User struct and the error.
return model.User{}, err
}
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
// Log the error and return an empty User struct and the error.
return model.User{}, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return model.User{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a User struct to store the response data.
var user model.User
// Decode the JSON response into the User struct.
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
// Log the error and return an empty User struct and the error.
return model.User{}, err
}
// Return the user information and no error (nil).
return user, nil
endpoint := fmt.Sprintf("users/%s.json", username)
return getRequest[model.User](requestContext, endpoint, nil)
}
// GetUsers retrieves a list of users from e621.net based on query parameters.
@ -69,46 +29,6 @@ func GetUser(requestContext model.RequestContext, username string) (model.User,
// - []model.User: A slice of users.
// - error: An error, if any, encountered during the API request or response handling.
func GetUsers(requestContext model.RequestContext, query map[string]string) ([]model.User, error) {
// Create a new HTTP GET request.
r, err := http.NewRequest("GET", fmt.Sprintf("%s/users.json", requestContext.Host), nil)
if err != nil {
return nil, err
}
// Append query parameters to the request URL.
q := r.URL.Query()
for k, v := range query {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
r.Header.Set("User-Agent", requestContext.UserAgent)
r.Header.Add("Accept", "application/json")
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
// Send the request using the provided http.Client.
resp, err := requestContext.Client.Do(r)
if err != nil {
return nil, err
}
// Check if the HTTP response status code indicates success (2xx range).
if resp.StatusCode < 200 || resp.StatusCode > 300 {
// If the status code is outside the 2xx range, return an error based on the status code.
return []model.User{}, utils.StatusCodesToError(resp.StatusCode)
}
// Initialize a slice of User struct to store the response data.
var users []model.User
// Decode the JSON response into the slice of User structs.
err = json.NewDecoder(resp.Body).Decode(&users)
if err != nil {
// Log the error and return an empty slice and the error.
return []model.User{}, err
}
// Return the list of users and no error (nil).
return users, nil
endpoint := "users.json"
return getRequest[[]model.User](requestContext, endpoint, query)
}

View File

@ -1,7 +1,14 @@
package model
import (
"fmt"
"strconv"
"strings"
)
type PoolCategory string
type PoolOrder string
type PoolIDs []int64
const (
Series PoolCategory = "series"
@ -16,15 +23,52 @@ const (
)
type Pool struct {
ID int64 `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
CreatorID int64 `json:"creator_id"`
Description string `json:"description"`
IsActive bool `json:"is_active"`
Category PoolCategory `json:"category"`
PostIDS []int64 `json:"post_ids"`
CreatorName string `json:"creator_name"`
PostCount int64 `json:"post_count"`
ID int64 `json:"id" csv:"id"`
Name string `json:"name" csv:"name"`
CreatedAt string `json:"created_at" csv:"created_at"`
UpdatedAt string `json:"updated_at" csv:"updated_at"`
CreatorID int64 `json:"creator_id" csv:"creator_id"`
Description string `json:"description" csv:"description"`
IsActive bool `json:"is_active" csv:"is_active"`
Category PoolCategory `json:"category" csv:"category"`
PostIDS PoolIDs `json:"post_ids" csv:"post_ids"`
CreatorName string `json:"creator_name" csv:"-"`
PostCount int64 `json:"post_count" csv:"-"`
}
// UnmarshalCSV parses a CSV-formatted string containing pool IDs and populates the PoolIDs receiver.
//
// This method is designed to unmarshal a CSV-formatted string, where pool IDs are separated by commas.
// It trims the surrounding curly braces and splits the string to extract individual pool IDs.
// The parsed IDs are then converted to int64 and assigned to the PoolIDs receiver.
//
// Parameters:
// - csv: The CSV-formatted string containing pool IDs.
//
// Returns:
// - error: An error encountered during the unmarshaling process, if any.
func (poolIDs *PoolIDs) UnmarshalCSV(csv string) error {
// Trim the surrounding curly braces
csv = strings.TrimPrefix(csv, "{")
csv = strings.TrimSuffix(csv, "}")
// Split the CSV string into individual pool IDs
ids := strings.Split(csv, ",")
var localPoolIDs PoolIDs
// Iterate through each ID, parse it to int64, and append to the localPoolIDs
for _, id := range ids {
if id != "" {
int64ID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse pool ID '%s': %v", id, err)
}
localPoolIDs = append(localPoolIDs, int64ID)
}
}
// Assign the parsed IDs to the receiver
*poolIDs = localPoolIDs
return nil
}

View File

@ -8,86 +8,86 @@ type PostResponse struct {
}
type Post struct {
ID PostID `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
File File `json:"file"`
Preview Preview `json:"preview"`
Sample Sample `json:"sample"`
Score Score `json:"score"`
Tags Tags `json:"tags"`
LockedTags []interface{} `json:"locked_tags"`
ChangeSeq int64 `json:"change_seq"`
Flags Flags `json:"flags"`
Rating string `json:"rating"`
FavCount int64 `json:"fav_count"`
Sources []string `json:"sources"`
Pools []interface{} `json:"pools"`
Relationships Relationships `json:"relationships"`
ApproverID interface{} `json:"approver_id"`
UploaderID int64 `json:"uploader_id"`
Description string `json:"description"`
CommentCount int64 `json:"comment_count"`
IsFavorited bool `json:"is_favorited"`
HasNotes bool `json:"has_notes"`
Duration interface{} `json:"duration"`
ID PostID `json:"id" csv:"id"`
CreatedAt string `json:"created_at" csv:"created_at"`
UpdatedAt string `json:"updated_at" csv:"updated_at"`
File File `json:"file" csv:"file"`
Preview Preview `json:"preview" csv:"-"`
Sample Sample `json:"sample" csv:"-"`
Score Score `json:"score" csv:"score"`
Tags Tags `json:"tags" csv:"tag_string"`
LockedTags []interface{} `json:"locked_tags" csv:"locked_tags"`
ChangeSeq int64 `json:"change_seq" csv:"change_seq"`
Flags Flags `json:"flags" csv:"-"`
Rating string `json:"rating" csv:"rating"`
FavCount int64 `json:"fav_count" csv:"fav_count"`
Sources []string `json:"sources" csv:"source"`
Pools []interface{} `json:"pools" csv:"-"`
Relationships Relationships `json:"relationships" csv:"-"`
ApproverID UserID `json:"approver_id" csv:"approver_id"`
UploaderID UserID `json:"uploader_id" csv:"uploader_id"`
Description string `json:"description" csv:"description"`
CommentCount int64 `json:"comment_count" csv:"comment_count"`
IsFavorited bool `json:"is_favorited" csv:"-"`
HasNotes bool `json:"has_notes" csv:"-"`
Duration interface{} `json:"duration" csv:"duration"`
}
type File struct {
Width int64 `json:"width"`
Height int64 `json:"height"`
EXT string `json:"ext"`
Size int64 `json:"size"`
Md5 string `json:"md5"`
URL string `json:"url"`
Width int64 `json:"width" csv:"image_width"`
Height int64 `json:"height" csv:"image_height"`
EXT string `json:"ext" csv:"file_ext"`
Size int64 `json:"size" csv:"file_size"`
Md5 string `json:"md5" csv:"md5"`
URL string `json:"url" csv:"-"`
}
type Flags struct {
Pending bool `json:"pending"`
Flagged bool `json:"flagged"`
NoteLocked bool `json:"note_locked"`
StatusLocked bool `json:"status_locked"`
RatingLocked bool `json:"rating_locked"`
Deleted bool `json:"deleted"`
Pending bool `json:"pending" csv:"is_pending"`
Flagged bool `json:"flagged" csv:"is_flagged"`
NoteLocked bool `json:"note_locked" csv:"is_note_locked"`
StatusLocked bool `json:"status_locked" csv:"is_status_locked"`
RatingLocked bool `json:"rating_locked" csv:"is_rating_locked"`
Deleted bool `json:"deleted" csv:"is_deleted"`
}
type Preview struct {
Width int64 `json:"width"`
Height int64 `json:"height"`
URL string `json:"url"`
Width int64 `json:"width" csv:"-"`
Height int64 `json:"height" csv:"-"`
URL string `json:"url" csv:"-"`
}
type Relationships struct {
ParentID interface{} `json:"parent_id"`
HasChildren bool `json:"has_children"`
HasActiveChildren bool `json:"has_active_children"`
Children []interface{} `json:"children"`
ParentID PostID `json:"parent_id" csv:"parent_id"`
HasChildren bool `json:"has_children" csv:"-"`
HasActiveChildren bool `json:"has_active_children" csv:"-"`
Children []PostID `json:"children" csv:"-"`
}
type Sample struct {
Has bool `json:"has"`
Height int64 `json:"height"`
Width int64 `json:"width"`
URL string `json:"url"`
Alternates Alternates `json:"alternates"`
Has bool `json:"has" csv:"-"`
Height int64 `json:"height" csv:"-"`
Width int64 `json:"width" csv:"-"`
URL string `json:"url" csv:"-"`
Alternates Alternates `json:"alternates" csv:"-"`
}
type Alternates struct {
}
type Score struct {
Up int64 `json:"up"`
Down int64 `json:"down"`
Total int64 `json:"total"`
Up int64 `json:"up" csv:"up_score"`
Down int64 `json:"down" csv:"down_score"`
Total int64 `json:"total" csv:"-"`
}
type Tags struct {
General []string `json:"general"`
Artist []string `json:"artist"`
Copyright []string `json:"copyright"`
Character []string `json:"character"`
Species []string `json:"species"`
Invalid []string `json:"invalid"`
Meta []string `json:"meta"`
Lore []string `json:"lore"`
General []string `json:"general" csv:"-"`
Artist []string `json:"artist" csv:"-"`
Copyright []string `json:"copyright" csv:"-"`
Character []string `json:"character" csv:"-"`
Species []string `json:"species" csv:"-"`
Invalid []string `json:"invalid" csv:"-"`
Meta []string `json:"meta" csv:"-"`
Lore []string `json:"lore" csv:"-"`
}

View File

@ -1,4 +1,4 @@
package utils
// E621_MAX_POST_COUNT is the maximum allowable post count for E621.
// E621_MAX_POST_COUNT is the maximum allowable post count for e621.
const E621_MAX_POST_COUNT = 320

View File

@ -3,6 +3,12 @@ package utils
import "fmt"
// StatusCodesToError maps HTTP status codes to corresponding error types.
//
// Parameters:
// - statusCode: The HTTP status code to be mapped to an error type.
//
// Returns:
// - error: An error representing the mapped HTTP status code.
func StatusCodesToError(statusCode int) error {
var err error
switch statusCode {
@ -31,6 +37,7 @@ func StatusCodesToError(statusCode int) error {
// AccessDeniedError represents an "Access Denied" error.
type AccessDeniedError struct{}
// Error returns the error message for AccessDeniedError.
func (_ AccessDeniedError) Error() string {
return "access denied"
}
@ -38,6 +45,7 @@ func (_ AccessDeniedError) Error() string {
// NotFoundError represents a "Not Found" error.
type NotFoundError struct{}
// Error returns the error message for NotFoundError.
func (_ NotFoundError) Error() string {
return "not found"
}
@ -45,6 +53,7 @@ func (_ NotFoundError) Error() string {
// PreconditionFailedError represents a "Precondition Failed" error.
type PreconditionFailedError struct{}
// Error returns the error message for PreconditionFailedError.
func (_ PreconditionFailedError) Error() string {
return "precondition failed"
}
@ -52,6 +61,7 @@ func (_ PreconditionFailedError) Error() string {
// RateLimitReachedError represents a "Rate Limit Reached" error.
type RateLimitReachedError struct{}
// Error returns the error message for RateLimitReachedError.
func (_ RateLimitReachedError) Error() string {
return "rate limit reached"
}
@ -59,6 +69,7 @@ func (_ RateLimitReachedError) Error() string {
// InvalidParametersError represents an "Invalid Parameters" error.
type InvalidParametersError struct{}
// Error returns the error message for InvalidParametersError.
func (_ InvalidParametersError) Error() string {
return "invalid parameters"
}
@ -66,6 +77,7 @@ func (_ InvalidParametersError) Error() string {
// InternalServerError represents an "Internal Server Error" error.
type InternalServerError struct{}
// Error returns the error message for InternalServerError.
func (_ InternalServerError) Error() string {
return "internal server error"
}
@ -73,6 +85,7 @@ func (_ InternalServerError) Error() string {
// BadGatewayError represents a "Bad Gateway" error.
type BadGatewayError struct{}
// Error returns the error message for BadGatewayError.
func (_ BadGatewayError) Error() string {
return "bad gateway"
}
@ -80,6 +93,7 @@ func (_ BadGatewayError) Error() string {
// ServiceUnavailableError represents a "Service Unavailable" error.
type ServiceUnavailableError struct{}
// Error returns the error message for ServiceUnavailableError.
func (_ ServiceUnavailableError) Error() string {
return "service unavailable"
}
@ -87,6 +101,7 @@ func (_ ServiceUnavailableError) Error() string {
// UnknownError represents an "Unknown" error.
type UnknownError struct{}
// Error returns the error message for UnknownError.
func (_ UnknownError) Error() string {
return "unknown error"
}
@ -94,6 +109,7 @@ func (_ UnknownError) Error() string {
// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error.
type OriginConnectionTimeOutError struct{}
// Error returns the error message for OriginConnectionTimeOutError.
func (_ OriginConnectionTimeOutError) Error() string {
return "origin connection time-out"
}
@ -101,6 +117,7 @@ func (_ OriginConnectionTimeOutError) Error() string {
// SSLHandshakeFailedError represents an "SSL Handshake Failed" error.
type SSLHandshakeFailedError struct{}
// Error returns the error message for SSLHandshakeFailedError.
func (_ SSLHandshakeFailedError) Error() string {
return "ssl handshake failed"
}

53
pkg/e621/utils/helper.go Normal file
View File

@ -0,0 +1,53 @@
package utils
import (
"encoding/json"
"os"
)
// SliceFilter filters elements of a generic type from a slice based on a provided filter function.
//
// Parameters:
// - slice: The input slice containing elements of type T.
// - filter: A function that takes an element of type T and returns a boolean indicating whether to include the element in the result.
//
// Returns:
// - ret: A slice containing elements that satisfy the filtering condition.
func SliceFilter[T any](slice []T, filter func(T) bool) (ret []T) {
for _, s := range slice {
if filter(s) {
ret = append(ret, s)
}
}
return
}
// LoadJsonTestData decodes JSON data from a file and populates a variable of a generic type T.
//
// Parameters:
// - testDataPath: The file path to the JSON data.
//
// Returns:
// - T: A variable of type T populated with the decoded JSON data.
// - error: An error, if any, encountered during file opening, decoding, or closing.
func LoadJsonTestData[T any](testDataPath string) (T, error) {
// Create a variable to store the decoded JSON data
var jsonData T
// Open the JSON file
file, err := os.Open(testDataPath)
if err != nil {
return jsonData, err
}
defer file.Close()
// Create a decoder
decoder := json.NewDecoder(file)
// Decode the JSON data into the struct
if err := decoder.Decode(&jsonData); err != nil {
return jsonData, err
}
return jsonData, nil
}

View File

@ -1,28 +0,0 @@
package utils
import (
"encoding/json"
"os"
)
func LoadJsonTestData[T any](testDataPath string) (T, error) {
// Create a variable to store the decoded JSON data
var jsonData T
// Open the JSON file
file, err := os.Open(testDataPath)
if err != nil {
return jsonData, err
}
defer file.Close()
// Create a decoder
decoder := json.NewDecoder(file)
// Decode the JSON data into the struct
if err := decoder.Decode(&jsonData); err != nil {
return jsonData, err
}
return jsonData, nil
}