From dd58a1b6e4248dd9abafb1391be3cc67382c5af2 Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Tue, 7 Jul 2020 17:32:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E6=BA=90'=E9=B8=9F=E5=93=A5=E7=AC=94=E8=AE=B0'=EF=BC=88?= =?UTF-8?q?=E4=BB=8A=E6=97=A5=E4=BA=8B,=E5=88=86=E7=B1=BB=E7=9B=AE?= =?UTF-8?q?=E5=BD=95)=20(#5005)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 11 +++++ lib/router.js | 4 ++ lib/routes/niaogebiji/cat.js | 86 ++++++++++++++++++++++++++++++++++ lib/routes/niaogebiji/today.js | 29 ++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 lib/routes/niaogebiji/cat.js create mode 100644 lib/routes/niaogebiji/today.js diff --git a/docs/new-media.md b/docs/new-media.md index c50b05e76..803d9e7e9 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -219,12 +219,23 @@ pageClass: routes ### 首页 + ## Nautilus ### 话题 +## 鸟哥笔记 + +### 今日事 + + + +### 分类目录 + + + ## PMCAFF ### 今日推荐 / 精选 diff --git a/lib/router.js b/lib/router.js index a6d139fc1..3cbd83707 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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; diff --git a/lib/routes/niaogebiji/cat.js b/lib/routes/niaogebiji/cat.js new file mode 100644 index 000000000..f15238e08 --- /dev/null +++ b/lib/routes/niaogebiji/cat.js @@ -0,0 +1,86 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +/** + * 在一个CheerioElement数组中查找有你想要的属性的元素 + * @author KotoriK + * @param {Array} 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} 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: '获取失败!', + }, + ], + }; +}; diff --git a/lib/routes/niaogebiji/today.js b/lib/routes/niaogebiji/today.js new file mode 100644 index 000000000..ae9a6197d --- /dev/null +++ b/lib/routes/niaogebiji/today.js @@ -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 + `
原文链接`, + link: `https://www.niaogebiji.com/pc/bulletin/detail?id=${item.id}`, + pubDate: new Date(parseInt(data[0].pub_time + '000')), + })) + : [ + { + title: '获取失败!', + }, + ], + }; +};