2020-05-26 17:49:13 +03:00
|
|
|
use ["std", "math", "http", "json", "functional", "files", "jdbc"]
|
2020-05-27 00:52:16 +03:00
|
|
|
conn = getConnection("jdbc:sqlite:redditimages.db")
|
2020-05-27 00:27:52 +03:00
|
|
|
include "own-modules/telegram-bot/TelegramBot.own"
|
2020-05-27 00:52:16 +03:00
|
|
|
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)
|
2020-05-27 00:52:16 +03:00
|
|
|
reddit = new Reddit()
|
2020-05-26 17:49:13 +03:00
|
|
|
|
2020-05-27 00:52:16 +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
|
|
|
|
2020-05-27 00:52:16 +03:00
|
|
|
debug(jsonencode(media))
|
2020-05-26 17:49:13 +03:00
|
|
|
|
2020-05-27 00:52:16 +03:00
|
|
|
if (length(media) > 0) {
|
|
|
|
bot.sendMediaGroup(config.peer, media)
|
2020-05-26 17:49:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-27 00:52:16 +03:00
|
|
|
stIsPostExists.close()
|
|
|
|
stAddPost.close()
|
|
|
|
conn.close()
|
2020-05-26 17:49:13 +03:00
|
|
|
|
|
|
|
// Helpers
|
2020-05-27 00:52:16 +03:00
|
|
|
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+", "_")
|
|
|
|
|
2020-05-26 18:30:46 +03:00
|
|
|
def safe(str) = str.replace("&", "&")
|
|
|
|
.replace("<", "<").replace(">", ">")
|
|
|
|
|
2020-05-26 17:49:13 +03:00
|
|
|
def getCaption(post) {
|
|
|
|
tag = ""
|
|
|
|
if (length(post.flair_text) > 0) {
|
2020-05-26 18:30:46 +03:00
|
|
|
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",
|
2020-05-26 18:30:46 +03:00
|
|
|
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)
|