Move helpers to Api
This commit is contained in:
parent
6df64a9a7b
commit
dd64dd75ac
@ -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()
|
||||
)
|
||||
|
31
danboo.own
31
danboo.own
@ -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()
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user