fix: Readhub cache issue (#1540)

this will hopefully fix #1508
This commit is contained in:
Henry Wang 2019-02-19 03:25:24 +00:00 committed by DIYgod
parent 02e8d64b8b
commit c9e9c06a7e
1 changed files with 48 additions and 44 deletions

View File

@ -43,56 +43,60 @@ module.exports = async (ctx) => {
url: `https://api.readhub.cn/${path}`,
});
const promises = data.map(async (news) => {
const id = news.id;
if (category === 'topic' || category === 'daily') {
const link = `https://readhub.cn/topic/${id}`;
const cache = await ctx.cache.get(link);
if (cache) {
return cache;
}
const { data } = await axios.get(`https://api.readhub.cn/topic/${id}`);
let description = data.summary;
if (data.newsArray) {
description += '<br/><br/>媒体报道:';
for (const one of data.newsArray) {
description += `<br/>${dayjs(new Date(one.publishDate)).format('YY-MM-DD')} ${one.siteName}: <a href='${one.mobileUrl || one.url}'>${one.title}</a>`;
const out = await Promise.all(
data.map(async (news) => {
const id = news.id;
let item;
if (category === 'topic' || category === 'daily') {
const link = `https://readhub.cn/topic/${id}`;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
}
if (data.timeline && data.timeline.topics) {
let type = '相关事件';
if (data.timeline.commonEntities && data.timeline.commonEntities.length > 0) {
type = '事件追踪';
const { data } = await axios.get(`https://api.readhub.cn/topic/${id}`);
let description = data.summary;
if (data.newsArray) {
description += '<br/><br/>媒体报道:';
for (const one of data.newsArray) {
description += `<br/>${dayjs(new Date(one.publishDate)).format('YY-MM-DD')} ${one.siteName}: <a href='${one.mobileUrl || one.url}'>${one.title}</a>`;
}
}
description += `<br/><br/>${type}`;
for (const one of data.timeline.topics) {
description += `<br/>${dayjs(new Date(one.createdAt)).format('YY-MM-DD')} <a href='https://readhub.cn/topic/${one.id}'>${one.title.trim()}</a>`;
if (data.timeline && data.timeline.topics) {
let type = '相关事件';
if (data.timeline.commonEntities && data.timeline.commonEntities.length > 0) {
type = '事件追踪';
}
description += `<br/><br/>${type}`;
for (const one of data.timeline.topics) {
description += `<br/>${dayjs(new Date(one.createdAt)).format('YY-MM-DD')} <a href='https://readhub.cn/topic/${one.id}'>${one.title.trim()}</a>`;
}
}
item = {
title: data.title,
description: description.replace(new RegExp('\n', 'g'), '<br/>'),
pubDate: data.publishDate,
guid: id,
link,
};
} else {
const description = news.summaryAuto || news.summary || news.title;
item = {
title: news.title,
description: description.replace(new RegExp('\n', 'g'), '<br/>'),
pubDate: news.publishDate,
guid: id,
link: news.url || news.mobileUrl,
};
}
const item = {
title: data.title,
description: description.replace(new RegExp('\n', 'g'), '<br/>'),
pubDate: data.publishDate,
guid: id,
link,
};
await ctx.cache.set(link, description, 24 * 60 * 60);
return item;
} else {
const description = news.summaryAuto || news.summary || news.title;
return {
title: news.title,
description: description.replace(new RegExp('\n', 'g'), '<br/>'),
pubDate: news.publishDate,
guid: id,
link: news.url || news.mobileUrl,
};
}
});
await ctx.cache.set(link, JSON.stringify(item), 24 * 60 * 60);
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `Readhub-${title}`,
title: `Readhub - ${title}`,
link,
item: await Promise.all(promises),
item: out,
};
};