2024-09-11 03:16:54 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
const config = require('config');
|
2024-09-11 03:16:55 +00:00
|
|
|
const Promise = require('bluebird');
|
2024-09-11 03:16:56 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const csvParse = Promise.promisify(require('csv').parse);
|
|
|
|
|
2024-09-11 03:16:54 +00:00
|
|
|
const getArchivePostIds = require('../archives/getArchivePostIds.js');
|
|
|
|
const curateUser = require('../curate/user.js');
|
2024-09-11 03:16:56 +00:00
|
|
|
const interpolate = require('../interpolate.js');
|
2024-09-11 03:16:54 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getUser(username, reddit) {
|
2024-09-11 03:16:55 +00:00
|
|
|
try {
|
|
|
|
const user = await reddit.getUser(username).fetch();
|
|
|
|
|
|
|
|
return curateUser(user);
|
2024-09-11 03:16:56 +00:00
|
|
|
} catch (error) {
|
2024-09-11 03:16:55 +00:00
|
|
|
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: username,
|
2024-09-11 03:16:56 +00:00
|
|
|
fallback: true,
|
2024-09-11 03:16:55 +00:00
|
|
|
};
|
2024-09-11 03:16:55 +00:00
|
|
|
}
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:54 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getPosts(username, reddit, args) {
|
2024-09-11 03:16:55 +00:00
|
|
|
try {
|
2024-09-11 03:16:56 +00:00
|
|
|
const user = await reddit.getUser(username).getSubmissions({
|
2024-09-11 03:16:55 +00:00
|
|
|
sort: args.sort,
|
2024-09-11 03:16:56 +00:00
|
|
|
limit: Infinity,
|
2024-09-11 03:16:55 +00:00
|
|
|
});
|
2024-09-11 03:16:56 +00:00
|
|
|
|
|
|
|
return user;
|
|
|
|
} catch (error) {
|
2024-09-11 03:16:55 +00:00
|
|
|
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch posts from reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
|
|
|
|
|
|
|
|
return [];
|
2024-09-11 03:16:55 +00:00
|
|
|
}
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getArchivedPosts(username, posts, reddit) {
|
|
|
|
const postIds = await getArchivePostIds(username, posts.map(post => post.id));
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
return Promise.all(postIds.map(postId => reddit.getSubmission(postId).fetch()));
|
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getIndexedPosts(user) {
|
|
|
|
const indexFilePath = interpolate(config.library.index.file, user, null, null, false);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const indexFile = await fs.readFile(indexFilePath, 'utf8');
|
|
|
|
|
|
|
|
return await csvParse(indexFile, { delimiter: '\t', columns: true, cast: true });
|
|
|
|
} catch (error) {
|
|
|
|
console.log('\x1b[33m%s\x1b[0m', `Could not load index file for '${user.name}' at '${indexFilePath}': ${error}`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
function getUserPostsWrap(reddit, args) {
|
2024-09-11 03:16:56 +00:00
|
|
|
return async function getUserPosts(usernames) {
|
|
|
|
const users = await Promise.map(usernames, async (username) => {
|
2024-09-11 03:16:56 +00:00
|
|
|
const [user, posts] = await Promise.all([
|
|
|
|
getUser(username, reddit),
|
|
|
|
getPosts(username, reddit, args),
|
|
|
|
]);
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
const indexed = await getIndexedPosts(user);
|
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
if (args.archives) {
|
|
|
|
posts.push(...await getArchivedPosts(username, posts, reddit));
|
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
if (posts.length) {
|
2024-09-11 03:16:56 +00:00
|
|
|
return { ...user, posts, indexed };
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return users.reduce((userPosts, user) => (user ? { ...userPosts, [user.name]: user } : userPosts), {});
|
2024-09-11 03:16:56 +00:00
|
|
|
};
|
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:54 +00:00
|
|
|
module.exports = getUserPostsWrap;
|