ripunzel/src/app.js

97 lines
3.0 KiB
JavaScript

'use strict';
const config = require('config');
const util = require('util');
const fs = require('fs-extra');
const snoowrap = require('snoowrap');
const promiseFinally = require('promise.prototype.finally');
const reddit = new snoowrap(config.reddit.api);
const curatePosts = require('./curate/posts.js');
const curateUser = require('./curate/user.js');
const interpolate = require('./interpolate.js');
const attachContentInfo = require('./fetch/info.js');
const fetchContent = require('./fetch/content.js');
const save = require('./save/save.js');
const saveProfileDetails = require('./save/profileDetails.js');
promiseFinally.shim();
const args = require('./cli.js');
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);
}
}).then((userPosts = []) => {
if(args.posts) {
return getPosts(args.posts).then(posts => posts.concat(userPosts));
}
return userPosts;
}).then(posts => {
return curatePosts(posts).slice(0, args.limit);
}).then(posts => {
return attachContentInfo(posts).then(info => fetchContent(posts));
}).catch(error => {
return console.error(error);
});
function getUserPosts(users) {
return users.reduce((chain, username) => {
return chain.then(accPosts => {
return reddit.getUser(username).fetch().then(curateUser).then(saveProfileDetails).then(user => ({user, accPosts}));
}).then(({user, accPosts}) => {
return reddit.getUser(username).getSubmissions({
sort: args.sort,
limit: Infinity
}).then(posts => {
return accPosts.concat(posts.map(post => {
post.user = user;
return post;
}));
});
});
}, Promise.resolve([]));
};
function getPosts(postIds) {
return postIds.reduce((chain, postId) => {
return chain.then(acc => {
return reddit.getSubmission(postId).fetch().then(post => ({post, acc}));
}).then(({post, acc}) => {
if(acc.users[post.author.name]) {
return {post, acc, user: acc.users[post.author.name]}
}
if(post.author.name === '[deleted]') {
return {post, acc, user: {name: '[deleted]'}};
}
return reddit.getUser(post.author.name).fetch().then(curateUser).then(saveProfileDetails).then(user => ({post, acc, user}));
}).then(({post, acc, user}) => {
post.user = user;
acc.posts.push(post);
// keep track of users to prevent fetching one user multiple times
acc.users[user.name] = user;
return acc;
});
}, Promise.resolve({
posts: [],
users: {}
})).then(({posts, users}) => {
return posts;
});
};