redditimages2telegram/Reddit.own

26 lines
853 B
Plaintext
Raw Normal View History

class Reddit {
def fetchSubreddit(subreddit, maxItems = 5) {
url = "https://www.reddit.com/r/" + subreddit + ".json"
data = sync(def(ret) = http(url, combine(::jsondecode, ret))).data ?? []
if (!length(data)) return []
return stream(data.children)
.map(def(child) = child.data)
.limit(min(maxItems, data.dist ?? 0))
.map(def(post) = {
"id": post.id,
"sub": subreddit,
"url": post.url,
"time": post.created_utc,
"title": post.title,
"permalink": post.permalink,
"flair_text": arrayKeyExists("link_flair_text", post) ? post.link_flair_text : ""
})
.toArray()
}
def fetchSubreddits(subreddits, maxItems = 5) =
stream(subreddits)
2023-06-19 12:20:42 +03:00
.flatMap(def(r) = try(def() = this.fetchSubreddit(r, maxItems), def(clazz, cause) = []))
.toArray()
2023-06-19 12:20:42 +03:00
}