From 352b2a66d7500ed851b8609727fba5cc6454ef2d Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:56 +0200 Subject: [PATCH] Allow usernames and post IDs to be read from file. --- .gitignore | 2 ++ src/app.js | 44 ++++++++++++++++++++++++++++++----------- src/cli.js | 8 ++++++++ src/sources/getPosts.js | 2 +- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index c692139..4bb4335 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ config/*.js !config/default.js output/ dist/ +users +posts diff --git a/src/app.js b/src/app.js index 4b025db..f8e2d43 100644 --- a/src/app.js +++ b/src/app.js @@ -2,6 +2,7 @@ const config = require('config'); const Snoowrap = require('snoowrap'); +const fs = require('fs-extra'); const exiftool = require('node-exiftool'); const exiftoolBin = require('dist-exiftool'); const cron = require('node-cron'); @@ -20,15 +21,41 @@ const fetchSaveContent = require('./fetch/content.js'); const getPosts = require('./sources/getPosts.js')(reddit, args); const getUserPosts = require('./sources/getUserPosts.js')(reddit, args); +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 []; + } +} + async function getCompleteUserPosts() { let userPosts = {}; + let usernames = args.users || []; + let postIds = args.posts || []; - if (args.users) { - userPosts = await getUserPosts(args.users); + if (args.fileUsers) { + usernames = usernames.concat(await getFileContents(args.fileUsers, 'username')); } - if (args.posts) { - userPosts = await getPosts(args.posts, userPosts); + 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?'); + } + + if (usernames.length) { + userPosts = await getUserPosts(usernames); + } + + if (postIds.length) { + userPosts = await getPosts(postIds, userPosts); } const curatedUserPosts = curatePosts(userPosts, args); @@ -41,13 +68,6 @@ function fetchSavePosts(userPosts, 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); @@ -60,7 +80,7 @@ async function initApp() { 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}'.`); } } catch (error) { - console.error(error); + console.log('\x1b[31m%s\x1b[0m', error.message); } return true; diff --git a/src/cli.js b/src/cli.js index 770bbaa..02140b3 100644 --- a/src/cli.js +++ b/src/cli.js @@ -11,11 +11,19 @@ function getArgs() { describe: 'Reddit usernames to fetch posts from', type: 'array', }) + .option('file-users', { + describe: 'Load reddit usernames from file', + type: 'string', + }) .option('posts', { alias: 'post', describe: 'Reddit post IDs to fetch', type: 'array', }) + .option('file-posts', { + describe: 'Load reddit post IDs from file', + type: 'string', + }) .option('limit', { describe: 'Maximum amount of posts to fetch per supplied user (!), after filtering out ignored, cross- and reposts', type: 'number', diff --git a/src/sources/getPosts.js b/src/sources/getPosts.js index 724ef6c..50565e6 100644 --- a/src/sources/getPosts.js +++ b/src/sources/getPosts.js @@ -38,7 +38,7 @@ const getPostsWrap = reddit => function getPosts(postIds, userPosts = {}) { } const user = await getUser(post.author.name, reddit); - const { profile, posts: indexed } = await getIndexedPosts(user); + const { profile, posts: indexed } = await getIndex(user); return { ...accUserPosts, [post.author.name]: { ...user, posts: [post], indexed: { profile, original: indexed, updated: [] } } }; }), userPosts);