feat:增加订阅源'鸟哥笔记'(今日事,分类目录) (#5005)

This commit is contained in:
KotoriK
2020-07-07 17:32:26 +08:00
committed by GitHub
parent 566096ff40
commit dd58a1b6e4
4 changed files with 130 additions and 0 deletions
+11
View File
@@ -219,12 +219,23 @@ pageClass: routes
### 首页
<Route author="EsuRt" example="/mittrchina/article" path="/mittrchina"/>
## Nautilus
### 话题
<Route author="emdoe" example="/nautilus/topic/Art" path="/nautilus/topic/:tid" :paramsDesc="['话题 id, 可在页面上方 TOPICS 栏目处找到']"/>
## 鸟哥笔记
### 今日事
<Route author="KotoriK" example="/ngbj/today" path="/ngbj/today"/>
### 分类目录
<Route author="KotoriK" example="/ngbj/cat/103" path="/ngbj/cat/:cat" :paramsDesc="['如https://www.niaogebiji.com/cat/103,最后的数字就是要填写在这的id']"/>
## PMCAFF
### 今日推荐 / 精选
+4
View File
@@ -2940,4 +2940,8 @@ router.get('/gov/chongqing/ljxq/zwgk/:caty', require('./routes/gov/chongqing/ljx
// 国家突发事件预警信息发布网
router.get('/12379', require('./routes/12379/index'));
// 鸟哥笔记
router.get('/ngbj/today', require('./routes/niaogebiji/today'));
router.get('/ngbj/cat/:cat', require('./routes/niaogebiji/cat'));
module.exports = router;
+86
View File
@@ -0,0 +1,86 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
/**
* 在一个CheerioElement数组中查找有你想要的属性的元素
* @author KotoriK
* @param {Array<CheerioElement>} eles CheerioElement数组
* @param {string} attrName 要查找的属性名称
* @param {boolean} recursive 是否递归子节点
* @returns 有你想要元素的数组
*/
function findAttributeFromNodes(eles, attrName, recursive = false) {
let array = [];
for (const ele of eles) {
if (ele.attribs && ele.attribs[attrName]) {
array.push(ele);
}
if (recursive && ele.children) {
array = array.concat(findAttributeFromNodes(ele.children, attrName, true));
}
}
return array;
}
/**
* 在一个CheerioElement数组中查找有你给的属性及对应的值的元素
* @author KotoriK
* @param {Array<CheerioElement>} eles CheerioElement数组
* @param {string} attrName 要查找的属性名称
* @param {boolean} recursive 是否递归子节点
* @returns 有你想要元素的数组
*/
function findMatch(eles, attrName, value, recursive = false) {
let array = [];
for (const ele of eles) {
if (ele.attribs && ele.attribs[attrName] === value) {
array.push(ele);
}
if (recursive && ele.children) {
array = array.concat(findMatch(ele.children, attrName, value, true));
}
}
return array;
}
module.exports = async (ctx) => {
const cat_id = ctx.params.cat;
const response = await got({
method: 'get',
url: `http://www.niaogebiji.com/cat/${cat_id}`,
});
const $ = cheerio.load(response.data);
const cat_name = $('h1').text();
const articles = $('div.articleBox.clearfix');
ctx.state.data = {
title: `鸟哥笔记-分类-${cat_name}`,
link: `http://www.niaogebiji.com/cat/${cat_id}`,
item: response.data
? await Promise.all(
articles.toArray().map((element) => {
const article_div = findAttributeFromNodes(element.children, 'href')[0];
const article_link = `http://www.niaogebiji.com${article_div.attribs.href}`;
return (async () => ({
title: findMatch(article_div.children, 'class', 'articleTitle elp', true)[0].children[0].data,
category: cat_name,
description: await ctx.cache.tryGet(`ngbj-${article_div.attribs.href}`, async () => {
// get article
const article_response = await got({
method: 'get',
url: article_link,
});
const article_dom = cheerio.load(article_response.data);
return article_dom('div.mobileHide.pc_content').html();
}),
link: article_link,
pubDate: parseInt(element.attribs['data-timepoint'] + '000'),
}))();
})
)
: [
{
title: '获取失败!',
},
],
};
};
+29
View File
@@ -0,0 +1,29 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const response = await got({
method: 'post',
url: 'https://www.niaogebiji.com/pc/bulletin/index',
form: {
page: 1,
pub_time: '',
isfromajax: 1,
},
});
const data = response.data.return_data;
ctx.state.data = {
title: '鸟哥笔记-今日事',
link: 'https://www.niaogebiji.com/pc/bulletin/index',
item: data
? data.map((item) => ({
title: item.title,
description: item.content + `</br><a href="${item.link}">原文链接</a>`,
link: `https://www.niaogebiji.com/pc/bulletin/detail?id=${item.id}`,
pubDate: new Date(parseInt(data[0].pub_time + '000')),
}))
: [
{
title: '获取失败!',
},
],
};
};