Last active
May 31, 2020 00:49
-
-
Save reichert621/09518fedc98e166a27f9a43cede40992 to your computer and use it in GitHub Desktop.
Taro Example: Get top posts from favorite subreddits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('superagent'); | |
const _ = require('lodash'); | |
const getTopPosts = async (subreddit, options = {}) => { | |
const {count = 5, interval = 'week'} = options; | |
const sub = `https://www.reddit.com/r/${subreddit}/top.json?sort=top&t=${interval}`; | |
const res = await request.get(sub); | |
const {children: posts} = res.body.data; | |
return posts.slice(0, count).map((post) => { | |
const {title, subreddit, score, url} = post.data; | |
return {title, subreddit, score, url}; | |
}); | |
}; | |
const getTopPostsInFavoriteSubreddits = async ( | |
subreddits, | |
options = {} | |
) => { | |
const {count = 5, interval = 'week'} = options; | |
const promises = subreddits.map((sub) => { | |
return getTopPosts(sub, {count, interval}); | |
}); | |
return Promise.all(promises).then(_.flatten); | |
}; | |
// Run function and verify output | |
getTopPostsInFavoriteSubreddits([ | |
'programming', | |
'javascript', | |
'learnprogramming', | |
]) | |
.then(console.log) | |
.catch(console.log); | |
const main = () => { | |
return getTopPostsInFavoriteSubreddits([ | |
'programming', | |
'javascript', | |
'learnprogramming', | |
]); | |
}; | |
// You must have a default export of the function you want to run | |
// in order for it to be deployed and scheduled | |
module.exports = main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment