diff --git a/README.md b/README.md index 2a7143b..376e7f8 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,14 @@ reddit-post-dump requires a arbitrarily recent version of Node.js. Before use, d `node app.js --user={username}` ### Optional parameters -* `--limit={number}`: Maximum amount posts to fetch content from +* `--users={user1,user2}`: You may fetch posts from multiple users by either supplying a comma-separated list of usernames (no spaces) with `--users`, or using multiple individual `--user` arguments +* `--limit={number}`: Maximum amount posts per user to fetch content from * `--sort={method}`: How posts should be sorted while fetched. This affects the `$postIndex` variable, and in combination with a `--limit` decides what posts will be included +### Examples +* `node app.js --user=ThePendulum` +* `node app.js --user=ThePendulum --limit=10 --sort=top` + ## Configuration The default configuration aims to be sensible, and the application may be used without any further tweaking. However, a multitude of options make this utility particularly powerful. diff --git a/app.js b/app.js index 41b1870..29d80bd 100644 --- a/app.js +++ b/app.js @@ -5,31 +5,34 @@ const util = require('util'); const yargs = require('yargs').argv; const snoowrap = require('snoowrap'); const methods = require('./methods/methods.js'); -const dissectLink = require('./dissectLink.js'); +const curate = require('./curate.js'); const fetchContent = require('./fetchContent.js'); const reddit = new snoowrap(config.reddit.api); -reddit.getUser(yargs.user).getSubmissions({ - sort: yargs.sort || config.reddit.sort, - limit: yargs.limit || config.reddit.limit -}).then(submissions => { - const curatedPosts = submissions.map((submission, index) => { - return { - id: submission.id, - index: index, - title: submission.title, - text: submission.selftext, - user: submission.author.name, - permalink: submission.permalink, - url: submission.url, - datetime: submission.created_utc * 1000, - subreddit: submission.subreddit.display_name, - host: dissectLink(submission.url) - }; - }); +function getSubmissions(users) { + return users.reduce((chain, user) => { + return chain.then(acc => { + return reddit.getUser(user).getSubmissions({ + sort: yargs.sort || config.reddit.sort, + limit: yargs.limit || config.reddit.limit + }).then(submissions => { + return acc.concat(submissions); + }); + }); + }, Promise.resolve([])); +}; - return Promise.all(curatedPosts.reduce((acc, post) => { +Promise.resolve().then(() => { + if(yargs.user || yargs.users) { + const users = yargs.users ? yargs.users.split(',') : [].concat(yargs.user); + + return getSubmissions(users); + } + + return Promise.reject('Please supply at least one user with one or multiple --user, or --users!'); +}).then(submissions => { + return Promise.all(curate(submissions).reduce((acc, post) => { if(post.host && methods[post.host.method]) { acc = acc.concat(methods[post.host.method](post).then(content => { post.content = content; @@ -42,6 +45,8 @@ reddit.getUser(yargs.user).getSubmissions({ return acc; }, [])); -}).then(posts => fetchContent(posts)).catch(error => { - console.error(error); +}).then(posts => { + return fetchContent(posts); +}).catch(error => { + return console.log('\x1b[33m%s\x1b[0m', error); }); diff --git a/curate.js b/curate.js new file mode 100644 index 0000000..98349e6 --- /dev/null +++ b/curate.js @@ -0,0 +1,22 @@ +'use strict'; + +const dissectLink = require('./dissectLink.js'); + +function curate(submissions) { + return submissions.map((submission, index) => { + return { + id: submission.id, + index: index, + title: submission.title, + text: submission.selftext, + user: submission.author.name, + permalink: submission.permalink, + url: submission.url, + datetime: submission.created_utc * 1000, + subreddit: submission.subreddit.display_name, + host: dissectLink(submission.url) + }; + }); +}; + +module.exports = curate;