From d8be065855bdc998105c7a6f22612af61da674d2 Mon Sep 17 00:00:00 2001 From: cssxsh <32539286+cssxsh@users.noreply.github.com> Date: Fri, 18 Oct 2019 12:18:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=A1=82=E6=9E=97=E7=94=B5?= =?UTF-8?q?=E5=AD=90=E7=A7=91=E6=8A=80=E5=A4=A7=E5=AD=A6=E6=96=B0=E9=97=BB?= =?UTF-8?q?=E8=B5=84=E8=AE=AF=20(#3281)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/university.md | 13 ++++++ lib/router.js | 3 ++ lib/routes/guet/news.js | 95 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 lib/routes/guet/news.js diff --git a/docs/university.md b/docs/university.md index a567967a14..ab3b2d45fc 100644 --- a/docs/university.md +++ b/docs/university.md @@ -275,6 +275,19 @@ xskb1 对应 http://www.auto.uestc.edu.cn/index/xskb1.htm +## 桂林电子科技大学 + +### 新闻资讯 + + + +| 桂电要闻 | 文明校园建设 | 桂电新闻 | 校园快讯 | 学院动态 | 媒体桂电 | 通知公告 | 招标公示 | 学术活动 | +| -------- | ------------ | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| gdyw | wmxyjs | gdxw | xykx | xydt | mtgd | tzgg | zbgs | xshd | + +注 1: 不要吐槽拼音缩写,缩写原本的 URL 构成就这样。 + + ## 哈尔滨工程大学 ### 本科生院工作通知 diff --git a/lib/router.js b/lib/router.js index 71d493feb9..9362e0fa30 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1851,6 +1851,9 @@ router.get('/4gamers/tag/:tag', require('./routes/4gamers/tag')); // 大麦网 router.get('/damai/activity/:city/:category/:subcategory/:keyword?', require('./routes/damai/activity')); +// 桂林电子科技大学新闻资讯 +router.get('/guet/xwzx/:type?', require('./routes/guet/news')); + // はてな匿名ダイアリー router.get('/hatena/anonymous_diary/archive', require('./routes/hatena/anonymous_diary/archive')); diff --git a/lib/routes/guet/news.js b/lib/routes/guet/news.js new file mode 100644 index 0000000000..bcd1304a38 --- /dev/null +++ b/lib/routes/guet/news.js @@ -0,0 +1,95 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const host = 'https://www.guet.edu.cn'; +const infos = { + gdyw: { + path: '/xwzx/gdyw.htm', + title: '桂电要闻', + }, + wmxyjs: { + path: '/xwzx/wmxyjs.htm', + title: '文明校园建设', + }, + gdxw: { + path: '/xwzx/gdxw.htm', + title: '桂电新闻', + }, + xykx: { + path: '/xwzx/xykx.htm', + title: '校园快讯', + }, + xydt: { + path: '/xwzx/xydt.htm', + title: '学院动态', + }, + mtgd: { + path: '/xwzx/mtgd.htm', + title: '媒体桂电', + }, + tzgg: { + path: '/xwzx/tzgg.htm', + title: '通知公告', + }, + zbgs: { + path: '/xwzx/mtgd.htm', + title: '招标公示', + }, + xshd: { + path: '/xwzx/tzgg.htm', + title: '学术活动', + }, +}; + +module.exports = async (ctx) => { + const type = ctx.params.type || 'gdyw'; + if (!infos[type]) { + throw Error('参数不在可选范围之内'); + } + const path = infos[type].path; + + const response = await got(path, { + baseUrl: host, + }); + const data = response.data; + const $ = cheerio.load(data); + const list = $('tr[id^="line"]').find('a'); + + const item = await Promise.all( + list + .map(async (index, element) => { + const href = $(element).attr('href'); + const path = href && href.replace(/\.\./, ''); + const link = host + path; + + const item = await ctx.cache.tryGet(link, async () => { + const result = await got.get(link); + const $ = cheerio.load(result.data); + const title = $('#newTitle').text(); + const publish = $('.newsPublish').text(); + const time = publish + .replace(/(发布时间:|日|分)/g, '') + .replace(/(年|月)/g, '-') + .replace(/时/g, ':'); + const pubDate = new Date(`${time} GMT+0800`).toUTCString(); + const description = $('div[id^="vsb_content"]').html(); + + const single = { + title: title, + link: link, + description: description, + pubDate: pubDate, + }; + return single; + }); + return Promise.resolve(item); + }) + .get() + ); + + ctx.state.data = { + title: infos[type].title, + link: response.url, + item: item, + }; +};