101 lines
2.5 KiB
Scala
101 lines
2.5 KiB
Scala
|
use std, functional, types, date
|
||
|
|
||
|
include "own-modules/telegram-bot/TelegramBot.own"
|
||
|
include "config.own"
|
||
|
include "database.own"
|
||
|
include "DanbooruApi.own"
|
||
|
|
||
|
bot = new TelegramBot(config.telegram.token)
|
||
|
api = new DanbooruApi(config.danbooru.auth)
|
||
|
|
||
|
// Get posts from API
|
||
|
posts = api.getPosts(config.danbooru.params)
|
||
|
// Skip previously processed
|
||
|
lastPostId = getLastPostId()
|
||
|
skipToPostId = lastPostId
|
||
|
filteredPosts = stream(posts)
|
||
|
.filter(def(p) = p.isImage(p.post))
|
||
|
.sortBy(def(p) = p.id)
|
||
|
.dropWhile(def(p) = p.id <= skipToPostId)
|
||
|
.peek(def(p) = lastPostId = p.id)
|
||
|
.toArray()
|
||
|
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))
|
||
|
if (blacklisted) {
|
||
|
continue
|
||
|
}
|
||
|
score = post.getScore(p)
|
||
|
|
||
|
for target : config.targets {
|
||
|
minScore = target.minScore ?? 0
|
||
|
if (score < minScore) continue
|
||
|
|
||
|
matched = stream(target.tags)
|
||
|
.anyMatch(def(tt) = arrayKeyExists(tt, p.tags))
|
||
|
if (!matched) continue
|
||
|
|
||
|
url = "https://danbooru.donmai.us/posts/" + p.id
|
||
|
println "%s: %s -> %s".sprintf(newDate(), url, target.name)
|
||
|
|
||
|
caption = getCaption(post)
|
||
|
buttonText = sprintf("%s 📈%d", post.getResolution(p), 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),
|
||
|
"parse_mode": "html",
|
||
|
"caption": caption,
|
||
|
"reply_markup": markup
|
||
|
}, def(r) {
|
||
|
if !r.contains("wrong file identifier") return 1
|
||
|
bot.invoke("sendPhoto", {
|
||
|
"chat_id": target.chat,
|
||
|
"message_thread_id": target.topic,
|
||
|
"photo": post.getSampleImageUrl(p),
|
||
|
"parse_mode": "html",
|
||
|
"caption": "small " + caption,
|
||
|
"reply_markup": markup
|
||
|
}, def(r) = 1)
|
||
|
})
|
||
|
sleep(2000)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
closeDB()
|
||
|
thread(def() {
|
||
|
sleep(2000)
|
||
|
exit(0)
|
||
|
})
|
||
|
|
||
|
// Helpers
|
||
|
def strToHashtag(str) =
|
||
|
"#" + str.toLowerCase()
|
||
|
.replaceAll("[^a-z_0-9\s]", "")
|
||
|
.replaceAll("\s+", "_")
|
||
|
|
||
|
def getCaption(post) {
|
||
|
p = post.post
|
||
|
characters = post.getCharacters(p)
|
||
|
if characters.length > 0 {
|
||
|
charactersHashTag = map(characters, ::strToHashtag).joinToString(" ")
|
||
|
} else {
|
||
|
charactersHashTag = "#original"
|
||
|
}
|
||
|
sourceUrl = post.getSource(p)
|
||
|
return "%s | <a href=\"%s\">source</a>".sprintf(
|
||
|
charactersHashTag,
|
||
|
sourceUrl
|
||
|
)
|
||
|
}
|
||
|
|