From 147b31b779a3f66f8a329c6be6a66e7d6326bf65 Mon Sep 17 00:00:00 2001 From: Bachhhhhhh <49983774+wooddance@users.noreply.github.com> Date: Tue, 4 Feb 2020 20:48:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8C=97=E5=A4=A7=E6=9C=AA=E5=90=8D=20?= =?UTF-8?q?BBS=20=E5=85=A8=E7=AB=99=E5=8D=81=E5=A4=A7=20(#3870)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/install/README.md | 3 ++ docs/university.md | 12 +++++ lib/config.js | 3 ++ lib/router.js | 1 + lib/routes/universities/pku/bbs/hot.js | 69 ++++++++++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 lib/routes/universities/pku/bbs/hot.js diff --git a/docs/install/README.md b/docs/install/README.md index 095dc51ca..42045ae39 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -396,3 +396,6 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式 - `FANFOU_CONSUMER_SECRET`: 饭否 Consumer Secret - `FANFOU_USERNAME`: 饭否登录用户名、邮箱、手机号 - `FANFOU_PASSWORD`: 饭否密码 + +- 北大未名 BBS 全站十大 + - `PKUBBS_COOKIE`: BBS 注册用户登录后的 Cookie 值,获取方式:1.登录后打开论坛首页 2. 打开控制台 3. 刷新 4. 找到 请求 5. 找到请求头中的 Cookie diff --git a/docs/university.md b/docs/university.md index f4b9faff5..be0e71234 100644 --- a/docs/university.md +++ b/docs/university.md @@ -58,6 +58,18 @@ pageClass: routes +### 北大未名 BBS 全站十大 + + + +::: warning 注意 + +论坛部分帖子正文内容的获取需要用户登录后的 Cookie 值,详情见部署页面的配置模块。 + +::: + + + ## 北京航空航天大学 ### 北京航空航天大学 diff --git a/lib/config.js b/lib/config.js index c2e784d1c..2149d9f04 100644 --- a/lib/config.js +++ b/lib/config.js @@ -107,6 +107,9 @@ const calculateValue = () => { username: envs.FANFOU_USERNAME, password: envs.FANFOU_PASSWORD, }, + pkubbs: { + cookie: envs.PKUBBS_COOKIE, + }, }; }; calculateValue(); diff --git a/lib/router.js b/lib/router.js index 1b2f3b141..e17e13c08 100644 --- a/lib/router.js +++ b/lib/router.js @@ -586,6 +586,7 @@ router.get('/lit/tw/:name?', require('./routes/universities/lit/tw')); router.get('/pku/eecs/:type?', require('./routes/universities/pku/eecs')); router.get('/pku/rccp/mzyt', require('./routes/universities/pku/rccp/mzyt')); router.get('/pku/cls/lecture', require('./routes/universities/pku/cls/lecture')); +router.get('/pku/bbs/hot', require('./routes/universities/pku/bbs/hot')); // 上海海事大学 router.get('/shmtu/www/:type', require('./routes/universities/shmtu/www')); diff --git a/lib/routes/universities/pku/bbs/hot.js b/lib/routes/universities/pku/bbs/hot.js new file mode 100644 index 000000000..dc524c914 --- /dev/null +++ b/lib/routes/universities/pku/bbs/hot.js @@ -0,0 +1,69 @@ +const config = require('@/config').value; +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const resolveUrl = require('url').resolve; + +function parseDate(text) { + const match = /[0-9 :-]+/.exec(text); + if (match) { + return new Date(match[0]).toUTCString(); + } +} + +module.exports = async (ctx) => { + const cookie = config.pkubbs.cookie; + const headers = {}; + if (cookie) { + headers.cookie = cookie; + } + const r = await got('https://bbs.pku.edu.cn/v2/hot-topic.php', { headers }); + const $ = cheerio.load(r.body); + const listItems = $('#list-content .list-item') + .map(function() { + return { + url: resolveUrl( + r.url, + $(this) + .find('> a.link') + .attr('href') + ), + title: $(this) + .find('.title') + .text(), + }; + }) + .get() + .slice(0, 10); + + const item = await Promise.all( + listItems.map( + async ({ url, title }) => + await ctx.cache.tryGet(url, async () => { + // try catch 处理部分无 Cookie 时无法访问的帖子 + try { + const r = await got(url, { headers }); + const $ = cheerio.load(r.body); + return { + title, + description: $('.post-card:first-child .content').html(), + link: url, + guid: url, + pubDate: parseDate($('.post-card:first-child .sl-triangle-container .title span').text()), + }; + } catch (error) { + return { + title, + link: url, + guid: url, + }; + } + }) + ) + ); + ctx.state.data = { + title: '北大未名BBS 全站十大', + link: 'https://bbs.pku.edu.cn/v2/hot-topic.php', + description: '北大未名BBS 全站热门话题前十名', + item, + }; +};