增加南京航空航天大学 研究生院 RSS 订阅方式 (#1646)

第一次写JS,有很多不懂的地方,代码是仿照南航教务处RSS的改写,请大佬多多包涵!
This commit is contained in:
junfengP
2019-03-04 19:20:21 +08:00
committed by DIYgod
parent e938ff9d71
commit 67f41eabb3
3 changed files with 69 additions and 0 deletions
+6
View File
@@ -1474,6 +1474,12 @@ GitHub 官方也提供了一些 RSS:
| -------- | -------- | -------- | -------- | -------- | -------- | -------- |
| tzgg | xwdt | kydt | jxdt | xsgz | zsxx | jyxx |
<route name="研究生院" author="junfengP" example="/nuaa/yjsy/latest" path="/universities/nuaa/yjsy/:type?" :paramsDesc="['分类名']"/>
| 最近动态 | 研院新闻 | 上级文件 | 管理文件 | 信息服务 |
| -------- | -------- | -------- | -------- | -------- |
| latest | yyxw | sjwj | glwj | xxfw |
### 哈尔滨工业大学
<route name="哈尔滨工业大学教务处通知公告" author="lty96117" example="/hit/jwc" path="/universities/hit/jwc"/>
+1
View File
@@ -570,6 +570,7 @@ router.get('/seu/cse/:type?', require('./routes/universities/seu/cse'));
// 南京航空航天大学
router.get('/nuaa/jwc/:type?', require('./routes/universities/nuaa/jwc/jwc'));
router.get('/nuaa/cs/:type?', require('./routes/universities/nuaa/cs/index'));
router.get('/nuaa/yjsy/:type?', require('./routes/universities/nuaa/yjsy/yjsy'));
// 哈尔滨工业大学
router.get('/hit/jwc', require('./routes/universities/hit/jwc'));
+62
View File
@@ -0,0 +1,62 @@
const axios = require('../../../../utils/axios');
const cheerio = require('cheerio');
const url = require('url');
const host = 'http://www.graduate.nuaa.edu.cn/';
const map = {
latest: '2146/list.htm',
yyxw: 'yyxw/list.htm',
sjwj: 'sjwj/list.htm',
glwj: '2017/list.htm',
xxfw: '2147/list.htm',
};
async function load(link, ctx) {
const cache = await ctx.cache.get(link);
if (cache) {
return cache;
}
const response = await axios.get(host + link);
const $ = cheerio.load(response.data);
const images = $('img');
for (let k = 0; k < images.length; k++) {
$(images[k]).replaceWith(`<img src="${url.resolve(host, $(images[k]).attr('src'))}" referrerpolicy="no-referrer" />`);
}
const description = $('.wp_articlecontent').html();
await ctx.cache.set(link, description);
return { description };
}
module.exports = async (ctx) => {
const type = ctx.params.type || 'latest';
const response = await axios({
method: 'get',
url: host + map[type],
});
const $ = cheerio.load(response.data);
const list = $('#news_list > tbody > tr > td > table > tbody > tr:nth-child(1)')
.slice(0, 10)
.get();
const process = await Promise.all(
list.map(async (item) => {
const a = $(item).find('td:nth-child(2) > a:nth-child(2)');
const t = $(item).find('td:nth-child(3)');
const itemUrl = a.attr('href');
const single = {
title: a.text(),
link: url.resolve(host, itemUrl),
guid: url.resolve(host, itemUrl),
pubDate: t.text().slice(0, 10),
};
const other = await load(itemUrl, ctx);
return Promise.resolve(Object.assign({}, single, other));
})
);
ctx.state.data = {
title: '南航研究生院',
link: host,
description: '南航研究生院RSS',
item: process,
};
};