From 148e017f3119adb04808ac2b2bd9916d3a4483ac Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 24 May 2020 12:57:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B5=99=E6=B1=9F=E5=A4=A7=E5=AD=A6?= =?UTF-8?q?=E8=BD=AF=E4=BB=B6=E5=AD=A6=E9=99=A2=20RSS=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=86=85=E5=AE=B9=20(#4808)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/university.md | 6 +- lib/routes/universities/zju/cst/index.js | 72 ++++++++++++++++-------- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/docs/university.md b/docs/university.md index 999cbb270..53a44f9ab 100644 --- a/docs/university.md +++ b/docs/university.md @@ -1574,9 +1574,9 @@ type 列表: -| 全部通知 | 招生信息 | 教务管理 | 论文管理 | 思政工作 | 评奖评优 | 实习就业 | 国内合作科研 | 国际合作科研 | | -| -------- | -------- | -------- | -------- | -------- | -------- | -------- | ------------ | ------------ | - | -| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | +| 全部通知 | 招生信息 | 教务管理 | 论文管理 | 思政工作 | 评奖评优 | 实习就业 | 国际实习 | 国内合作科研 | 国际合作科研 | +| -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | ------------ | ------------ | +| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | diff --git a/lib/routes/universities/zju/cst/index.js b/lib/routes/universities/zju/cst/index.js index 2cd81c213..01935b73a 100644 --- a/lib/routes/universities/zju/cst/index.js +++ b/lib/routes/universities/zju/cst/index.js @@ -4,21 +4,22 @@ const cheerio = require('cheerio'); const host = 'http://www.cst.zju.edu.cn/'; const map = new Map([ - [1, { id: '32178', title: '浙大软件学院-招生信息' }], - [2, { id: '36216', title: '浙大软件学院-教务管理' }], - [3, { id: '36217', title: '浙大软件学院-论文管理' }], - [4, { id: '36224', title: '浙大软件学院-思政工作' }], - [5, { id: '36228', title: '浙大软件学院-评奖评优' }], - [6, { id: '36233', title: '浙大软件学院-实习就业' }], - [7, { id: '36235', title: '浙大软件学院-国际实习' }], - [8, { id: '36194', title: '浙大软件学院-国内合作科研' }], - [9, { id: '36246', title: '浙大软件学院-国际合作科研' }], + [0, { id: '', title: '浙大软件学院-全部通知' }], + [1, { id: '32178/list.htm', title: '浙大软件学院-招生信息' }], + [2, { id: '36216/list.htm', title: '浙大软件学院-教务管理' }], + [3, { id: '36217/list.htm', title: '浙大软件学院-论文管理' }], + [4, { id: '36224/list.htm', title: '浙大软件学院-思政工作' }], + [5, { id: '36228/list.htm', title: '浙大软件学院-评奖评优' }], + [6, { id: '36233/list.htm', title: '浙大软件学院-实习就业' }], + [7, { id: '36235/list.htm', title: '浙大软件学院-国际实习' }], + [8, { id: '36194/list.htm', title: '浙大软件学院-国内合作科研' }], + [9, { id: '36246/list.htm', title: '浙大软件学院-国际合作科研' }], ]); async function getPage(id) { const res = await got({ method: 'get', - url: host + `${id}` + '/list.htm', + url: host + id, }); const $ = cheerio.load(res.data); @@ -32,7 +33,7 @@ async function getPage(id) { return { title: item.find('a').text(), pubDate: new Date(item.find('.fr').text()).toUTCString(), - link: `http://www.cst.zju.edu.cn/${item.find('a').attr('href')}`, + link: item.find('a').attr('href'), }; }) .get() @@ -41,27 +42,52 @@ async function getPage(id) { module.exports = async (ctx) => { const type = Number.parseInt(ctx.params.type); + const link = host + map.get(type).id; + let items = []; if (type === 0) { const tasks = []; for (const value of map.values()) { tasks.push(getPage(value.id)); } const results = await Promise.all(tasks); - let items = []; results.forEach((result) => { items = items.concat(result); }); - ctx.state.data = { - title: '浙大软件学院-全部通知', - link: host, - item: items, - }; } else { - const id = map.get(type).id; - ctx.state.data = { - title: map.get(type).title, - link: host + `${id}` + '/list.htm', - item: await getPage(id), - }; + items = await getPage(map.get(type).id); } + + const out = await Promise.all( + items.map(async (item) => { + const itemUrl = host + item.link; + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await got({ + method: 'get', + url: itemUrl, + headers: { + Referer: link, + }, + }); + const $ = cheerio.load(response.data); + const description = $('.vid_wz').html(); + const single = { + title: item.title, + link: itemUrl, + description: description, + pubDate: item.pubDate, + }; + ctx.cache.set(itemUrl, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: map.get(type).title, + link: link, + item: out, + }; };