feat: 优化"王垠的博客-当然我在扯淡" (#2646)

* feat: 优化"王垠的博客-当然我在扯淡"

* 删除文章数量参数
This commit is contained in:
junbaor
2019-07-20 12:49:53 +08:00
committed by DIYgod
parent fc3a2a760d
commit e9aacb416e
+30 -12
View File
@@ -3,27 +3,45 @@ const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = 'https://www.yinwang.org';
const response = await got({
method: 'get',
url,
});
const response = await got({ method: 'get', url });
const $ = cheerio.load(response.data);
const resultItem = $("li[class='list-group-item title']")
.map((index, elem) => {
elem = $(elem);
const list = $("li[class='list-group-item title']")
.map((i, e) => {
const element = $(e);
const title = element.find('a').text();
const link = url + element.find('a').attr('href');
return {
title: elem.find('a').text(),
description: elem.find('a').text(),
link: url + elem.find('a').attr('href'),
title: title,
description: '',
link: link,
author: '王垠',
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link);
const itemElement = cheerio.load(itemReponse.data);
item.description = itemElement('.inner').html();
ctx.cache.set(link, JSON.stringify(item));
return Promise.resolve(item);
})
);
ctx.state.data = {
title: '王垠的博客-当然我在扯淡',
title: '王垠的博客 - 当然我在扯淡',
link: url,
item: resultItem,
item: result,
};
};