ripunzel/src/app.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-04-09 22:26:30 +00:00
'use strict';
const config = require('config');
const Snoowrap = require('snoowrap');
const exiftool = require('node-exiftool');
const exiftoolBin = require('dist-exiftool');
require('array.prototype.flatten').shim();
const reddit = new Snoowrap(config.reddit.api);
const args = require('./cli.js')();
const curatePosts = require('./curate/posts.js');
const attachContentInfo = require('./fetch/info.js');
const fetchSaveContent = require('./fetch/content.js');
2018-05-05 00:27:15 +00:00
const getPosts = require('./sources/getPosts.js')(reddit, args);
const getUserPosts = require('./sources/getUserPosts.js')(reddit, args);
2018-04-18 02:04:39 +00:00
async function getCompleteUserPosts() {
let userPosts = await getUserPosts(args.users);
if (args.posts) {
userPosts = await getPosts(args.posts, userPosts);
}
const curatedUserPosts = curatePosts(userPosts, args);
return attachContentInfo(curatedUserPosts);
}
function fetchSavePosts(userPosts, ep) {
return Promise.all(Object.values(userPosts).map(user => fetchSaveContent(user, ep)));
}
async function initApp() {
const usersProvided = args.users && args.users.length;
const postIdsProvided = args.posts && args.posts.length;
if (!usersProvided && !postIdsProvided) {
return console.log('\x1b[31m%s\x1b[0m', 'Please supply at least one user or post ID. See --help for more details.');
}
try {
const userPosts = await getCompleteUserPosts();
const ep = new exiftool.ExiftoolProcess(exiftoolBin);
await ep.open();
await fetchSavePosts(userPosts, ep);
return ep.close();
} catch (error) {
console.error(error);
}
}
initApp();