From 47dde5e4ef43b773825a7dcdcb88e80588ca4524 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 13 Dec 2020 00:22:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E4=B8=AD=E5=9B=BD=E6=9C=BA?= =?UTF-8?q?=E6=A2=B0=E5=B7=A5=E7=A8=8B=E5=AD=A6=E4=BC=9A=E5=AD=A6=E4=BC=9A?= =?UTF-8?q?=E6=96=B0=E9=97=BB=20(#6415)?= 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/cmes/news.js | 65 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 lib/routes/cmes/news.js diff --git a/docs/new-media.md b/docs/new-media.md index 4e0129d9e..aaed639be 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1934,6 +1934,18 @@ column 为 third 时可选的 category: +## 中国机械工程学会 + +### 学会新闻 + + + +| 学会要闻 | 学会动态 | 科技新闻 | +| ----------- | -------- | -------- | +| Information | Dynamics | TechNews | + + + ## 中国劳工观察 ### 调查报告 diff --git a/lib/router.js b/lib/router.js index 84212d575..e2c515fe9 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3698,4 +3698,7 @@ router.get('/treasury/press-releases/:category?/:title?', require('./routes/us/t // Bandisoft router.get('/bandisoft/:id?/:lang?', require('./routes/bandisoft/index')); +// 中国机械工程学会 +router.get('/cmes/news/:category?', require('./routes/cmes/news')); + module.exports = router; diff --git a/lib/routes/cmes/news.js b/lib/routes/cmes/news.js new file mode 100644 index 000000000..9abf15bc3 --- /dev/null +++ b/lib/routes/cmes/news.js @@ -0,0 +1,65 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const category = ctx.params.category || 'Information'; + + const rootUrl = 'https://www.cmes.org'; + const currentUrl = `${rootUrl}/News/${category}/index.html`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.r_ct') + .find('a') + .slice(0, 10) + .map((_, item) => { + item = $(item); + + const link = item.attr('href'); + + let date = '00000000'; + if (link.split('/')[2] !== 'mkc.ckcest.cn') { + date = link.split('/')[5]; + } + + return { + link, + title: item.text(), + pubDate: new Date(`${date.substr(0, 4)}-${date.substr(5, 2)}-${date.substr(7, 2)}`).toDateString(), + }; + }) + .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); + + content('h3, script, .bshare-custom').remove(); + + item.description = content('.xhjj').html() || content('.detail-page').html(); + + if (content('#articleTime').html()) { + item.pubDate = new Date(content('#articleTime').text()).toUTCString(); + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};