diff --git a/docs/university.md b/docs/university.md index 541630f141..aa21254ebc 100644 --- a/docs/university.md +++ b/docs/university.md @@ -100,6 +100,10 @@ pageClass: routes +### 研究生院通知 + + + ## 常州大学 ### 教务处 diff --git a/lib/router.js b/lib/router.js index 897e7bbb2d..63e018687e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1265,6 +1265,7 @@ router.get('/21caijing/channel/:name', require('./routes/21caijing/channel')); // 北京邮电大学 router.get('/bupt/yz/:type', require('./routes/universities/bupt/yz')); +router.get('/bupt/grs', require('./routes/universities/bupt/grs')); // VOCUS 方格子 router.get('/vocus/publication/:id', require('./routes/vocus/publication')); diff --git a/lib/routes/universities/bupt/grs.js b/lib/routes/universities/bupt/grs.js new file mode 100644 index 0000000000..507c13b99c --- /dev/null +++ b/lib/routes/universities/bupt/grs.js @@ -0,0 +1,71 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const url = require('url'); + +const base = 'https://grs.bupt.edu.cn'; +const sourceTimezoneOffset = -8; + +module.exports = async (ctx) => { + let out = []; + + const fetch = async (pageIndex) => { + const pageUrl = url.resolve(base, `/list/list.php?p=16_1_${pageIndex}`); + const response = await got({ + method: 'get', + url: pageUrl, + }); + + const $ = cheerio.load(response.data); + const list = $('#news li').get(); + + const result = await Promise.all( + list.map(async (i) => { + const item = $(i); + const itemUrl = url.resolve( + base, + $(item) + .find('a') + .attr('href') + ); + + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const title = $(item) + .find('a') + .attr('title'); + const itemResponse = await got.get(itemUrl); + const itemElement = cheerio.load(itemResponse.data); + const description = itemElement('#news #article').html(); + + const pageInfo = itemElement('#news #date').text(); + const regex = /\d{4}-\d{2}-\d{2}/; + const regRes = regex.exec(pageInfo); + const time = regRes === null ? new Date() : new Date(regRes[0]); + time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000); + + const single = { + title: title, + description: description, + pubDate: time.toUTCString(), + link: itemUrl, + guid: itemUrl, + }; + + ctx.cache.set(itemUrl, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + out = out.concat(result); + }; + + await Promise.all([1, 2].map(async (value) => await fetch(value))); + + ctx.state.data = { + title: '北京邮电大学研究生院', + link: 'https://grs.bupt.edu.cn/list/list.php?p=16_1_1', + item: out, + }; +};