From 804d01d7cff4610c9b22dbe7c9a0887f36a1e25f Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Sat, 5 May 2018 02:27:15 +0200 Subject: [PATCH] Cleaned up entrypoint. --- src/app.js | 88 ++----------------------------- src/archives/getArchivePostIds.js | 19 +++++++ src/sources/getPosts.js | 41 ++++++++++++++ src/sources/getUserPosts.js | 47 +++++++++++++++++ 4 files changed, 110 insertions(+), 85 deletions(-) create mode 100644 src/archives/getArchivePostIds.js create mode 100644 src/sources/getPosts.js create mode 100644 src/sources/getUserPosts.js diff --git a/src/app.js b/src/app.js index 3886d5e..8373636 100644 --- a/src/app.js +++ b/src/app.js @@ -9,21 +9,17 @@ 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 curateUser = require('./curate/user.js'); const interpolate = require('./interpolate.js'); const attachContentInfo = require('./fetch/info.js'); const fetchContent = require('./fetch/content.js'); -const archives = require('./archives/archives.js'); - -const save = require('./save/save.js'); -const saveProfileDetails = require('./save/profileDetails.js'); - -const args = require('./cli.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 or one post with --post . See --help for more options.'); @@ -51,81 +47,3 @@ Promise.resolve().then(() => { return console.error(error); }); -function getUserPosts(usernames) { - return usernames.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 => ({ - user, - accPosts: accPosts.concat(posts) - })); - }).then(({user, accPosts}) => { - if(args.archives || config.fetch.archives.search) { - return getArchivePostIds(username, accPosts.map(post => post.id)).then(postIds => { - return Promise.all(postIds.map(postId => { - return reddit.getSubmission(postId).fetch(); - })); - }).then(archivedPosts => { - return { - user, - accPosts: accPosts.concat(archivedPosts) - }; - }); - } - - return {user, accPosts}; - }).then(({user, accPosts}) => { - return accPosts.map(post => Object.assign(post, {user})); - }); - }, 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; - }); -}; - -function getArchivePostIds(username, exclude) { - console.log('Searching archives for posts...'); - - return Promise.all(config.fetch.archives.reddit.map(source => archives[source](username))).then(postIds => postIds.flatten()).then(postIds => { - return exclude ? postIds.filter(postId => !exclude.includes(postId)) : postIds; - }).then(postIds => { - console.log(`Found ${postIds.length} unique archived posts`); - - return postIds; - }); -}; diff --git a/src/archives/getArchivePostIds.js b/src/archives/getArchivePostIds.js new file mode 100644 index 0000000..5885a7f --- /dev/null +++ b/src/archives/getArchivePostIds.js @@ -0,0 +1,19 @@ +'use strict'; + +const config = require('config'); + +const archives = require('./archives.js'); + +function getArchivePostIds(username, exclude) { + console.log('Searching archives for posts...'); + + return Promise.all(config.fetch.archives.reddit.map(source => archives[source](username))).then(postIds => postIds.flatten()).then(postIds => { + return exclude ? postIds.filter(postId => !exclude.includes(postId)) : postIds; + }).then(postIds => { + console.log(`Found ${postIds.length} unique archived posts`); + + return postIds; + }); +}; + +module.exports = getArchivePostIds; diff --git a/src/sources/getPosts.js b/src/sources/getPosts.js new file mode 100644 index 0000000..eecc8e2 --- /dev/null +++ b/src/sources/getPosts.js @@ -0,0 +1,41 @@ +'use strict'; + +const config = require('config'); + +const curateUser = require('../curate/user.js'); +const saveProfileDetails = require('../save/profileDetails.js'); + +function getPostsWrap(reddit, args) { + return 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; + }); + }; +}; + +module.exports = getPostsWrap; diff --git a/src/sources/getUserPosts.js b/src/sources/getUserPosts.js new file mode 100644 index 0000000..57f24c1 --- /dev/null +++ b/src/sources/getUserPosts.js @@ -0,0 +1,47 @@ +'use strict'; + +const config = require('config'); + +const getArchivePostIds = require('../archives/getArchivePostIds.js'); +const curateUser = require('../curate/user.js'); +const saveProfileDetails = require('../save/profileDetails.js'); + +function getUserPostsWrap(reddit, args) { + return function getUserPosts(usernames) { + return usernames.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 => ({ + user, + accPosts: accPosts.concat(posts) + })); + }).then(({user, accPosts}) => { + if(args.archives || config.fetch.archives.search) { + return getArchivePostIds(username, accPosts.map(post => post.id)).then(postIds => { + return Promise.all(postIds.map(postId => { + return reddit.getSubmission(postId).fetch(); + })); + }).then(archivedPosts => { + return { + user, + accPosts: accPosts.concat(archivedPosts) + }; + }); + } + + return {user, accPosts}; + }).then(({user, accPosts}) => { + return accPosts.map(post => Object.assign(post, {user})); + }); + }, Promise.resolve([])); + }; +}; + +module.exports = getUserPostsWrap;