2024-08-10 19:12:42 +03:00
|
|
|
use okhttp, types, json, functional
|
|
|
|
|
|
|
|
class DanbooruApi {
|
|
|
|
def DanbooruApi(auth) {
|
|
|
|
this.headers = {
|
|
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
|
|
|
|
}
|
|
|
|
if auth != "" {
|
|
|
|
this.headers["Authorization"] = auth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 00:19:34 +03:00
|
|
|
def getPost(id) {
|
|
|
|
url = "https://danbooru.donmai.us/posts/" + id + ".json"
|
|
|
|
response = okhttp.request()
|
|
|
|
.headers(this.headers)
|
|
|
|
.url(url)
|
|
|
|
.get()
|
|
|
|
.newCall(okhttp.client)
|
|
|
|
.execute()
|
|
|
|
.body()
|
|
|
|
.string()
|
|
|
|
if (!length(response)) return []
|
|
|
|
post = jsondecode(response)
|
|
|
|
if (typeof(post) != MAP) return []
|
|
|
|
return new Post(post)
|
|
|
|
}
|
|
|
|
|
2024-08-10 19:12:42 +03:00
|
|
|
def getPosts(params) {
|
|
|
|
url = "https://danbooru.donmai.us/posts.json" + params
|
|
|
|
response = okhttp.request()
|
|
|
|
.headers(this.headers)
|
|
|
|
.url(url)
|
|
|
|
.get()
|
|
|
|
.newCall(okhttp.client)
|
|
|
|
.execute()
|
|
|
|
.body()
|
|
|
|
.string()
|
|
|
|
if (!length(response)) return []
|
|
|
|
posts = jsondecode(response)
|
|
|
|
if (typeof(posts) != ARRAY) return []
|
|
|
|
return map(posts, def(p) = new Post(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Post {
|
|
|
|
def Post(post) {
|
2024-08-11 18:30:44 +03:00
|
|
|
this.id = int(post.id) ?? 0
|
|
|
|
this.tags = tomap(post.tag_string.split(" "), def(k) = k, def(v) = 1)
|
2024-08-10 19:12:42 +03:00
|
|
|
this.post = post
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:30:44 +03:00
|
|
|
def isImage() {
|
|
|
|
ext = this.post.file_ext
|
2024-08-10 19:12:42 +03:00
|
|
|
return ext == "jpg" || ext == "png"
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:30:44 +03:00
|
|
|
def getResolution() {
|
|
|
|
w = int(this.post.image_width ?? 0)
|
|
|
|
h = int(this.post.image_height ?? 0)
|
2024-08-10 19:12:42 +03:00
|
|
|
if w == 0 || h == 0 return ""
|
|
|
|
return "" + w + "x" + h
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:30:44 +03:00
|
|
|
def getImageUrl() {
|
|
|
|
fileSize = this.post.file_size ?? 0
|
2024-08-10 19:12:42 +03:00
|
|
|
if (fileSize >= 9500000) {
|
2024-08-11 18:30:44 +03:00
|
|
|
return this.post.large_file_url
|
2024-08-10 19:12:42 +03:00
|
|
|
}
|
2024-08-11 18:30:44 +03:00
|
|
|
return this.post.file_url
|
2024-08-10 19:12:42 +03:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:57:47 +03:00
|
|
|
def getSampleImageUrl() = this.post.large_file_url
|
2024-08-10 19:12:42 +03:00
|
|
|
|
2024-08-11 18:30:44 +03:00
|
|
|
def containsTags(tags) = stream(tags)
|
|
|
|
.anyMatch(def(t) = arrayKeyExists(t, this.tags))
|
2024-08-10 19:12:42 +03:00
|
|
|
|
2024-08-11 18:30:44 +03:00
|
|
|
def getScore() = int(this.post.score ?? 0)
|
|
|
|
|
2024-08-11 18:57:47 +03:00
|
|
|
def getArtists() = match int(this.post.tag_count_artist ?? 0) {
|
|
|
|
case 0: []
|
|
|
|
case 1: [this.post.tag_string_artist]
|
|
|
|
case _: this.post.tag_string_artist.split(" ")
|
2024-08-10 19:12:42 +03:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:57:47 +03:00
|
|
|
def getCopyrights() = match int(this.post.tag_count_copyright ?? 0) {
|
|
|
|
case 0: []
|
|
|
|
case 1: [this.post.tag_string_copyright]
|
|
|
|
case _: this.post.tag_string_copyright.split(" ")
|
|
|
|
}
|
|
|
|
|
|
|
|
def getCharacters() = match int(this.post.tag_count_character ?? 0) {
|
|
|
|
case 0: []
|
|
|
|
case 1: [this.post.tag_string_character]
|
|
|
|
case _: this.post.tag_string_character.split(" ")
|
2024-08-10 19:12:42 +03:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:30:44 +03:00
|
|
|
def getSource() {
|
|
|
|
src = this.post.source
|
2024-08-10 19:12:42 +03:00
|
|
|
if src.contains("twitter") {
|
|
|
|
return src.replace("twitter", "x")
|
|
|
|
}
|
2024-08-11 18:30:44 +03:00
|
|
|
pixivId = this.post.pixiv_id ?? 0
|
2024-08-10 19:12:42 +03:00
|
|
|
if pixivId {
|
|
|
|
return "https://www.pixiv.net/en/artworks/" + pixivId
|
|
|
|
}
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 00:19:34 +03:00
|
|
|
|
|
|
|
// Helpers
|
|
|
|
def strToHashtag(str) =
|
|
|
|
"#" + str.toLowerCase()
|
|
|
|
.replaceAll("[^a-z_0-9\s]", "")
|
|
|
|
.replaceAll("\s+", "_")
|
|
|
|
|
|
|
|
def getCharacterHashtags(post) {
|
|
|
|
characters = post.getCharacters()
|
|
|
|
if characters.isEmpty() {
|
|
|
|
return "#original"
|
|
|
|
}
|
|
|
|
copyrights = post.getCopyrights()
|
|
|
|
hashtags = []
|
|
|
|
for character : characters {
|
|
|
|
hashtag = strToHashtag(character)
|
|
|
|
// Clean-up copyright suffix
|
|
|
|
for copyright : copyrights {
|
|
|
|
replaceFrom = "_" + copyright
|
|
|
|
hashtag = hashtag.replace(replaceFrom, "")
|
|
|
|
}
|
|
|
|
hashtags += hashtag
|
|
|
|
}
|
|
|
|
return hashtags.joinToString(" ")
|
|
|
|
}
|
|
|
|
|
|
|
|
def getCaption(post) = sprintf(
|
|
|
|
"%s | <a href=\"%s\">source</a>",
|
|
|
|
getCharacterHashtags(post),
|
|
|
|
post.getSource()
|
|
|
|
)
|