feat: add 中国机械工程学会学会新闻 (#6415)

This commit is contained in:
Ethan Shen 2020-12-13 00:22:02 +08:00 committed by GitHub
parent a7fc1881f8
commit 47dde5e4ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 0 deletions

View File

@ -1934,6 +1934,18 @@ column 为 third 时可选的 category:
</Route>
## 中国机械工程学会
### 学会新闻
<Route author="nczitzk" example="/cmes/news" path="/cmes/news/:category?" :paramsDesc="['分类,见下表,默认为 学会要闻']">
| 学会要闻 | 学会动态 | 科技新闻 |
| ----------- | -------- | -------- |
| Information | Dynamics | TechNews |
</Route>
## 中国劳工观察
### 调查报告

View File

@ -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;

65
lib/routes/cmes/news.js Normal file
View File

@ -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,
};
};