From 14e948112fe3fad724eb93790efb2ffa5ea0d81f Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 8 Nov 2020 09:21:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=9C=AA=E5=90=8D=E6=96=B0?= =?UTF-8?q?=E9=97=BB=20(#6109)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 12 +++++++++ lib/router.js | 3 +++ lib/routes/mitbbs/index.js | 55 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/routes/mitbbs/index.js diff --git a/docs/new-media.md b/docs/new-media.md index 1aefa6ac9..43cbac6c1 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1419,6 +1419,18 @@ column 为 third 时可选的 category: +## 未名新闻 + +### 分类 + + + +| 新闻大杂烩 | 军事 | 国际 | 体育 | 娱乐 | 科技 | 财经 | +| ---------- | -------- | ------ | ---- | ---- | ---- | ------- | +| | zhongguo | haiwai | tiyu | yule | keji | caijing | + + + ## 微信 ::: tip 提示 diff --git a/lib/router.js b/lib/router.js index 768bb0128..384c7d81c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3468,6 +3468,9 @@ router.get('/grandchallenge/challenges', require('./routes/grandchallenge/challe // 西北工业大学 router.get('/nwpu/:column', require('./routes/nwpu/index')); +// 未名新闻 +router.get('/mitbbs/:caty?', require('./routes/mitbbs/index')); + // 8kcos router.get('/8kcos/', require('./routes/8kcos/latest')); router.get('/8kcos/cat/:cat*', require('./routes/8kcos/cat')); diff --git a/lib/routes/mitbbs/index.js b/lib/routes/mitbbs/index.js new file mode 100644 index 000000000..7f374f0dc --- /dev/null +++ b/lib/routes/mitbbs/index.js @@ -0,0 +1,55 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const caty = ctx.params.caty || ''; + const rootUrl = 'http://www.mitbbs.com'; + const currentUrl = `${rootUrl}/${caty === '' ? 'news/mitbbs_news_zahui.php' : 'news_pg/' + ctx.params.caty + '.html'}`; + const response = await got({ + method: 'get', + url: currentUrl, + responseType: 'buffer', + }); + + const $ = cheerio.load(iconv.decode(response.data, 'gb2312')); + const list = $('tr[align="center"] td a.blue_14p_link, tr[bgcolor="#FFFFFF"] td a.blue_14p_link') + .slice(0, 10) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + pubDate: new Date(item.find('div.weinei_left_con_line_date').text() + ' GMT+8').toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + responseType: 'buffer', + }); + const content = cheerio.load(iconv.decode(detailResponse.data, 'gb2312')); + + const dateTd = content('.black_32p').parent(); + dateTd.find('span, p').remove(); + + item.pubDate = new Date(dateTd.text().trim().replace(/年|月/g, '-').replace('日', ' ')).toUTCString(); + item.description = content('table').eq(5).find('table').eq(1).html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `未名新闻 - ${caty === '' ? '新闻大杂烩' : $('strong').eq(1).text()}`, + link: currentUrl, + item: items, + }; +};