fix: repetitive setting cache in bjnews

This commit is contained in:
DIYgod 2019-05-07 14:32:03 +08:00
parent a1a6243d75
commit 1f152fbebc
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
1 changed files with 7 additions and 13 deletions

View File

@ -7,11 +7,8 @@ module.exports = async (ctx) => {
const $ = cheerio.load(res.data);
const list = $('#news_ul li').get();
const proList = [];
const indexList = [];
const out = await Promise.all(
list.map(async (item, i) => {
list.map(async (item) => {
const $ = cheerio.load(item);
const time = $('p').text();
const title = $('a').text();
@ -20,24 +17,21 @@ module.exports = async (ctx) => {
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const responses = await axios.get(itemUrl);
const $d = cheerio.load(responses.data);
const single = {
title,
pubDate: new Date(time).toUTCString(),
link: itemUrl,
guid: itemUrl,
description: $d('#main .content').html(),
};
proList.push(axios.get(itemUrl));
indexList.push(i);
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
const responses = await axios.all(proList);
for (let i = 0; i < responses.length; i++) {
const res = responses[i];
const $ = cheerio.load(res.data);
out[indexList[i]].description = $('#main .content').html();
ctx.cache.set(out[indexList[i]].link, JSON.stringify(out[i]), 24 * 60 * 60);
}
ctx.state.data = {
title: $('title').text(),
link: url,