ripunzel/app.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-04-09 22:26:30 +00:00
'use strict';
const config = require('config');
const util = require('util');
2018-04-09 22:26:30 +00:00
const yargs = require('yargs').argv;
const snoowrap = require('snoowrap');
const methods = require('./methods/methods.js');
2018-04-18 02:04:39 +00:00
const curate = require('./curate.js');
const fetchContent = require('./fetchContent.js');
2018-04-09 22:26:30 +00:00
const reddit = new snoowrap(config.reddit.api);
2018-04-09 22:26:30 +00:00
2018-04-18 02:04:39 +00:00
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!');
2018-04-09 22:26:30 +00:00
}).then(submissions => {
2018-04-18 02:04:39 +00:00
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;
}, []));
2018-04-18 02:04:39 +00:00
}).then(posts => {
return fetchContent(posts);
}).catch(error => {
return console.log('\x1b[33m%s\x1b[0m', error);
2018-04-09 22:26:30 +00:00
});