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) .flatMap(def(r) = this.fetchSubreddit(r, maxItems)) .toArray() }