Use non-static functions

This commit is contained in:
aNNiMON 2024-08-11 18:30:44 +03:00
parent 17b3f00029
commit dabb0b95ab
2 changed files with 38 additions and 39 deletions

View File

@ -29,59 +29,62 @@ class DanbooruApi {
class Post { class Post {
def Post(post) { def Post(post) {
this.id = int(post.id) ?? 0 this.id = int(post.id) ?? 0
post.tags = tomap(post.tag_string.split(" "), def(k) = k, def(v) = 1) this.tags = tomap(post.tag_string.split(" "), def(k) = k, def(v) = 1)
this.post = post this.post = post
} }
def isImage(post) { def isImage() {
ext = post.file_ext ext = this.post.file_ext
return ext == "jpg" || ext == "png" return ext == "jpg" || ext == "png"
} }
def getResolution(post) { def getResolution() {
w = int(post.image_width ?? 0) w = int(this.post.image_width ?? 0)
h = int(post.image_height ?? 0) h = int(this.post.image_height ?? 0)
if w == 0 || h == 0 return "" if w == 0 || h == 0 return ""
return "" + w + "x" + h return "" + w + "x" + h
} }
def getImageUrl(post) { def getImageUrl() {
fileSize = post.file_size ?? 0 fileSize = this.post.file_size ?? 0
if (fileSize >= 9500000) { if (fileSize >= 9500000) {
return post.large_file_url return this.post.large_file_url
} }
return post.file_url return this.post.file_url
} }
def getSampleImageUrl(post) { def getSampleImageUrl() {
return post.large_file_url return this.post.large_file_url
} }
def getScore(post) = int(post.score ?? 0) def containsTags(tags) = stream(tags)
.anyMatch(def(t) = arrayKeyExists(t, this.tags))
def getArtists(post) { def getScore() = int(this.post.score ?? 0)
return match int(post.tag_count_artist ?? 0) {
def getArtists() {
return match int(this.post.tag_count_artist ?? 0) {
case 0: [] case 0: []
case 1: [post.tag_string_artist] case 1: [this.post.tag_string_artist]
case _: post.tag_string_artist.split(" ") case _: this.post.tag_string_artist.split(" ")
} }
} }
def getCharacters(post) { def getCharacters() {
return match int(post.tag_count_character ?? 0) { return match int(this.post.tag_count_character ?? 0) {
case 0: [] case 0: []
case 1: [post.tag_string_character] case 1: [this.post.tag_string_character]
case _: post.tag_string_character.split(" ") case _: this.post.tag_string_character.split(" ")
} }
} }
def getSource(post) { def getSource() {
src = post.source src = this.post.source
if src.contains("twitter") { if src.contains("twitter") {
return src.replace("twitter", "x") return src.replace("twitter", "x")
} }
pixivId = post.pixiv_id ?? 0 pixivId = this.post.pixiv_id ?? 0
if pixivId { if pixivId {
return "https://www.pixiv.net/en/artworks/" + pixivId return "https://www.pixiv.net/en/artworks/" + pixivId
} }

View File

@ -14,7 +14,7 @@ posts = api.getPosts(config.danbooru.params)
lastPostId = getLastPostId() lastPostId = getLastPostId()
skipToPostId = lastPostId skipToPostId = lastPostId
filteredPosts = stream(posts) filteredPosts = stream(posts)
.filter(def(p) = p.isImage(p.post)) .filter(def(p) = p.isImage())
.sortBy(def(p) = p.id) .sortBy(def(p) = p.id)
.dropWhile(def(p) = p.id <= skipToPostId) .dropWhile(def(p) = p.id <= skipToPostId)
.peek(def(p) = lastPostId = p.id) .peek(def(p) = lastPostId = p.id)
@ -24,35 +24,32 @@ setLastPostId(lastPostId)
// Process // Process
blacklistTags = config.danbooru.blacklistTags blacklistTags = config.danbooru.blacklistTags
for post : filteredPosts { for post : filteredPosts {
p = post.post
// Check global blacklist // Check global blacklist
blacklisted = stream(blacklistTags) blacklisted = post.containsTags(blacklistTags)
.anyMatch(def(tt) = arrayKeyExists(tt, p.tags))
if (blacklisted) { if (blacklisted) {
continue continue
} }
score = post.getScore(p) score = post.getScore()
for target : config.targets { for target : config.targets {
minScore = target.minScore ?? 0 minScore = target.minScore ?? 0
if (score < minScore) continue if (score < minScore) continue
matched = stream(target.tags) matched = post.containsTags(target.tags)
.anyMatch(def(tt) = arrayKeyExists(tt, p.tags))
if (!matched) continue if (!matched) continue
url = "https://danbooru.donmai.us/posts/" + p.id url = "https://danbooru.donmai.us/posts/" + post.id
println "%s: %s -> %s".sprintf(newDate(), url, target.name) println "%s: %s -> %s".sprintf(newDate(), url, target.name)
caption = getCaption(post) caption = getCaption(post)
buttonText = sprintf("%s 📈%d", post.getResolution(p), score) buttonText = sprintf("%s 📈%d", post.getResolution(), score)
markup = jsonencode({"inline_keyboard": [ markup = jsonencode({"inline_keyboard": [
[{"text":buttonText, "url":url}] [{"text":buttonText, "url":url}]
]}) ]})
bot.invoke("sendPhoto", { bot.invoke("sendPhoto", {
"chat_id": target.chat, "chat_id": target.chat,
"message_thread_id": target.topic, "message_thread_id": target.topic,
"photo": post.getImageUrl(p), "photo": post.getImageUrl(),
"parse_mode": "html", "parse_mode": "html",
"caption": caption, "caption": caption,
"reply_markup": markup "reply_markup": markup
@ -61,7 +58,7 @@ for post : filteredPosts {
bot.invoke("sendPhoto", { bot.invoke("sendPhoto", {
"chat_id": target.chat, "chat_id": target.chat,
"message_thread_id": target.topic, "message_thread_id": target.topic,
"photo": post.getSampleImageUrl(p), "photo": post.getSampleImageUrl(),
"parse_mode": "html", "parse_mode": "html",
"caption": "small " + caption, "caption": "small " + caption,
"reply_markup": markup "reply_markup": markup
@ -84,14 +81,13 @@ def strToHashtag(str) =
.replaceAll("\s+", "_") .replaceAll("\s+", "_")
def getCaption(post) { def getCaption(post) {
p = post.post characters = post.getCharacters()
characters = post.getCharacters(p)
if characters.length > 0 { if characters.length > 0 {
charactersHashTag = map(characters, ::strToHashtag).joinToString(" ") charactersHashTag = map(characters, ::strToHashtag).joinToString(" ")
} else { } else {
charactersHashTag = "#original" charactersHashTag = "#original"
} }
sourceUrl = post.getSource(p) sourceUrl = post.getSource()
return "%s | <a href=\"%s\">source</a>".sprintf( return "%s | <a href=\"%s\">source</a>".sprintf(
charactersHashTag, charactersHashTag,
sourceUrl sourceUrl