ripunzel/src/fetch/info.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
const config = require('config');
const Promise = require('bluebird');
const methods = require('../methods/methods.js');
const attachContentInfo = (users, reddit) => Promise.reduce(Object.values(users), async (accUsers, user) => ({
...accUsers,
[user.name]: {
...user,
posts: await Promise.reduce(user.posts, async (accPosts, post) => {
if (!post.host || !methods[post.host.method]) {
console.log('\x1b[33m%s\x1b[0m', `Ignoring unsupported content '${post.url}' (${post.permalink})`);
return accPosts;
}
try {
return [
...accPosts,
{
...post,
content: await methods[post.host.method](post.host, post, reddit),
},
];
} catch (error) {
console.log('\x1b[31m%s\x1b[0m', `${error} (${post.permalink})`);
if (config.fetch.archives.preview && post.preview) {
console.log(`Found preview images for unavailable source '${post.url}' (${post.permalink})`);
return [
...accPosts,
{
...post,
previewFallback: true,
content: await methods.redditPreview(post.host, post),
},
];
}
return accPosts;
}
}, []),
},
}), {});
async function getInfo(host, reddit) {
return methods[host.method](host, null, reddit);
}
module.exports = {
attachContentInfo,
getInfo,
};