diff --git a/DanbooruApi.own b/DanbooruApi.own index 7b2bc76..770a361 100644 --- a/DanbooruApi.own +++ b/DanbooruApi.own @@ -29,59 +29,62 @@ class DanbooruApi { class Post { def Post(post) { - this.id = int(post.id) ?? 0 - post.tags = tomap(post.tag_string.split(" "), def(k) = k, def(v) = 1) + this.id = int(post.id) ?? 0 + this.tags = tomap(post.tag_string.split(" "), def(k) = k, def(v) = 1) this.post = post } - def isImage(post) { - ext = post.file_ext + def isImage() { + ext = this.post.file_ext return ext == "jpg" || ext == "png" } - def getResolution(post) { - w = int(post.image_width ?? 0) - h = int(post.image_height ?? 0) + def getResolution() { + w = int(this.post.image_width ?? 0) + h = int(this.post.image_height ?? 0) if w == 0 || h == 0 return "" return "" + w + "x" + h } - def getImageUrl(post) { - fileSize = post.file_size ?? 0 + def getImageUrl() { + fileSize = this.post.file_size ?? 0 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) { - return post.large_file_url + def getSampleImageUrl() { + 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) { - return match int(post.tag_count_artist ?? 0) { + def getScore() = int(this.post.score ?? 0) + + def getArtists() { + return match int(this.post.tag_count_artist ?? 0) { case 0: [] - case 1: [post.tag_string_artist] - case _: post.tag_string_artist.split(" ") + case 1: [this.post.tag_string_artist] + case _: this.post.tag_string_artist.split(" ") } } - def getCharacters(post) { - return match int(post.tag_count_character ?? 0) { + def getCharacters() { + return match int(this.post.tag_count_character ?? 0) { case 0: [] - case 1: [post.tag_string_character] - case _: post.tag_string_character.split(" ") + case 1: [this.post.tag_string_character] + case _: this.post.tag_string_character.split(" ") } } - def getSource(post) { - src = post.source + def getSource() { + src = this.post.source if src.contains("twitter") { return src.replace("twitter", "x") } - pixivId = post.pixiv_id ?? 0 + pixivId = this.post.pixiv_id ?? 0 if pixivId { return "https://www.pixiv.net/en/artworks/" + pixivId } diff --git a/danboo.own b/danboo.own index b527227..67fb028 100644 --- a/danboo.own +++ b/danboo.own @@ -14,7 +14,7 @@ posts = api.getPosts(config.danbooru.params) lastPostId = getLastPostId() skipToPostId = lastPostId filteredPosts = stream(posts) - .filter(def(p) = p.isImage(p.post)) + .filter(def(p) = p.isImage()) .sortBy(def(p) = p.id) .dropWhile(def(p) = p.id <= skipToPostId) .peek(def(p) = lastPostId = p.id) @@ -24,35 +24,32 @@ setLastPostId(lastPostId) // Process blacklistTags = config.danbooru.blacklistTags for post : filteredPosts { - p = post.post // Check global blacklist - blacklisted = stream(blacklistTags) - .anyMatch(def(tt) = arrayKeyExists(tt, p.tags)) + blacklisted = post.containsTags(blacklistTags) if (blacklisted) { continue } - score = post.getScore(p) + score = post.getScore() for target : config.targets { minScore = target.minScore ?? 0 if (score < minScore) continue - matched = stream(target.tags) - .anyMatch(def(tt) = arrayKeyExists(tt, p.tags)) + matched = post.containsTags(target.tags) 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) caption = getCaption(post) - buttonText = sprintf("%s 📈%d", post.getResolution(p), score) + buttonText = sprintf("%s 📈%d", post.getResolution(), score) markup = jsonencode({"inline_keyboard": [ [{"text":buttonText, "url":url}] ]}) bot.invoke("sendPhoto", { "chat_id": target.chat, "message_thread_id": target.topic, - "photo": post.getImageUrl(p), + "photo": post.getImageUrl(), "parse_mode": "html", "caption": caption, "reply_markup": markup @@ -61,7 +58,7 @@ for post : filteredPosts { bot.invoke("sendPhoto", { "chat_id": target.chat, "message_thread_id": target.topic, - "photo": post.getSampleImageUrl(p), + "photo": post.getSampleImageUrl(), "parse_mode": "html", "caption": "small " + caption, "reply_markup": markup @@ -84,14 +81,13 @@ def strToHashtag(str) = .replaceAll("\s+", "_") def getCaption(post) { - p = post.post - characters = post.getCharacters(p) + characters = post.getCharacters() if characters.length > 0 { charactersHashTag = map(characters, ::strToHashtag).joinToString(" ") } else { charactersHashTag = "#original" } - sourceUrl = post.getSource(p) + sourceUrl = post.getSource() return "%s | source".sprintf( charactersHashTag, sourceUrl