2018-04-09 22:26:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
2018-04-15 01:55:10 +00:00
|
|
|
const util = require('util');
|
2018-04-09 22:26:30 +00:00
|
|
|
const yargs = require('yargs').argv;
|
|
|
|
const snoowrap = require('snoowrap');
|
2018-04-15 01:55:10 +00:00
|
|
|
const methods = require('./methods/methods.js');
|
2018-04-18 02:04:39 +00:00
|
|
|
const curate = require('./curate.js');
|
2018-04-15 01:55:10 +00:00
|
|
|
const fetchContent = require('./fetchContent.js');
|
2018-04-09 22:26:30 +00:00
|
|
|
|
2018-04-17 23:55:20 +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) => {
|
2018-04-15 01:55:10 +00:00
|
|
|
if(post.host && methods[post.host.method]) {
|
|
|
|
acc = acc.concat(methods[post.host.method](post).then(content => {
|
|
|
|
post.content = content;
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}));
|
|
|
|
} else {
|
2018-04-17 22:18:04 +00:00
|
|
|
console.log('\x1b[33m%s\x1b[0m', `"${post.title}": '${post.url}' not supported :(`);
|
2018-04-15 01:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
});
|