From 67f41eabb375d19f28b9d9949a1af812887a7e0a Mon Sep 17 00:00:00 2001 From: junfengP <840282629@qq.com> Date: Mon, 4 Mar 2019 19:20:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=97=E4=BA=AC=E8=88=AA?= =?UTF-8?q?=E7=A9=BA=E8=88=AA=E5=A4=A9=E5=A4=A7=E5=AD=A6=20=E7=A0=94?= =?UTF-8?q?=E7=A9=B6=E7=94=9F=E9=99=A2=20RSS=20=E8=AE=A2=E9=98=85=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=20(#1646)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次写JS,有很多不懂的地方,代码是仿照南航教务处RSS的改写,请大佬多多包涵! --- docs/README.md | 6 +++ lib/router.js | 1 + lib/routes/universities/nuaa/yjsy/yjsy.js | 62 +++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 lib/routes/universities/nuaa/yjsy/yjsy.js diff --git a/docs/README.md b/docs/README.md index ca33b6dfee..7e81cfeefb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1474,6 +1474,12 @@ GitHub 官方也提供了一些 RSS: | -------- | -------- | -------- | -------- | -------- | -------- | -------- | | tzgg | xwdt | kydt | jxdt | xsgz | zsxx | jyxx | + + +| 最近动态 | 研院新闻 | 上级文件 | 管理文件 | 信息服务 | +| -------- | -------- | -------- | -------- | -------- | +| latest | yyxw | sjwj | glwj | xxfw | + ### 哈尔滨工业大学 diff --git a/lib/router.js b/lib/router.js index a74ea8bb32..a34a7abe19 100644 --- a/lib/router.js +++ b/lib/router.js @@ -570,6 +570,7 @@ router.get('/seu/cse/:type?', require('./routes/universities/seu/cse')); // 南京航空航天大学 router.get('/nuaa/jwc/:type?', require('./routes/universities/nuaa/jwc/jwc')); router.get('/nuaa/cs/:type?', require('./routes/universities/nuaa/cs/index')); +router.get('/nuaa/yjsy/:type?', require('./routes/universities/nuaa/yjsy/yjsy')); // 哈尔滨工业大学 router.get('/hit/jwc', require('./routes/universities/hit/jwc')); diff --git a/lib/routes/universities/nuaa/yjsy/yjsy.js b/lib/routes/universities/nuaa/yjsy/yjsy.js new file mode 100644 index 0000000000..5c0907cd1e --- /dev/null +++ b/lib/routes/universities/nuaa/yjsy/yjsy.js @@ -0,0 +1,62 @@ +const axios = require('../../../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); +const host = 'http://www.graduate.nuaa.edu.cn/'; + +const map = { + latest: '2146/list.htm', + yyxw: 'yyxw/list.htm', + sjwj: 'sjwj/list.htm', + glwj: '2017/list.htm', + xxfw: '2147/list.htm', +}; + +async function load(link, ctx) { + const cache = await ctx.cache.get(link); + if (cache) { + return cache; + } + const response = await axios.get(host + link); + const $ = cheerio.load(response.data); + const images = $('img'); + for (let k = 0; k < images.length; k++) { + $(images[k]).replaceWith(``); + } + const description = $('.wp_articlecontent').html(); + await ctx.cache.set(link, description); + return { description }; +} + +module.exports = async (ctx) => { + const type = ctx.params.type || 'latest'; + const response = await axios({ + method: 'get', + url: host + map[type], + }); + const $ = cheerio.load(response.data); + const list = $('#news_list > tbody > tr > td > table > tbody > tr:nth-child(1)') + .slice(0, 10) + .get(); + const process = await Promise.all( + list.map(async (item) => { + const a = $(item).find('td:nth-child(2) > a:nth-child(2)'); + const t = $(item).find('td:nth-child(3)'); + const itemUrl = a.attr('href'); + const single = { + title: a.text(), + link: url.resolve(host, itemUrl), + guid: url.resolve(host, itemUrl), + pubDate: t.text().slice(0, 10), + }; + const other = await load(itemUrl, ctx); + return Promise.resolve(Object.assign({}, single, other)); + }) + ); + + ctx.state.data = { + title: '南航研究生院', + link: host, + description: '南航研究生院RSS', + item: process, + }; +};