ripunzel/src/sources/getUserPosts.js

88 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-09-11 03:16:54 +00:00
'use strict';
const config = require('config');
const Promise = require('bluebird');
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');
const interpolate = require('../interpolate.js');
2024-09-11 03:16:54 +00:00
async function getUser(username, reddit) {
try {
const user = await reddit.getUser(username).fetch();
return curateUser(user);
} catch (error) {
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
return {
name: username,
fallback: true,
};
}
}
2024-09-11 03:16:54 +00:00
async function getPosts(username, reddit, args) {
try {
const user = await reddit.getUser(username).getSubmissions({
sort: args.sort,
limit: Infinity,
});
return user;
} catch (error) {
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch posts from reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
return [];
}
}
async function getArchivedPosts(username, posts, reddit) {
const postIds = await getArchivePostIds(username, posts.map(post => post.id));
return Promise.all(postIds.map(postId => reddit.getSubmission(postId).fetch()));
}
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 [];
}
}
function getUserPostsWrap(reddit, args) {
return async function getUserPosts(usernames) {
const users = await Promise.map(usernames, async (username) => {
const [user, posts] = await Promise.all([
getUser(username, reddit),
getPosts(username, reddit, args),
]);
const indexed = await getIndexedPosts(user);
if (args.archives) {
posts.push(...await getArchivedPosts(username, posts, reddit));
}
if (posts.length) {
return { ...user, posts, indexed };
}
return null;
});
return users.reduce((userPosts, user) => (user ? { ...userPosts, [user.name]: user } : userPosts), {});
};
}
2024-09-11 03:16:54 +00:00
module.exports = getUserPostsWrap;