Move helpers to Api

This commit is contained in:
aNNiMON 2024-08-15 00:19:34 +03:00
parent 6df64a9a7b
commit dd64dd75ac
2 changed files with 47 additions and 31 deletions

View File

@ -10,6 +10,22 @@ class DanbooruApi {
}
}
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)
}
def getPosts(params) {
url = "https://danbooru.donmai.us/posts.json" + params
response = okhttp.request()
@ -91,3 +107,34 @@ class Post {
return src
}
}
// 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()
)

View File

@ -72,34 +72,3 @@ thread(def() {
exit(0)
})
// 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()
)