fix: if fulltext parser failed, return default description and… (#2992)

This commit is contained in:
Cloud
2019-09-04 11:41:05 +08:00
committed by DIYgod
parent b308529545
commit c859291ca3

View File

@@ -19,16 +19,21 @@ module.exports = async (ctx, next) => {
const tasks = ctx.state.data.item.map(async (item) => { const tasks = ctx.state.data.item.map(async (item) => {
const { link, author, description } = item; const { link, author, description } = item;
const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => { const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => {
// if parser failed, return default description and not report error
try {
const res = await got(link); const res = await got(link);
const $ = cheerio.load(res.data); const $ = cheerio.load(res.data);
const result = await mercury_parser.parse(link, { const result = await mercury_parser.parse(link, {
html: $.html(), html: $.html(),
}); });
return result; return result;
} catch (e) {
// no-empty
}
}); });
item.author = author || parsed_result.author; item.author = author || (parsed_result ? parsed_result.author : '');
item.description = parsed_result.content || description; item.description = parsed_result ? parsed_result.content : description;
}); });
await Promise.all(tasks); await Promise.all(tasks);
} }