From f3acba5f9e27fdfb30fac2c762bc66e9c5dde3de Mon Sep 17 00:00:00 2001
From: markmingjie <34265999+markmingjie@users.noreply.github.com>
Date: Tue, 12 May 2020 14:24:39 +0800
Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Aadd=20=E5=8C=97=E4=BA=AC=E6=9E=97?=
=?UTF-8?q?=E4=B8=9A=E5=A4=A7=E5=AD=A6=E6=95=99=E5=8A=A1=E5=A4=84=20(#4746?=
=?UTF-8?q?)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docs/university.md | 10 ++++
lib/router.js | 1 +
lib/routes/universities/bjfu/jwc/index.js | 55 ++++++++++++++++++++
lib/routes/universities/bjfu/jwc/utils.js | 63 +++++++++++++++++++++++
4 files changed, 129 insertions(+)
create mode 100644 lib/routes/universities/bjfu/jwc/index.js
create mode 100644 lib/routes/universities/bjfu/jwc/utils.js
diff --git a/docs/university.md b/docs/university.md
index 2e1f8ac78..ddcab3f38 100644
--- a/docs/university.md
+++ b/docs/university.md
@@ -116,6 +116,16 @@ pageClass: routes
+### 教务处通知公告
+
+
+
+| 教务快讯 | 考试信息 | 课程信息 | 教改动态 | 图片新闻 |
+| -------- | -------- | -------- | -------- | -------- |
+| jwkx | ksxx | kcxx | jgdt | tpxw |
+
+
+
## 北京邮电大学
### 硕士研究生招生通知
diff --git a/lib/router.js b/lib/router.js
index 2d0f2ca18..6edb1ece1 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -557,6 +557,7 @@ router.get('/zdfx', require('./routes/galgame/zdfx'));
// 北京林业大学
router.get('/bjfu/grs', require('./routes/universities/bjfu/grs'));
+router.get('/bjfu/jwc/:type', require('./routes/universities/bjfu/jwc/index'));
// 北京理工大学
router.get('/bit/jwc', require('./routes/universities/bit/jwc/jwc'));
diff --git a/lib/routes/universities/bjfu/jwc/index.js b/lib/routes/universities/bjfu/jwc/index.js
new file mode 100644
index 000000000..d11b75adb
--- /dev/null
+++ b/lib/routes/universities/bjfu/jwc/index.js
@@ -0,0 +1,55 @@
+const got = require('@/utils/got');
+const cheerio = require('cheerio');
+const util = require('./utils');
+const iconv = require('iconv-lite'); // 转码
+
+module.exports = async (ctx) => {
+ const type = ctx.params.type;
+ let title, path;
+ switch (type) {
+ case 'jwkx':
+ title = '教务快讯';
+ path = 'jwkx/';
+ break;
+ case 'jgdt':
+ title = '教改动态';
+ path = 'jgdt/';
+ break;
+ case 'ksxx':
+ title = '考试信息';
+ path = 'ksxx/';
+ break;
+ case 'kcxx':
+ title = '课程信息';
+ path = 'tkxx/';
+ break;
+ case 'tpxw':
+ title = '图片新闻';
+ path = 'tpxw/';
+ break;
+ default:
+ title = '教务快讯';
+ path = 'jwkx/';
+ }
+ const base = 'http://jwc.bjfu.edu.cn/' + path;
+
+ const response = await got({
+ method: 'get',
+ responseType: 'buffer', // 转码
+ url: base,
+ });
+
+ const data = iconv.decode(response.data, 'gb2312'); // 转码
+ const $ = cheerio.load(data);
+
+ const list = $('.list_c li').slice(0, 10).get();
+
+ const result = await util.ProcessFeed(base, list, ctx.cache); // 感谢@hoilc指导
+
+ ctx.state.data = {
+ title: '北林教务处 - ' + title,
+ link: 'http://jwc.bjfu.edu.cn/' + path,
+ description: '北京林业大学教务处 - ' + title,
+ item: result,
+ };
+};
diff --git a/lib/routes/universities/bjfu/jwc/utils.js b/lib/routes/universities/bjfu/jwc/utils.js
new file mode 100644
index 000000000..1b8dea5c3
--- /dev/null
+++ b/lib/routes/universities/bjfu/jwc/utils.js
@@ -0,0 +1,63 @@
+const got = require('@/utils/got');
+const cheerio = require('cheerio');
+const iconv = require('iconv-lite'); // 转码
+
+// 完整文章页
+async function load(link) {
+ const response = await got.get(link, {
+ responseType: 'buffer',
+ });
+
+ const data = iconv.decode(response.data, 'gb2312'); // 转码
+
+ // 加载文章内容
+ const $ = cheerio.load(data);
+
+ // 解析日期
+ const date = new Date(
+ $('div #con_djl')
+ .text()
+ .match(/\d{4}-\d{2}-\d{2}/)
+ );
+
+ const timeZone = 8;
+ const serverOffset = date.getTimezoneOffset() / 60;
+ const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString();
+
+ // 提取内容
+ const description = $('#con_c').html();
+
+ // 返回解析的结果
+ return { description, pubDate };
+}
+
+const ProcessFeed = async (base, list, caches) =>
+ // 使用 Promise.all() 进行 async 并发
+ await Promise.all(
+ // 遍历每一篇文章
+ list.map(async (item) => {
+ const $ = cheerio.load(item);
+
+ const $title = $('a');
+ // 还原相对链接为绝对链接
+ const itemUrl = new URL($title.attr('href'), base); // 感谢@hoilc指导
+
+ // 列表上提取到的信息
+ 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,
+};