Files
RSSHub/lib/routes/blogs/wangyin.js
junbaor e9aacb416e feat: 优化"王垠的博客-当然我在扯淡" (#2646)
* feat: 优化"王垠的博客-当然我在扯淡"

* 删除文章数量参数
2019-07-20 12:49:53 +08:00

48 lines
1.3 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = 'https://www.yinwang.org';
const response = await got({ method: 'get', url });
const $ = cheerio.load(response.data);
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: 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: '王垠的博客 - 当然我在扯淡',
link: url,
item: result,
};
};