2018-04-09 22:26:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
2018-06-16 23:11:10 +00:00
|
|
|
const Snoowrap = require('snoowrap');
|
2018-06-12 23:51:45 +00:00
|
|
|
const exiftool = require('node-exiftool');
|
|
|
|
const exiftoolBin = require('dist-exiftool');
|
2018-05-04 22:51:58 +00:00
|
|
|
|
|
|
|
require('array.prototype.flatten').shim();
|
2018-04-22 21:46:14 +00:00
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
const reddit = new Snoowrap(config.reddit.api);
|
|
|
|
const args = require('./cli.js')();
|
2018-04-22 23:50:07 +00:00
|
|
|
|
2018-04-29 00:02:34 +00:00
|
|
|
const curatePosts = require('./curate/posts.js');
|
2018-04-22 23:50:07 +00:00
|
|
|
|
2018-04-29 00:02:34 +00:00
|
|
|
const attachContentInfo = require('./fetch/info.js');
|
2018-06-14 23:06:31 +00:00
|
|
|
const fetchSaveContent = require('./fetch/content.js');
|
2018-04-22 21:46:14 +00:00
|
|
|
|
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
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
async function getCompleteUserPosts() {
|
2018-06-30 23:07:32 +00:00
|
|
|
let userPosts = {};
|
|
|
|
|
|
|
|
if (args.users) {
|
|
|
|
userPosts = await getUserPosts(args.users);
|
|
|
|
}
|
2018-06-16 23:11:10 +00:00
|
|
|
|
|
|
|
if (args.posts) {
|
|
|
|
userPosts = await getPosts(args.posts, userPosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
const curatedUserPosts = curatePosts(userPosts, args);
|
|
|
|
|
|
|
|
return attachContentInfo(curatedUserPosts);
|
2018-04-22 21:46:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
function fetchSavePosts(userPosts, ep) {
|
2018-07-01 01:26:26 +00:00
|
|
|
return Promise.all(Object.values(userPosts).map(user => fetchSaveContent(user, ep, args)));
|
2018-06-16 23:11:10 +00:00
|
|
|
}
|
2018-06-12 23:51:45 +00:00
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
async function initApp() {
|
2018-07-01 01:06:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
const usersProvided = args.users && args.users.length;
|
|
|
|
const postIdsProvided = args.posts && args.posts.length;
|
2018-05-04 22:51:58 +00:00
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
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.');
|
2018-04-29 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 23:11:10 +00:00
|
|
|
try {
|
|
|
|
const userPosts = await getCompleteUserPosts();
|
|
|
|
const ep = new exiftool.ExiftoolProcess(exiftoolBin);
|
|
|
|
|
|
|
|
await ep.open();
|
|
|
|
await fetchSavePosts(userPosts, ep);
|
2018-06-17 01:39:12 +00:00
|
|
|
await ep.close();
|
2018-07-01 01:06:57 +00:00
|
|
|
|
|
|
|
if (args.watch) {
|
|
|
|
watch();
|
|
|
|
}
|
2018-06-16 23:11:10 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2018-07-01 01:06:57 +00:00
|
|
|
|
|
|
|
if (args.watch && config.fetch.watch.ignoreErrors) {
|
|
|
|
watch();
|
|
|
|
}
|
2018-06-16 23:11:10 +00:00
|
|
|
}
|
2018-06-17 01:39:12 +00:00
|
|
|
|
|
|
|
return true;
|
2018-06-16 23:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initApp();
|