A Discord bot that searches for images on Danbooru.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GoGetMeHentai/danbooru/danbooru.go

160 lines
5.0 KiB

package danbooru
import (
"git.packetlostandfound.us/chiefnoah/GoGetMeHentai/config"
"net/http"
"log"
"io/ioutil"
"encoding/json"
"time"
"math/rand"
"errors"
"strconv"
)
const (
BASE string = "https://danbooru.donmai.us"
POST string = "/posts"
SUFFIX string = ".json"
UTF8 string = "utf8=✓"
)
func SearchPosts(tags []string) (*[]Post, error) {
cfg := config.LoadConfig()
requestString := BASE + POST + "?" + UTF8 + "&tags="
for _, v := range tags {
requestString = requestString + v + "+"
}
requestString = requestString + "&login=" + cfg.DanbooruConfig.DanbooruUser + "&api_key=" + cfg.DanbooruConfig.DanbooruKey;
resp, err := http.Get(requestString)
if err != nil {
log.Printf("Unable to contact Danbooru", err)
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Unable to read response body: ", err)
return nil, err
}
var posts []Post
json.Unmarshal(body, &posts)
return &posts, nil
}
func SearchPost(tags []string) (*Post, error) {
cfg := config.LoadConfig()
log.Printf("First arg: ", tags[0])
firstArg, err := strconv.Atoi(tags[0])
if err == nil && firstArg > 0 {
log.Printf("Argument appears to be post ID... Error:%s\nArg: %d",err, firstArg )
return SearchID(firstArg)
}
requestString := BASE + POST + SUFFIX + "?tags="
for _, v := range tags {
requestString = requestString + v + "+"
}
requestString = requestString + "&login=" + cfg.DanbooruConfig.DanbooruUser + "&api_key=" + cfg.DanbooruConfig.DanbooruKey
//log.Printf("Request string: ", requestString)
resp, err := http.Get(requestString)
if err != nil {
log.Printf("Unable to contact Danbooru", err)
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Unable to read response body: ", err)
return nil, err
}
var posts []Post
json.Unmarshal(body, &posts)
if len(posts) < 1 {
return nil, errors.New("No posts returned")
}
//log.Printf("Posts %+s", body)
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
log.Print("Number of posts returned: ", len(posts))
index := r1.Intn(len(posts))
return &posts[index], nil
}
func SearchID(id int) (*Post, error) {
log.Print("Searching for post by ID")
if id < 1 {
return nil, errors.New("Invalid ID")
}
requestString := BASE + POST + "/" + strconv.Itoa(id) + SUFFIX
log.Printf("Request String: ", requestString)
resp, err := http.Get(requestString)
if err != nil {
log.Printf("Unable to contact Danbooru", err)
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Unable to read response body: ", err)
return nil, err
}
var post Post
json.Unmarshal(body, &post)
return &post, nil
}
type Post struct {
Id int `json:"id"`
CreatedAt string `json:"created_at"`
UploaderID int `json:"uploader_id"`
Score int `json:"score"`
Source string `json:"source"`
MD5 string `json:"md5"`
LastCommentBumpedAt string `json:"last_comment_bumped_at"`
Rating string `json:"s"`
ImageWidth int `json:"image_width"`
ImageHeight int `json:"image_height"`
TagString string `json:"tag_string"`
IsNoteLocked bool `json:"is_note_locked"`
FavCount int `json:"fav_count"`
FileExt string `json:"file_ext"`
LastNotedAt string `json:"last_noted_at"`
IsRatingLocked bool `json:"is_rating_locked"`
ParentID int `json:"parent_id"`
HasChildren bool `json:"has_children"`
ApproverID int `json:"approver_id"`
TagCountGeneral int `json:"tag_count_general"`
TagCountArtist int `json:"tag_count_artist"`
TagCountCharacter int `json:"tag_count_character"`
TagCountCopyright int `json:"tag_count_copyright"`
FileSize int`json:"file_size"`
IsStatusLocked bool `json:"is_status_locked"`
FavString string `json:"fav_string"`
PoolString string `json:"pool_string"`
UpScore int `json:"up_score"`
DownScore int `json:"down_score"`
IsPending bool `json:"is_pending"`
IsFlagged bool `json:"is_flagged"`
IsDeleted bool `json:"is_deleted"`
TagCount int `json:"tag_count"`
UpdatedAt string `json:"updated_at"`
IsBanned bool `json:"is_banned"`
PixivID int `json:"pixiv_id"`
LastCommentedAt string `json:"last_commented_at"`
HasActiveChildren bool `json:"has_active_children"`
BitFlags int `json:"bit_flags"`
UploaderName string `json:"uploader_name"`
HasLarge bool `json:"has_large"`
TagStringArtist string `json:"tag_string_artist"`
TagStringCharacter string `json:"tag_string_character"`
TagStringCopyright string `json:"tag_string_copyright"`
TagStringGeneral string `json:"tag_string_general"`
HasVisibleChildren bool `json:"has_visible_children"`
FileUrl string `json:"file_url"`
LargeFileUrl string `json:"large_file_url"`
PreviewFileUrl string `json:"preview_file_url"`
}