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-07-01 22:25:48 +00:00
const fs = require ( 'fs-extra' ) ;
2018-06-12 23:51:45 +00:00
const exiftool = require ( 'node-exiftool' ) ;
const exiftoolBin = require ( 'dist-exiftool' ) ;
2018-07-01 21:16:03 +00:00
const cron = require ( 'node-cron' ) ;
2018-07-01 01:38:29 +00:00
const { format } = require ( 'date-fns' ) ;
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-07-01 22:25:48 +00:00
async function getFileContents ( location , label ) {
try {
const fileContents = await fs . readFile ( location , 'utf8' ) ;
return fileContents . split ( '\n' ) . filter ( entry => entry ) ;
} catch ( error ) {
console . log ( '\x1b[31m%s\x1b[0m' , ` Could not read ${ label } file ' ${ location } ': ${ error } . ` ) ;
return [ ] ;
}
}
2018-06-16 23:11:10 +00:00
async function getCompleteUserPosts ( ) {
2018-06-30 23:07:32 +00:00
let userPosts = { } ;
2018-07-01 22:25:48 +00:00
let usernames = args . users || [ ] ;
let postIds = args . posts || [ ] ;
if ( args . fileUsers ) {
usernames = usernames . concat ( await getFileContents ( args . fileUsers , 'username' ) ) ;
}
if ( args . filePosts ) {
postIds = postIds . concat ( await getFileContents ( args . filePosts , 'post ID' ) ) ;
}
if ( ! usernames . length && ! postIds . length ) {
throw new Error ( 'Could not retrieve any posts. Did you supply --users, --posts, --file-users or --file-posts?' ) ;
}
2018-06-30 23:07:32 +00:00
2018-07-01 22:25:48 +00:00
if ( usernames . length ) {
userPosts = await getUserPosts ( usernames ) ;
2018-06-30 23:07:32 +00:00
}
2018-06-16 23:11:10 +00:00
2018-07-01 22:25:48 +00:00
if ( postIds . length ) {
userPosts = await getPosts ( postIds , userPosts ) ;
2018-06-16 23:11:10 +00:00
}
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 ( ) {
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 ) {
2018-07-01 21:16:03 +00:00
console . log ( ` [ ${ format ( new Date ( ) , 'YYYY-MM-DD HH:mm:ss' ) } ] Watch-mode enabled, checking again for new posts according to crontab ' ${ config . fetch . watch . schedule } '. ` ) ;
2018-07-01 01:06:57 +00:00
}
2018-06-16 23:11:10 +00:00
} catch ( error ) {
2018-07-01 22:25:48 +00:00
console . log ( '\x1b[31m%s\x1b[0m' , error . message ) ;
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 ( ) ;
2018-07-01 21:16:03 +00:00
if ( args . watch ) {
cron . schedule ( config . fetch . watch . schedule , initApp ) ;
}