diff --git a/docs/bbs.md b/docs/bbs.md index 9458bd32c..b5b18dc87 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -357,6 +357,16 @@ pageClass: routes ## 水木社区 +### 分区文章 + + + +| 社区管理 | 国内院校 | 休闲娱乐 | 五湖四海 | 游戏运动 | 社会信息 | 知性感性 | 文化人文 | 学术科学 | 电脑技术 | +| --------- | ---------- | ------------- | -------- | -------- | -------- | -------- | -------- | -------- | ---------- | +| community | university | entertainment | location | game | society | romance | culture | science | technology | + + + ### 用户文章 diff --git a/lib/router.js b/lib/router.js index 3eea3fc1d..b3f196f86 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3096,6 +3096,7 @@ router.get('/telecompaper/news/:caty/:year?/:country?/:type?/:keyword?', require // 水木社区 router.get('/newsmth/account/:id', require('./routes/newsmth/account')); +router.get('/newsmth/section/:section', require('./routes/newsmth/section')); // Kotaku router.get('/kotaku/story/:type', require('./routes/kotaku/story')); diff --git a/lib/routes/newsmth/section.js b/lib/routes/newsmth/section.js new file mode 100644 index 000000000..bf72265c4 --- /dev/null +++ b/lib/routes/newsmth/section.js @@ -0,0 +1,100 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const date = require('@/utils/date'); + +const config = { + community: { + link: '/section/7fba65e45f678eb8c605d4107de04185', + title: '社区管理', + }, + university: { + link: '/section/4fcab28694a0be93d9297d8cede052d9', + title: '国内院校', + }, + entertainment: { + link: '/section/3497e48bb537373d0f738b41fe53a41b', + title: '休闲娱乐', + }, + location: { + link: '/section/353fdfda1dfe7a714e592bab99c762cd', + title: '五湖四海', + }, + game: { + link: '/section/c8d614e56acb8a192ec4af8b375a5eea', + title: '游戏运动', + }, + society: { + link: '/section/5b634fdc9ecddf6042561c959176c077', + title: '社会信息', + }, + romance: { + link: '/section/1c455a5dccf4242008d188f9676e3f4e', + title: '知性感性', + }, + culture: { + link: '/section/12af235486fde6684e4b9e83f5d2b779', + title: '文化人文', + }, + science: { + link: '/section/4ed7f0d8b621c8ccf9e11eca9991d6dc', + title: '学术科学', + }, + technology: { + link: '/section/4dda79c64b3ffb61f8048d745292ff5d', + title: '电脑技术', + }, +}; + +module.exports = async (ctx) => { + const cfg = config[ctx.params.section]; + if (!cfg) { + throw Error('Bad section. See docs'); + } + + const rootUrl = `https://exp.newsmth.net/statistics/hot`; + const currentUrl = `${rootUrl}${cfg.link}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + const $ = cheerio.load(response.data); + + const list = $('ul.article-list li') + .slice(0, 15) + .map((_, item) => { + item = $(item); + const a = item.find('a.article-subject'); + const pubDate = date(item.find('div.article-account-name span').text()); + + return { + title: a.text(), + link: `https://exp.newsmth.net${a.attr('href')}`, + pubDate: pubDate, + }; + }) + .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, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('div.show-content').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: cfg.title + ' - 水木社区', + link: currentUrl, + item: items, + }; +};