ripunzel/app.js

53 lines
1.6 KiB
JavaScript

'use strict';
const config = require('config');
const util = require('util');
const yargs = require('yargs').argv;
const snoowrap = require('snoowrap');
const methods = require('./methods/methods.js');
const curate = require('./curate.js');
const fetchContent = require('./fetchContent.js');
const reddit = new snoowrap(config.reddit.api);
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([]));
};
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;
return post;
}));
} else {
console.log('\x1b[33m%s\x1b[0m', `"${post.title}": '${post.url}' not supported :(`);
}
return acc;
}, []));
}).then(posts => {
return fetchContent(posts);
}).catch(error => {
return console.log('\x1b[33m%s\x1b[0m', error);
});