feat: add 四川省科学技术厅 (#4284)

This commit is contained in:
Xiaolei Liu
2020-03-24 10:41:06 +08:00
committed by GitHub
parent af20226677
commit ef27e7ca8b
3 changed files with 61 additions and 0 deletions
+3
View File
@@ -2409,4 +2409,7 @@ router.get('/hubu/news/:type', require('./routes/universities/hubu/news'));
// 大连海事大学
router.get('/dlmu/news/:type', require('./routes/universities/dlmu/news'));
// 四川省科学技术厅
router.get('/sckjt/news/:type?', require('./routes/sckjt/news'));
module.exports = router;
+46
View File
@@ -0,0 +1,46 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const baseUrl = 'http://kjt.sc.gov.cn';
const dateRegex = /\((\d{4})-(\d{2})-(\d{2})\)/;
const map = {
tz: '/tz/index.jhtml',
gs: '/gs/index.jhtml',
};
module.exports = async (ctx) => {
const type = ctx.params.type || 'tz';
const response = await got({
method: 'get',
url: baseUrl + map[type],
});
const data = response.data;
const $ = cheerio.load(data);
ctx.state.data = {
title: '四川省科学技术厅',
link: baseUrl,
item: $('div[class="news_middle_top"]')
.next('div')
.children('h2')
.slice(0, 15)
.map((_, elem) => ({
link:
baseUrl +
$(elem)
.children('a')
.attr('href'),
title: $(elem)
.children('a')
.text(),
pubDate: new Date(
$(elem)
.children('span')
.text()
.replace(dateRegex, '$1-$2-$3')
).toUTCString(),
}))
.get(),
};
};