swust: 修复教务处 (#882)

This commit is contained in:
Artin
2018-10-14 16:36:26 +08:00
committed by DIYgod
parent 3707145428
commit bcabc04156
6 changed files with 123 additions and 71 deletions
+7 -5
View File
@@ -950,20 +950,22 @@ GitHub 官方也提供了一些 RSS:
### 西南科技大学
<route name="教务处通知" author="lengthmin" example="/swust/jwc/1" path="/universities/swust/jwc/:type" :paramsDesc="['分区 type, 详见下方表格']">
<route name="教务处新闻" author="lengthmin" example="/swust/jwc/news" path="/universities/swust/jwc/news"/>
<route name="教务处通知" author="lengthmin" example="/swust/jwc/notice/1" path="/universities/swust/jwc/notice/:type?" :paramsDesc="['分区 type,缺省为 1, 详见下方表格']">
| 通知公告 | 学校新闻 |
| -------- | -------- |
| 1 | 2 |
| 创新创业教育 | 学生学业 | 建设与改革 | 教学质量保障 | 教学运行 | 教师教学 |
| ------------ | -------- | ---------- | ------------ | -------- | -------- |
| 1 | 2 | 3 | 4 | 5 | 6 |
</route>
<route name="计科学院通知" author="lengthmin" example="/swust/cs/1" path="/universities/swust/cs/:type" :paramsDesc="['分区 type, 详见下方表格']">
<route name="计科学院通知" author="lengthmin" example="/swust/cs/1" path="/universities/swust/cs/:type?" :paramsDesc="['分区 type, 缺省为 1, 详见下方表格']">
| 新闻动态 | 学术动态 | 通知公告 | 教研动态 |
| -------- | -------- | -------- | -------- |
| 1 | 2 | 3 | 4 |
</route>
### 北京大学
<route name="信科公告通知" author="Ir1d" example="/pku/eecs/0" path="/universities/pku/eecs/:type" :paramsDesc="['分区 type, 可在网页 URL 中找到']">
+3 -2
View File
@@ -505,8 +505,9 @@ router.get('/shmtu/notes', require('./routes/universities/shmtu/notes'));
router.get('/shmtu/jwc/:type', require('./routes/universities/shmtu/jwc'));
// 西南科技大学
router.get('/swust/jwc/:type', require('./routes/universities/swust/jwc'));
router.get('/swust/cs/:type', require('./routes/universities/swust/cs'));
router.get('/swust/jwc/news', require('./routes/universities/swust/jwc_news'));
router.get('/swust/jwc/notice/:type?', require('./routes/universities/swust/jwc_notice'));
router.get('/swust/cs/:type?', require('./routes/universities/swust/cs'));
// 华南师范大学
router.get('/scnu/jw', require('./routes/universities/scnu/jw'));
+1 -1
View File
@@ -10,7 +10,7 @@ module.exports = async (ctx) => {
responseType: 'arraybuffer',
});
let type = ctx.params.type;
let type = ctx.params.type || 1;
let info = '新闻动态';
let word = 'news';
if (type === '2') {
-63
View File
@@ -1,63 +0,0 @@
const axios = require('../../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const host = 'http://www.dean.swust.edu.cn';
let type = ctx.params.type;
let info = '通知公告';
let word = 'notices/';
if (type === '2') {
info = '站点新闻';
word = 'news/';
} else {
type = '1';
}
const web = 'http://www.dean.swust.edu.cn/xml/' + word + 'index.xml';
const response = await axios({
method: 'get',
url: web,
headers: {
Referer: host,
},
});
const data = response.data;
const $ = cheerio.load(data, {
xmlMode: true,
});
const list = $('entrity');
ctx.state.data = {
title: '西南科技大学 教务处 ' + info,
link: web,
description: $('title')
.first()
.text(),
item:
list &&
list
.map((index, item) => {
item = $(item);
return {
title: item.find('title').text(),
description: item.find('summary').text(),
pubDate: new Date(
Date.parse(
item
.find('date')
.text()
.replace('年', '-')
.replace('月', '-')
.replace('日', '')
)
).toUTCString(),
link: host + '/xml/' + word + item.attr('id') + '.xml',
};
})
.get(),
};
};
+53
View File
@@ -0,0 +1,53 @@
const axios = require('../../../utils/axios');
// 转码需要设定返回数据的格式,其可选项是arraybuffer,blob,document,json,text,stream
// 默认为json
const axios_ins = axios.create({ responseType: 'blob' });
const api = 'http://www.dean.swust.edu.cn/cms/portal/news.json';
const host = 'http://www.dean.swust.edu.cn/';
const page = 'http://www.dean.swust.edu.cn/news/';
function evil(fn) {
const Fn = Function; // 一个变量指向Function,防止有些前端编译工具报错
return new Fn('return ' + fn)();
}
module.exports = async (ctx) => {
const response = await axios_ins.get(api);
const data = evil(response.data);
const resultItems = await Promise.all(
data.map(async (item) => {
const title = item.title;
const date = item.outdate;
const link = page + item.id;
const author = item.publisher;
let resultItem = {};
const value = await ctx.cache.get(link);
if (value) {
resultItem = JSON.parse(value);
} else {
resultItem = {
title: title,
link: link,
pubDate: new Date(date),
author: author,
};
ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60);
}
return Promise.resolve(resultItem);
})
);
ctx.state.data = {
title: '西南科技大学教务处 新闻',
link: host,
description: '西南科技大学教务处 新闻',
item: resultItems,
};
};
+59
View File
@@ -0,0 +1,59 @@
const axios = require('../../../utils/axios');
// 转码需要设定返回数据的格式,其可选项是arraybuffer,blob,document,json,text,stream
// 默认为json
const axios_ins = axios.create({ responseType: 'blob' });
const api = 'http://www.dean.swust.edu.cn/cms/portal/notice.json';
const page = 'http://www.dean.swust.edu.cn/notice/page/';
const tag = 'http://www.dean.swust.edu.cn/notice/tag/';
function evil(fn) {
const Fn = Function; // 一个变量指向Function,防止有些前端编译工具报错
return new Fn('return ' + fn)();
}
module.exports = async (ctx) => {
const type = ctx.params.type || 1;
const response = await axios_ins.get(api);
const responsedata = evil(response.data);
const data = responsedata[type - 1];
const info = data.name || '';
const tag_url = data.id || '';
const resultItems = await Promise.all(
data.items.map(async (item) => {
const link = page + item.id;
const author = item.publisher;
const date = item.outdate;
const title = item.title;
let resultItem = {};
const value = await ctx.cache.get(link);
if (value) {
resultItem = JSON.parse(value);
} else {
resultItem = {
title: title,
link: link,
pubDate: new Date(date),
author: author,
};
ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60);
}
return Promise.resolve(resultItem);
})
);
ctx.state.data = {
title: '西南科技大学教务处 ' + info,
link: tag + tag_url,
description: `西南科技大学教务处 ${info}`,
item: resultItems,
};
};