mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-09 23:00:48 +08:00
添加对 [D2 资源库日报](https://awesome.fairyever.com/daily/) 的支持。 **注意**: 引入了一个新的依赖:[sanitize-html](https://www.npmjs.com/package/sanitize-html) 用于去除 HTML 中不需要的内容 ```javascript description = sanitizeHtml(description, { allowedTags: ['section', 'h2', 'ul', 'li', 'p', 'span', 'a'], }); ```
51 lines
1.7 KiB
JavaScript
51 lines
1.7 KiB
JavaScript
const axios = require('../../utils/axios');
|
|
const cheerio = require('cheerio');
|
|
const url = require('url');
|
|
const sanitizeHtml = require('sanitize-html');
|
|
|
|
module.exports = async (ctx) => {
|
|
const indexLink = 'https://awesome.fairyever.com/daily/';
|
|
const { data } = await axios.get(indexLink);
|
|
|
|
const $ = cheerio.load(data);
|
|
// 修改 slice 可以获取更多天的内容,暂时最新的一天就够了
|
|
const days = $('ul.menu-list>li>a')
|
|
.slice(0, 1)
|
|
.toArray();
|
|
|
|
const promises = days.map(async (ele) => {
|
|
ele = $(ele);
|
|
const relativePath = ele.attr('href');
|
|
const innerText = ele.text();
|
|
const link = url.resolve(indexLink, relativePath);
|
|
const cache = ctx.cache.get(link);
|
|
if (cache) {
|
|
return cache;
|
|
}
|
|
const { data } = await axios.get(link);
|
|
const $$ = cheerio.load(data);
|
|
let description = $$('article[title*=日报]>section')
|
|
// 去掉文章头部的 logo 和文章尾部的二维码
|
|
.slice(1, -1)
|
|
.toArray()
|
|
.reduce((pre, cur) => pre + $$.html(cur), '');
|
|
description = sanitizeHtml(description, {
|
|
allowedTags: ['section', 'h2', 'ul', 'li', 'p', 'span', 'a'],
|
|
});
|
|
return {
|
|
title: $('head>title').text(),
|
|
pubDate: new Date(innerText.trim()).toUTCString(),
|
|
description,
|
|
guid: link,
|
|
link,
|
|
};
|
|
});
|
|
|
|
ctx.state.data = {
|
|
title: '日报 | D2 资源库',
|
|
link: 'https://awesome.fairyever.com/daily/',
|
|
description: '日报 | D2 资源库',
|
|
item: await Promise.all(promises),
|
|
};
|
|
};
|