diff --git a/docs/university.md b/docs/university.md index 5c608d788d..1c4625f7b1 100644 --- a/docs/university.md +++ b/docs/university.md @@ -774,6 +774,24 @@ category 列表: +### 南京理工大学电光学院 + + + +grade 列表: + +| 本科 2016 级 | 本科 2017 级 | 本科 2018 级 | 本科 2019 级 | +| ------------ | ------------ | ------------ | ------------ | +| 16 | 17 | 18 | 19 | + +type 列表: + +| 年级通知(通知公告) | 每日动态(主任寄语) | +| -------------------- | -------------------- | +| tz | dt | + + + ## 南京林业大学 ### 教务处 diff --git a/lib/router.js b/lib/router.js index 1b356b02e9..c54f7dfad6 100644 --- a/lib/router.js +++ b/lib/router.js @@ -663,6 +663,7 @@ router.get('/cczu/news/:category?', require('./routes/universities/cczu/news')); router.get('/njust/jwc/:type', require('./routes/universities/njust/jwc')); router.get('/njust/cwc/:type', require('./routes/universities/njust/cwc')); router.get('/njust/gs/:type', require('./routes/universities/njust/gs')); +router.get('/njust/eo/:grade?/:type?', require('./routes/universities/njust/eo')); // 四川旅游学院 router.get('/sctu/xgxy', require('./routes/universities/sctu/information-engineer-faculty/index')); diff --git a/lib/routes/universities/njust/eo/index.js b/lib/routes/universities/njust/eo/index.js new file mode 100644 index 0000000000..646a91a68b --- /dev/null +++ b/lib/routes/universities/njust/eo/index.js @@ -0,0 +1,50 @@ +const date = require('@/utils/date'); +const cheerio = require('cheerio'); +const { getContent } = require('@/routes/universities/njust/eo/util'); + +const map = new Map([ + ['16tz', { title: '南京理工大学电光16 -- 通知公告', id: '/_t217/tzgg/list.htm' }], + ['16dt', { title: '南京理工大学电光16 -- 主任寄语', id: '/_t217/zrjy/list.htm' }], + ['17tz', { title: '南京理工大学电光17 -- 年级通知', id: '/_t689/njtz/list.htm' }], + ['17dt', { title: '南京理工大学电光17 -- 每日动态', id: '/_t689/mrdt/list.htm' }], + ['18tz', { title: '南京理工大学电光18 -- 年级通知', id: '/_t900/njtz_10234/list.htm' }], + ['18dt', { title: '南京理工大学电光18 -- 主任寄语', id: '/_t900/zrjy_10251/list.htm' }], + ['19tz', { title: '南京理工大学电光19 -- 通知公告', id: '/_t1163/tzgg_11606/list.htm' }], + ['19dt', { title: '南京理工大学电光19 -- 每日动态', id: '/_t1163/mrdt_11608/list.htm' }], +]); + +const baseUrl = 'http://dgxg.njust.edu.cn'; + +module.exports = async (ctx) => { + const grade = ctx.params.grade || '17'; + const type = ctx.params.type || 'tz'; + const category = grade + type; + const id = map.get(category).id; + + const html = await getContent(baseUrl + id); + const $ = cheerio.load(html); + const list = $('li.list_item'); + + ctx.state.data = { + title: map.get(category).title, + link: baseUrl + '/' + id.split('/')[1] + '/main.htm', + item: + list && + list + .map((index, item) => ({ + title: $(item) + .find('a') + .text() + .trim(), + pubDate: date( + $(item) + .find('span.Article_PublishDate') + .text() + ), + link: $(item) + .find('a') + .attr('href'), + })) + .get(), + }; +}; diff --git a/lib/routes/universities/njust/eo/util.js b/lib/routes/universities/njust/eo/util.js new file mode 100644 index 0000000000..1a7ed66ebd --- /dev/null +++ b/lib/routes/universities/njust/eo/util.js @@ -0,0 +1,28 @@ +const delay = (timeout = 1000) => new Promise((resolve) => setTimeout(resolve, timeout)); + +async function getContent(url) { + const browser = await require('@/utils/puppeteer')(); + try { + const page = await browser.newPage(); + page.waitForNavigation({ + timeout: 20000, + }); + // 更改 window.navigator.webdriver 值以避开反爬 + await page.evaluateOnNewDocument(() => { + // eslint-disable-next-line no-undef + Object.defineProperty(navigator, 'webdriver', { + get: () => undefined, + }); + }); + await page.goto(url); + await delay(1000); + const content = await page.content(); + return content; + } finally { + browser.close(); + } +} + +module.exports = { + getContent, +}; diff --git a/lib/routes/universities/njust/gs/index.js b/lib/routes/universities/njust/gs/index.js index 03261bdc82..f2e00865a2 100644 --- a/lib/routes/universities/njust/gs/index.js +++ b/lib/routes/universities/njust/gs/index.js @@ -1,40 +1,42 @@ -const got = require('@/utils/got'); +const date = require('@/utils/date'); const cheerio = require('cheerio'); +const { getContent } = require('@/routes/universities/njust/eo/util'); const map = new Map([ [1, { title: '南京理工大学研究生院 -- 通知公告', id: '/sytzgg_4568' }], [2, { title: '南京理工大学研究生院 -- 学术公告', id: '/xshdggl' }], ]); +const baseUrl = 'http://gs.njust.edu.cn'; + module.exports = async (ctx) => { const type = Number.parseInt(ctx.params.type); const id = map.get(type).id; - const baseUrl = 'http://gs.njust.edu.cn'; - const res = await got({ - method: 'get', - url: baseUrl + id + '/list.htm', - headers: { - Referer: 'https://gs.njust.edu.cn/', - }, - }); - const $ = cheerio.load(res.data); - const list = $('.wp_article_list li').slice(0, 10); + + const html = await getContent(baseUrl + id + '/list.htm'); + const $ = cheerio.load(html); + const list = $('li.list_item'); ctx.state.data = { title: map.get(type).title, - link: 'https://gs.njust.edu.cn', + link: 'http://gs.njust.edu.cn', item: list && list - .map((index, item) => { - item = $(item); - - return { - title: item.find('a').text(), - link: `${baseUrl + $(item.find('a')).attr('href')}`, - pubDate: new Date(item.find('.Article_PublishDate').text()).toUTCString(), - }; - }) + .map((index, item) => ({ + title: $(item) + .find('a') + .text() + .trim(), + pubDate: date( + $(item) + .find('span.Article_PublishDate') + .text() + ), + link: $(item) + .find('a') + .attr('href'), + })) .get(), }; };