redditimages2telegram/redditimages2telegram.own

67 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-05-26 17:49:13 +03:00
use ["std", "math", "http", "json", "functional", "files", "jdbc"]
conn = getConnection("jdbc:sqlite:redditimages.db")
2020-05-27 00:27:52 +03:00
include "own-modules/telegram-bot/TelegramBot.own"
include "Reddit.own"
include "database.own"
2020-05-26 17:49:13 +03:00
2020-05-27 00:27:52 +03:00
bot = new TelegramBot(config.token)
reddit = new Reddit()
2020-05-26 17:49:13 +03:00
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()
2020-05-26 17:49:13 +03:00
debug(jsonencode(media))
2020-05-26 17:49:13 +03:00
if (length(media) > 0) {
bot.sendMediaGroup(config.peer, media)
2020-05-26 17:49:13 +03:00
}
stIsPostExists.close()
stAddPost.close()
conn.close()
2020-05-26 17:49:13 +03:00
// Helpers
def debug(r) {
// echo(r)
}
2020-05-26 17:49:13 +03:00
def strToHashtag(str) =
str.toLowerCase()
.replaceAll("[^a-z_0-9\s]", "")
.replaceAll("\s+", "_")
def safe(str) = str.replace("&", "&")
.replace("<", "&lt;").replace(">", "&gt;")
2020-05-26 17:49:13 +03:00
def getCaption(post) {
tag = ""
if (length(post.flair_text) > 0) {
tag = " #" + strToHashtag(post.sub + "_" + post.flair_text)
2020-05-26 17:49:13 +03:00
}
return sprintf(
2020-05-27 00:27:52 +03:00
"<a href=\"%s\">%s</a>\n" +
2020-05-26 17:49:13 +03:00
"<a href=\"https://reddit.com%s\">🗨 Comments</a>%s",
safe(post.url),
safe(post.title),
safe(post.permalink),
2020-05-26 17:49:13 +03:00
tag
)
}
sleep(10)
use "java"
System = newClass("java.lang.System")
System.exit(0)