redditimages2telegram/redditimages.own
2020-10-29 17:25:39 +02:00

69 lines
1.7 KiB
Plaintext

use ["std", "math", "http", "json", "functional", "files", "jdbc"]
conn = getConnection("jdbc:sqlite:redditimages.db")
include "own-modules/telegram-bot/TelegramBot.own"
include "Reddit.own"
include "database.own"
bot = new TelegramBot(config.token)
reddit = new Reddit()
subreddits = reddit.fetchSubreddits(config.subreddits, config["items-in-top"])
media = stream(subreddits)
.filter(def(p) = reduce([".jpg", ".png"], false, def(acc, ext) = acc || indexOf(p.url, ext) > 0))
.filter(::isPostUnique)
//.peek(def(p) = bot.sendPhoto(config.peer, p.url))
.peek(::addPost)
.map(def(p) = {
"type": "photo",
"media": p.url,
"caption": getCaption(p),
"parse_mode": "html"
})
.limit(10)
.toArray()
debug(jsonencode(media))
if (length(media) > 0) {
bot.sendMediaGroup(config.peer, media)
}
stIsPostExists.close()
stAddPost.close()
conn.close()
// Helpers
def debug(r) {
// echo(r)
}
def strToHashtag(str) =
str.toLowerCase()
.replaceAll("[^a-z_0-9\s]", "")
.replaceAll("\s+", "_")
def safe(str) = str.replace("&", "&")
.replace("<", "&lt;").replace(">", "&gt;")
def getCaption(post) {
tag = ""
if (length(post.flair_text) > 0) {
tag = " #" + strToHashtag(post.sub + "_" + post.flair_text)
}
return sprintf(
"<a href=\"%s\">%s</a>\n" +
"<a href=\"https://reddit.com%s\">🗨 Comments</a>%s\n" +
"🔎 <a href=\"https://saucenao.com/search.php?url=%s\">SauceNAO</a>, " +
"<a href=\"https://yandex.ua/images/search?rpt=imageview&url=%s\">Yandex</a>",
safe(post.url), safe(post.title),
safe(post.permalink), tag,
urlencode(post.url),
urlencode(post.url)
)
}
sleep(4000)
use "java"
System = newClass("java.lang.System")
System.exit(0)