From 1959a83b230fb9438fd699b7b4bc1f70e985bbc6 Mon Sep 17 00:00:00 2001 From: Xiaotouming <33121254+Xiaotouming@users.noreply.github.com> Date: Fri, 19 Apr 2019 00:23:15 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=B9=BF=E4=B8=9C=E6=B5=B7=E6=B4=8B?= =?UTF-8?q?=E5=A4=A7=E5=AD=A6=E6=95=99=E5=8A=A1=E9=80=9A=E7=9F=A5=20(#1956?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改:代码格式标准化 --- docs/university.md | 12 ++-- lib/router.js | 3 + lib/routes/universities/gdou/jwc/jwtz.js | 29 +++++++++ lib/routes/universities/gdou/jwc/utils.js | 73 +++++++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 lib/routes/universities/gdou/jwc/jwtz.js create mode 100644 lib/routes/universities/gdou/jwc/utils.js diff --git a/docs/university.md b/docs/university.md index 3582db112..48ed74d38 100644 --- a/docs/university.md +++ b/docs/university.md @@ -607,10 +607,6 @@ https://rsshub.app/**nuist**/`bulletin` 或 https://rsshub.app/**nuist**/`bullet -## 中国传媒大学 - - - ## 中国药科大学 @@ -697,6 +693,10 @@ https://rsshub.app/**nuist**/`bulletin` 或 https://rsshub.app/**nuist**/`bullet +## 中国传媒大学 + + + ## 北京科技大学天津学院 @@ -706,3 +706,7 @@ https://rsshub.app/**nuist**/`bulletin` 或 https://rsshub.app/**nuist**/`bullet | all | xyxw | xshhd | csjsxy | xxgcxy | jjx | glxy | clx | jxgcx | hlx | flx | wyx | ysx | + +## 广东海洋大学 + + diff --git a/lib/router.js b/lib/router.js index f759798e8..97a1643e3 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1267,4 +1267,7 @@ router.get('/vocus/publication/:id', require('./routes/vocus/publication')); router.get('/1point3acres/user/:id/threads', require('./routes/1point3acres/threads')); router.get('/1point3acres/user/:id/posts', require('./routes/1point3acres/posts')); +// 广东海洋大学 +router.get('/gdoujwc', require('./routes/universities/gdou/jwc/jwtz')); + module.exports = router; diff --git a/lib/routes/universities/gdou/jwc/jwtz.js b/lib/routes/universities/gdou/jwc/jwtz.js new file mode 100644 index 000000000..9988b10c6 --- /dev/null +++ b/lib/routes/universities/gdou/jwc/jwtz.js @@ -0,0 +1,29 @@ +const axios = require('../../../../utils/axios'); +const cheerio = require('cheerio'); +const util = require('./utils'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const response = await axios({ + method: 'get', + url: 'http://www3.gdou.edu.cn/jwc/', + responseType: 'arraybuffer', + }); + + // HTML-buffer转为gb2312 + const data = iconv.decode(response.data, 'gb2312'); + const $ = cheerio.load(data); + + const list = $('.bor03.cont li') + .slice(0, 10) + .get(); + + const result = await util.ProcessFeed(list, ctx.cache); + + ctx.state.data = { + title: $('title').text(), + link: 'http://www3.gdou.edu.cn/jwc/Item/list.asp?id=1224', + description: '教务通知', + item: result, + }; +}; diff --git a/lib/routes/universities/gdou/jwc/utils.js b/lib/routes/universities/gdou/jwc/utils.js new file mode 100644 index 000000000..d7acd9410 --- /dev/null +++ b/lib/routes/universities/gdou/jwc/utils.js @@ -0,0 +1,73 @@ +const axios = require('../../../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); +const iconv = require('iconv-lite'); + +// 专门定义一个function用于加载文章内容 +async function load(link) { + // 异步请求文章 + const response = await axios({ + method: 'get', + url: link, + responseType: 'arraybuffer', + headers: { + Referer: 'http://www3.gdou.edu.cn/jwc/', + }, + }); + + // 加载文章内容 + // 先将HTML-buffer转为gb2312 + const data = iconv.decode(response.data, 'gb2312'); + const $ = cheerio.load(data); + + // 解析日期 + const date = new Date( + $('.info') + .text() + .match(/\d{4}\/\d{1,2}\/\d{1,2}/) + ); + const timeZone = 8; + const serverOffset = date.getTimezoneOffset() / 60; + const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString(); + + // 提取内容 + const description = $('.left01').html(); + + // 返回解析的结果 + return { description, pubDate }; +} + +const ProcessFeed = async (list, caches) => { + const host = 'http://www3.gdou.edu.cn/'; + + // 使用 Promise.all() 进行 async 并发 + return await Promise.all( + // 遍历每一篇文章 + list.map(async (item) => { + const $ = cheerio.load(item); + + const $title = $('a'); + // 还原相对链接为绝对链接 + const itemUrl = url.resolve(host, $title.attr('href')); + + // 列表上提取到的信息 + const single = { + title: $title.text(), + link: itemUrl, + author: '教务部', + guid: itemUrl, + }; + + // 使用tryGet方法从缓存获取内容。 + // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。 + const other = await caches.tryGet(itemUrl, async () => await load(itemUrl)); + + // 合并解析后的结果集作为该篇文章最终的输出结果 + return Promise.resolve(Object.assign({}, single, other)); + }) + ); +}; + +module.exports = { + ProcessFeed, +};