ripunzel/src/app.js

50 lines
1.4 KiB
JavaScript

'use strict';
const config = require('config');
const util = require('util');
const fs = require('fs-extra');
const snoowrap = require('snoowrap');
require('promise.prototype.finally').shim();
require('array.prototype.flatten').shim();
const reddit = new snoowrap(config.reddit.api);
const args = require('./cli.js');
const curatePosts = require('./curate/posts.js');
const interpolate = require('./interpolate.js');
const attachContentInfo = require('./fetch/info.js');
const fetchContent = require('./fetch/content.js');
const getPosts = require('./sources/getPosts.js')(reddit, args);
const getUserPosts = require('./sources/getUserPosts.js')(reddit, args);
if(!(args.users && args.users.length) && !(args.posts && args.posts.length)) {
return console.log('\x1b[31m%s\x1b[0m', 'Please supply at least one user with --user <user> or one post with --post <post-id>. See --help for more options.');
}
Promise.resolve().then(() => {
if(args.users) {
return getUserPosts(args.users);
}
return [];
}).then(userPosts => {
if(args.posts) {
return getPosts(args.posts).then(posts => posts.concat(userPosts));
}
return userPosts;
}).then(posts => {
return curatePosts(posts, args.ignore).slice(0, args.limit);
}).then(posts => {
return attachContentInfo(posts);
}).then(posts => {
return fetchContent(posts);
}).catch(error => {
return console.error(error);
});