ripunzel/src/app.js

78 lines
2.1 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 = {};
if (args.users) {
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, args)));
}
async function initApp() {
function watch() {
console.log(`Watch-mode enabled, checking for new posts ${config.fetch.watch.interval} minutes from now.`);
setTimeout(initApp, Math.ceil(config.fetch.watch.interval) * 1000 * 60);
}
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);
await ep.close();
if (args.watch) {
watch();
}
} catch (error) {
console.error(error);
if (args.watch && config.fetch.watch.ignoreErrors) {
watch();
}
}
return true;
}
initApp();