Skip to content

Instantly share code, notes, and snippets.

@reichert621
Last active May 31, 2020 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reichert621/09518fedc98e166a27f9a43cede40992 to your computer and use it in GitHub Desktop.
Save reichert621/09518fedc98e166a27f9a43cede40992 to your computer and use it in GitHub Desktop.
Taro Example: Get top posts from favorite subreddits
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