feat(route): add Nanjing Normal University(RSS) (#7932)

* Update router.js

* update

* try

* Update utils.js

* Update utils.js

* Update utils.js

* update

* update

* update

* refactor: migrate to v2

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
Shujakuin kuraudo
2022-02-24 02:24:13 +08:00
committed by GitHub
co-authored by TonyRL
parent 8537de593a
commit 751d89e8e4
8 changed files with 252 additions and 0 deletions
+22
View File
@@ -1537,6 +1537,28 @@ type 列表:
</Route>
## 南京师范大学
### 教务通知
<Route author="Shujakuinkuraudo" example="/njnu/jwc/xstz" path="/njnu/jwc/:type" :paramsDesc="['分类名']">
| 教师通知 | 新闻动态 | 学生通知 |
| ---- | ---- | ---- |
| jstz | xwdt | xstz |
</Route>
### 计算机与电子信息学院 - 人工智能学院
<Route author="Shujakuinkuraudo" example="/njnu/ceai/xszx" path="/njnu/ceai/:type" :paramsDesc="['分类名']">
| 学院公告 | 学院新闻 | 学生资讯 |
| ---- | ---- | ---- |
| xygg | xyxw | xszx |
</Route>
## 南京信息工程大学
::: tip 提示
+44
View File
@@ -0,0 +1,44 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const util = require('./utils');
module.exports = async (ctx) => {
const type = ctx.params.type;
let title, path;
switch (type) {
case 'xygg':
title = '学院公告';
path = '1651';
break;
case 'xyxw':
title = '学院新闻';
path = '1652';
break;
case 'xszx':
title = '学生资讯';
path = '1659';
break;
}
const base = 'http://ceai.njnu.edu.cn/Item/List.asp?ID=' + path;
const response = await got({
method: 'get',
url: base,
https: {
rejectUnauthorized: false,
},
});
const $ = cheerio.load(response.data);
const list = $('span a').get();
const result = await util.ProcessFeed(list, ctx.cache);
ctx.state.data = {
title: '南京师范大学计电人院 - ' + title,
link: 'http://ceai.njnu.edu.cn/',
description: '南京师范大学计电人院',
item: result,
};
};
+55
View File
@@ -0,0 +1,55 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
// 专门定义一个function用于加载文章内容
async function load(link) {
// 异步请求文章
const response = await got.get(link, {
https: {
rejectUnauthorized: false,
},
});
// 加载文章内容
const $ = cheerio.load(response.data);
const dateStr = $('#xw_xinxi span:nth-child(1)').text();
// 解析日期
const pubDate = timezone(parseDate(dateStr, 'YYYY-MM-DD', 'zh-cn'), +8);
// 提取内容
const description = $('#xw_content').html();
// 返回解析的结果
return { description, pubDate };
}
const ProcessFeed = async (list, caches) =>
await Promise.all(
// 遍历每一篇文章
list.map(async (item) => {
const $ = cheerio.load(item);
const $title = $('a');
// 还原相对链接为绝对链接
const itemUrl = $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 { ...single, ...other };
})
);
module.exports = {
ProcessFeed,
};
+43
View File
@@ -0,0 +1,43 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const util = require('./utils');
module.exports = async (ctx) => {
const type = ctx.params.type;
let title, path;
switch (type) {
case 'jstz':
title = '教师通知';
path = 'jstz.htm';
break;
case 'xwdt':
title = '新闻动态';
path = 'xwdt.htm';
break;
case 'xstz':
title = '学生通知';
path = 'xstz.htm';
}
const base = 'http://jwc.njnu.edu.cn/index/' + path;
const response = await got({
method: 'get',
url: base,
https: {
rejectUnauthorized: false,
},
});
const $ = cheerio.load(response.data);
const list = $('.list_txt a').get();
const result = await util.ProcessFeed(list, ctx.cache);
ctx.state.data = {
title: '南京师范大学教务处 - ' + title,
link: 'http://jwc.njnu.edu.cn/',
description: '南京师范大学教务处',
item: result,
};
};
+59
View File
@@ -0,0 +1,59 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
// 专门定义一个function用于加载文章内容
async function load(link) {
// 异步请求文章
const response = await got.get(link, {
https: {
rejectUnauthorized: false,
},
});
// 加载文章内容
const $ = cheerio.load(response.data);
const dateStr = $('span+ span').text();
// 解析日期
const pubDate = timezone(parseDate(dateStr, 'YYYY-MM-DD', 'zh-cn'), +8);
// 提取内容
const description = $('form > div').html();
// 返回解析的结果
return { description, pubDate };
}
const ProcessFeed = async (list, caches) => {
const host = 'http://jwc.njnu.edu.cn/';
// 使用 Promise.all() 进行 async 并发
return await Promise.all(
// 遍历每一篇文章
list.map(async (item) => {
const $ = cheerio.load(item);
const $title = $('a');
// 还原相对链接为绝对链接
const itemUrl = new URL($title.attr('href'), host);
// 列表上提取到的信息
const single = {
title: $title.text(),
link: itemUrl,
author: '教务处',
guid: itemUrl,
};
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。
const other = await caches.tryGet(itemUrl, async () => await load(itemUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return { ...single, ...other };
})
);
};
module.exports = {
ProcessFeed,
};
+4
View File
@@ -0,0 +1,4 @@
module.exports = {
'/jwc/:type': ['Shujakuinkuraudo'],
'/ceai/:type': ['Shujakuinkuraudo'],
};
+21
View File
@@ -0,0 +1,21 @@
module.exports = {
'njnu.edu.cn': {
_name: '南京师范大学',
ceai: [
{
title: '计算机与电子信息学院-人工智能学院',
docs: 'https://docs.rsshub.app/university.html#nan-jing-shi-fan-da-xue',
source: '/',
target: '/njnu/ceai/xygg',
},
],
jwc: [
{
title: '教务通知',
docs: 'https://docs.rsshub.app/university.html#nan-jing-shi-fan-da-xue',
source: '/',
target: '/njnu/jwc/xwdt',
},
],
},
};
+4
View File
@@ -0,0 +1,4 @@
module.exports = (router) => {
router.get('/jwc/:type', require('./jwc/jwc'));
router.get('/ceai/:type', require('./ceai/ceai'));
};