e621-sdk-go/example/lowlevel/tag.go

98 lines
2.5 KiB
Go

package main
import (
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
_ "github.com/joho/godotenv/autoload"
"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: Getting a single tag.
log.Println("Getting a single tag: ")
// Specify the tag ID for retrieval.
tagID := "1530" // Replace with the desired tag's ID.
// Call the GetTag function to retrieve the specified tag.
tag, err := endpoints.GetTag(requestContext, tagID)
if err != nil {
log.Println(err)
} else {
// Log the name of the retrieved tag.
log.Println(tag.Name)
}
log.Println("----------")
// Log: Getting a list of tags.
log.Println("Getting a list of tags: ")
// Define query parameters for retrieving a list of tags.
query := map[string]string{
"limit": "5",
}
// Call the GetTags function to retrieve a list of tags based on the query parameters.
tagList, err := endpoints.GetTags(requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the number of tags retrieved.
log.Println(len(tagList))
}
log.Println("----------")
// Log: Searching for tags containing "cat."
log.Println("Searching for tags containing 'cat': ")
// Define query parameters for searching tags that match "cat."
query = map[string]string{
"limit": "5",
"search[name_matches]": "cat*",
}
// Call the GetTags function with the search query to retrieve matching tags.
tagList, err = endpoints.GetTags(requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the retrieved tags.
log.Println(tagList)
}
log.Println("----------")
// Log: Searching for tags with the category "artist."
log.Println("Searching for tags with the category 'artist': ")
// Define query parameters for searching tags in the "artist" category.
query = map[string]string{
"limit": "5",
"search[category]": "1",
}
// Call the GetTags function with the category filter to retrieve artist tags.
tagList, err = endpoints.GetTags(requestContext, query)
if err != nil {
log.Println(err)
} else {
// Log the retrieved artist tags.
log.Println(tagList)
}
log.Println("----------")
}