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("<", "<").replace(">", ">") def getCaption(post) { tag = "" if (length(post.flair_text) > 0) { tag = " #" + strToHashtag(post.sub + "_" + post.flair_text) } return sprintf( "%s\n" + "🗨 Comments%s\n" + "🔎 SauceNAO, " + "Yandex", 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)