feat: add 未名新闻 (#6109)

This commit is contained in:
Ethan Shen
2020-11-08 01:21:41 +00:00
committed by GitHub
parent 1c306746f0
commit 14e948112f
3 changed files with 70 additions and 0 deletions
+12
View File
@@ -1419,6 +1419,18 @@ column 为 third 时可选的 category:
<Route author="HenryQW" example="/wikipedia/mainland" path="/wikipedia/mainland"/>
## 未名新闻
### 分类
<Route author="nczitzk" example="/mitbbs" path="/mitbbs/:caty?" :paramsDesc="['新闻分类,参见下表,默认为“新闻大杂烩”']">
| 新闻大杂烩 | 军事 | 国际 | 体育 | 娱乐 | 科技 | 财经 |
| ---------- | -------- | ------ | ---- | ---- | ---- | ------- |
| | zhongguo | haiwai | tiyu | yule | keji | caijing |
</Route>
## 微信
::: tip 提示
+3
View File
@@ -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'));
+55
View File
@@ -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,
};
};