'use strict'; const config = require('config'); const util = require('util'); const fs = require('fs-extra'); const yargs = require('yargs').argv; const snoowrap = require('snoowrap'); const curateSubmissions = require('./curate/submissions.js'); const curateUser = require('./curate/user.js'); const methods = require('./methods/methods.js'); const interpolate = require('./interpolate.js'); const fetchItem = require('./fetchItem.js'); const fetchContent = require('./fetchContent.js'); const save = require('./save.js'); const textToStream = require('./textToStream.js'); const reddit = new snoowrap(config.reddit.api); function saveProfileDetails(user) { if(config.library.profile.image) { // pass profile image as item to interpolate extension variable const filepath = interpolate(config.library.profile.image, user, null, { url: user.profile.image }); fetchItem(user.profile.image).then(stream => save(filepath, stream)).catch(error => { console.log('\x1b[33m%s\x1b[0m', `Could not save profile image for '${user.name}': ${error}`); }); } if(config.library.profile.description) { if(user.profile.description) { const filepath = interpolate(config.library.profile.description, user); const stream = textToStream(user.profile.description); save(filepath, stream).catch(error => { console.log('\x1b[33m%s\x1b[0m', `Could not save profile description for '${user.name}': ${error}`); }); } else { console.log('\x1b[33m%s\x1b[0m', `No profile description for '${user.name}'`); } } }; function getSubmissions(users, sort, limit) { return users.reduce((chain, user) => { return chain.then(acc => { return reddit.getUser(user).getSubmissions({ sort: sort, limit: limit }).then(submissions => { return acc.concat(submissions); }); }); }, Promise.resolve([])); }; if(!yargs.user && typeof yargs.users !== 'string') { return console.log('\x1b[31m%s\x1b[0m', 'Please supply at least one user with --user=[user], or multiple users with --users=[user1,user2] or --user=[user1] --user=[user2]'); } const users = yargs.users ? yargs.users.split(',') : [].concat(yargs.user); users.forEach(user => { return Promise.resolve().then(() => { // get reddit profile return reddit.getUser(user).fetch().then(curateUser); }).then(user => { return saveProfileDetails(user); // get submissions }).catch(error => { return console.log('\x1b[33m%s\x1b[0m', error); }); }); /* Promise.resolve().then(() => { if(yargs.user || yargs.users) { const users = yargs.users ? yargs.users.split(',') : [].concat(yargs.user); return Promise.resolve().then(() => { if(config.library.profile) { return getProfiles(users); } }).then(() => { return getSubmissions(users, yargs.sort || config.reddit.sort, yargs.limit === undefined ? config.reddit.limit : yargs.limit); }); } return Promise.reject('Please supply at least one user with one or multiple --user, or --users!'); }).then(submissions => { 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; }, [])); }).then(posts => { return fetchContent(posts); }).catch(error => { return console.log('\x1b[31m%s\x1b[0m', error); }); */