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)
|
2023-08-13 19:50:17 +03:00
|
|
|
reddit = new Reddit(config.cookie)
|
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)
|
2023-10-28 14:47:24 +03:00
|
|
|
.filter(def(p) = reduce([".jpg", ".jpeg", ".png"], false, def(acc, ext) = acc || indexOf(p.url, ext) > 0))
|
2020-05-27 00:52:16 +03:00
|
|
|
.filter(::isPostUnique)
|
|
|
|
//.peek(def(p) = bot.sendPhoto(config.peer, p.url))
|
2023-06-11 14:06:18 +03:00
|
|
|
.limit(10)
|
2020-05-27 00:52:16 +03:00
|
|
|
.peek(::addPost)
|
|
|
|
.map(def(p) = {
|
|
|
|
"type": "photo",
|
|
|
|
"media": p.url,
|
|
|
|
"caption": getCaption(p),
|
|
|
|
"parse_mode": "html"
|
|
|
|
})
|
|
|
|
.toArray()
|
2020-05-26 17:49:13 +03:00
|
|
|
|
2023-06-11 14:21:00 +03:00
|
|
|
debug(media)
|
2020-05-26 17:49:13 +03:00
|
|
|
|
2023-10-28 14:47:24 +03:00
|
|
|
while (length(media) > 0) {
|
|
|
|
r = bot.sendMediaGroupSync(config.peer, media)
|
|
|
|
if (r.ok) break
|
|
|
|
mediaIndex = try(def() = parseInt(r.description.replaceAll(".*failed.*?#(\\d+).*", "$1")), -1)
|
|
|
|
if (mediaIndex == -1) break
|
|
|
|
media = arraySplice(media, mediaIndex - 1, 1)
|
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) {
|
2023-06-11 14:21:00 +03:00
|
|
|
// echo(jsonencode(r))
|
2020-05-27 00:52:16 +03:00
|
|
|
}
|
|
|
|
|
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 = ""
|
2023-06-11 14:21:00 +03:00
|
|
|
if (length(post.flair_text ?? "") > 0) {
|
2020-05-26 18:30:46 +03:00
|
|
|
tag = " #" + strToHashtag(post.sub + "_" + post.flair_text)
|
2023-06-11 14:21:00 +03:00
|
|
|
} else if (length(post.link_flair_text ?? "") > 0) {
|
|
|
|
tag = " #" + strToHashtag(post.sub + "_" + post.link_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" +
|
2023-06-11 14:06:18 +03:00
|
|
|
"<a href=\"https://teddit.zaggy.nl%s\">🗨 Comments</a>%s\n" +
|
2020-10-29 17:25:39 +02:00
|
|
|
"🔎 <a href=\"https://saucenao.com/search.php?url=%s\">SauceNAO</a>, " +
|
2023-06-11 14:06:18 +03:00
|
|
|
"<a href=\"https://www.alamy.com/search.html?imageurl=%s\">Alamy</a>",
|
2020-10-29 17:25:39 +02:00
|
|
|
safe(post.url), safe(post.title),
|
|
|
|
safe(post.permalink), tag,
|
|
|
|
urlencode(post.url),
|
|
|
|
urlencode(post.url)
|
2020-05-26 17:49:13 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:25:39 +02:00
|
|
|
sleep(4000)
|
2020-05-26 17:49:13 +03:00
|
|
|
use "java"
|
|
|
|
System = newClass("java.lang.System")
|
|
|
|
System.exit(0)
|