rss: natgeo use Promise.all

This commit is contained in:
DIYgod 2018-07-28 12:13:16 +08:00
parent 65d3a406f4
commit 0815931782
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
1 changed files with 27 additions and 23 deletions

View File

@ -18,30 +18,34 @@ module.exports = async (ctx) => {
const list = $('.td-ss-main-content').find('.td-animation-stack');
const out = [];
for (let i = 0; i < list.length; i++) {
const each = $(list[i]);
const storyLink = each.find('a[itemprop=url]').attr('href');
const item = {
title: each.find('a[itemprop=url]').attr('title'),
pubDate: each.find('time').attr('datetime'),
link: storyLink,
guid: storyLink.match(/\d+/g)[0],
};
const key = `${ctx.params.cat}${type}${item.guid}`;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
// 获取全文
const storyDetail = await axios_ins.get(item.link);
const data = storyDetail.data;
const $ = cheerio.load(data);
item.description = $('.td-post-content').html();
ctx.cache.set(key, item.description, 12 * 60 * 60);
}
out.push(item);
const count = [];
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
}
const out = await Promise.all(
count.map(async (i) => {
const each = $(list[i]);
const storyLink = each.find('a[itemprop=url]').attr('href');
const item = {
title: each.find('a[itemprop=url]').attr('title'),
pubDate: each.find('time').attr('datetime'),
link: storyLink,
guid: storyLink.match(/\d+/g)[0],
};
const key = `${ctx.params.cat}${type}${item.guid}`;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
const storyDetail = await axios_ins.get(item.link);
const data = storyDetail.data;
const $ = cheerio.load(data);
item.description = $('.td-post-content').html();
ctx.cache.set(key, item.description, 12 * 60 * 60);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: $('title').text(),
link: url,