89 lines
2.8 KiB
JavaScript
89 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('config');
|
|
const Promise = require('bluebird');
|
|
|
|
const args = require('../cli')();
|
|
const logger = require('../logger')(__filename);
|
|
const methods = require('../methods/methods');
|
|
|
|
async function attachContentInfo(users, { reddit, predata }) {
|
|
return 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].fetchInfo || methods[post.host.method])(post.host, post, {
|
|
predata: predata[post.host.method],
|
|
reddit,
|
|
}),
|
|
},
|
|
];
|
|
} catch (error) {
|
|
if (args.debug) {
|
|
logger.warn(`${error.stack} (${post.permalink})`);
|
|
} else {
|
|
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, {
|
|
predata: predata.redditPreview,
|
|
reddit,
|
|
}),
|
|
},
|
|
];
|
|
}
|
|
|
|
return accPosts;
|
|
}
|
|
}, []),
|
|
},
|
|
}), {});
|
|
}
|
|
|
|
async function getInfo(host, { reddit, url, predata }) {
|
|
if (host === null) {
|
|
try {
|
|
const info = await methods.tube(host, null, {
|
|
reddit,
|
|
predata: predata.tube,
|
|
});
|
|
|
|
return info;
|
|
} catch (error) {
|
|
logger.verbose(`Ignoring unsupported content '${url}'`);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return (methods[host.method].fetchInfo || methods[host.method])(host, null, {
|
|
reddit,
|
|
predata: predata[host.method],
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
attachContentInfo,
|
|
getInfo,
|
|
};
|