From 0740a651bc4e719c8d7230738c956a7709da4575 Mon Sep 17 00:00:00 2001 From: ueiu <39592269+ueiu@users.noreply.github.com> Date: Thu, 3 Sep 2020 02:49:41 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0=E4=B8=B4?= =?UTF-8?q?=E6=B2=82=E5=A4=A7=E5=AD=A6=E6=A0=A1=E5=9B=AD=E6=96=B0=E9=97=BB?= =?UTF-8?q?RSS=20(#5054)=20(#5580)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ueiu --- docs/university.md | 11 ++++++ lib/router.js | 3 ++ lib/routes/universities/lyu/news/index.js | 40 +++++++++++++++++++++ lib/routes/universities/lyu/news/utils.js | 43 +++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 lib/routes/universities/lyu/news/index.js create mode 100644 lib/routes/universities/lyu/news/utils.js diff --git a/docs/university.md b/docs/university.md index b448963eee..a2dc2215f1 100644 --- a/docs/university.md +++ b/docs/university.md @@ -843,6 +843,17 @@ jsjxy.hbut.edu.cn 证书链不全,自建 RSSHub 可设置环境变量 NODE_TLS +## 临沂大学 + +### 新闻 + + + +| 临大要闻 | 信息公告 | +| -------- | -------- | +| ldyw | xxgg | + + ## 洛阳理工学院 ### 教务处 diff --git a/lib/router.js b/lib/router.js index 3e42c24a34..abbb8ddef0 100644 --- a/lib/router.js +++ b/lib/router.js @@ -840,6 +840,9 @@ router.get('/ahut/cstzgg', require('./routes/universities/ahut/cstzgg')); // 上海理工大学 router.get('/usst/jwc', require('./routes/universities/usst/jwc')); +// 临沂大学 +router.get('/lyu/news/:type', require('./routes/universities/lyu/news/index')); + // ifanr router.get('/ifanr/:channel?', require('./routes/ifanr/index')); diff --git a/lib/routes/universities/lyu/news/index.js b/lib/routes/universities/lyu/news/index.js new file mode 100644 index 0000000000..9ee4d6ad54 --- /dev/null +++ b/lib/routes/universities/lyu/news/index.js @@ -0,0 +1,40 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const util = require('./utils'); +const baseUrl = 'https://www.lyu.edu.cn/'; + +module.exports = async (ctx) => { + const type = ctx.params.type; + let title, path; + switch (type) { + case 'ldyw': + title = '临大要闻'; + path = '8125'; + break; + case 'xxgg': + title = '信息公告'; + path = '8126'; + break; + } + + // 完整url + const fullUrl = baseUrl + path + '/list.htm'; + + const response = await got({ + method: 'get', + url: fullUrl, + }); + + const data = response.data; + const $ = cheerio.load(data); + const list = $('.column-news-item').get(); + + const result = await util.ProcessFeed(baseUrl, list, ctx.cache); + + ctx.state.data = { + title: '临沂大学 - ' + title, + link: fullUrl, + description: '临沂大学 - ' + title, + item: result, + }; +}; diff --git a/lib/routes/universities/lyu/news/utils.js b/lib/routes/universities/lyu/news/utils.js new file mode 100644 index 0000000000..a19286e32b --- /dev/null +++ b/lib/routes/universities/lyu/news/utils.js @@ -0,0 +1,43 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const url = require('url'); + +// 获取完整文章内容 +async function load(link) { + const response = await got.get(link); + const $ = cheerio.load(response.data); + + // 提取内容 + const description = $('.wp-container').html(); + // 返回结果 + return { description }; +} + +const ProcessFeed = async (baseUrl, list, caches) => + // 使用 Promise.all() 进行 async 并发 + await Promise.all( + // 遍历每一篇文章 + list.map(async (item) => { + const $ = cheerio.load(item); + // 还原相对链接为绝对链接 + const itemUrl = url.resolve(baseUrl, $('a').attr('href')); + + // 提取列表信息 + const single = { + title: $('.column-news-title').text(), + link: itemUrl, + author: '临沂大学', + pubDate: new Date($('.news-date-hide').text()).toUTCString(), + guid: itemUrl, + }; + + // 使用tryGet方法从缓存获取内容。 + // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。 + const other = await caches.tryGet(itemUrl, async () => await load(itemUrl)); + // 合并解析后的结果集作为该篇文章最终的输出结果 + return Promise.resolve(Object.assign({}, single, other)); + }) + ); +module.exports = { + ProcessFeed, +};