feat:add 北京林业大学教务处 (#4746)

This commit is contained in:
markmingjie 2020-05-12 14:24:39 +08:00 committed by GitHub
parent e2b315f767
commit f3acba5f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 0 deletions

View File

@ -116,6 +116,16 @@ pageClass: routes
<Route author="markmingjie" example="/bjfu/grs" path="/bjfu/grs" />
### 教务处通知公告
<Route author="markmingjie" example="/bjfu/jwc/jwkx" path="/bjfu/jwc/:type" :paramsDesc="['通知类别']">
| 教务快讯 | 考试信息 | 课程信息 | 教改动态 | 图片新闻 |
| -------- | -------- | -------- | -------- | -------- |
| jwkx | ksxx | kcxx | jgdt | tpxw |
</Route>
## 北京邮电大学
### 硕士研究生招生通知

View File

@ -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'));

View File

@ -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,
};
};

View File

@ -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,
};