ripunzel/src/fetch/info.js

70 lines
2.0 KiB
JavaScript

'use strict';
const config = require('config');
const Promise = require('bluebird');
const logger = require('../logger')(__filename);
const methods = require('../methods/methods');
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]) {
logger.warn(`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) {
logger.warn(`${error.message} (${post.permalink})`);
if (config.fetch.archives.preview && post.preview) {
logger.info(`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, url) {
if (host === null) {
try {
const info = await methods.tube(host, null, reddit);
return info;
} catch (error) {
logger.verbose(`Ignoring unsupported content '${url}'`);
return null;
}
}
return methods[host.method](host, null, reddit);
}
module.exports = {
attachContentInfo,
getInfo,
};