This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/example/lowlevel/tag.go

98 lines
2.5 KiB
Go
Raw Normal View History

2023-10-16 19:48:14 +00:00
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"
2023-10-16 19:48:14 +00:00
"log"
"net/http"
"os"
2023-10-16 19:48:14 +00:00
)
func main() {
// Define the request context with essential information.
2023-10-16 19:48:14 +00:00
requestContext := model.RequestContext{
Client: http.Client{},
2023-10-16 19:48:14 +00:00
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
2023-10-16 19:48:14 +00:00
}
// 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)
2023-10-16 19:48:14 +00:00
if err != nil {
log.Println(err)
} else {
// Log the name of the retrieved tag.
log.Println(tag.Name)
2023-10-16 19:48:14 +00:00
}
log.Println("----------")
// Log: Getting a list of tags.
log.Println("Getting a list of tags: ")
// Define query parameters for retrieving a list of tags.
2023-10-16 19:48:14 +00:00
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)
2023-10-16 19:48:14 +00:00
if err != nil {
log.Println(err)
} else {
// Log the number of tags retrieved.
log.Println(len(tagList))
2023-10-16 19:48:14 +00:00
}
log.Println("----------")
// Log: Searching for tags containing "cat."
log.Println("Searching for tags containing 'cat': ")
// Define query parameters for searching tags that match "cat."
2023-10-16 19:48:14 +00:00
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)
2023-10-16 19:48:14 +00:00
if err != nil {
log.Println(err)
} else {
// Log the retrieved tags.
log.Println(tagList)
2023-10-16 19:48:14 +00:00
}
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.
2023-10-16 19:48:14 +00:00
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)
2023-10-16 19:48:14 +00:00
if err != nil {
log.Println(err)
} else {
// Log the retrieved artist tags.
log.Println(tagList)
2023-10-16 19:48:14 +00:00
}
log.Println("----------")
}