fix(route): 修正南京大学本科生院教学信息网信息获取 (#10749)

将 HTML 解释改为利用 WebPlus 的 REST 接口。通过自定义 `returnInfos` 可以实现
返回格式的自定义,特别是可以获取时间信息。增大每次返回条目数。
This commit is contained in:
Charlie Jiang 2022-09-09 22:26:55 +08:00 committed by GitHub
parent d246d53a14
commit 54296e19de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 30 deletions

View File

@ -1,45 +1,59 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const type = ctx.params.type;
const type_dict = {
ggtz: ['https://jw.nju.edu.cn/ggtz/list.htm', '公告通知'],
jxdt: ['https://jw.nju.edu.cn/_t1143/24774/list.psp', '教学动态'],
ggtz: [
26263, // columnID
'公告通知', // type name
'[{"field":"top","type":"desc"},{"field":"new","type":"desc"},{"field":"publishTime","type":"desc"}]', // order
'[{"field":"title","name":"title"},{"field":"f1","name":"f1"},{"field":"publishTime","pattern":[{"name":"d","value":"yyyy-MM-dd HH:mm:ss"}],"name":"publishTime"},{"field":"link","name":"link"}]', // returnInfos
'https://jw.nju.edu.cn/ggtz/list.htm',
],
jxdt: [
24774,
'教学动态',
'[{"field":"top","type":"desc"},{"field":"new","type":"desc"},{"field":"publishTime","type":"desc"}]',
'[{"field":"title","name":"title"},{"field":"publishTime","pattern":[{"name":"d","value":"yyyy-MM-dd HH:mm:ss"}],"name":"publishTime"},{"field":"link","name":"link"}]',
'https://jw.nju.edu.cn/_s414/24774/list.psp',
],
};
const response = await got({
method: 'get',
url: type_dict[type][0],
const { data } = await got({
method: 'post',
url: 'https://jw.nju.edu.cn/_wp3services/generalQuery?queryObj=articles',
form: {
siteId: 414,
columnId: type_dict[type][0],
pageIndex: 1,
rows: 24, // 用大一点的值,因为前面太多置顶的
orders: type_dict[type][2],
returnInfos: type_dict[type][3],
},
headers: {
Origin: 'https://jw.nju.edu.cn',
Referer: 'https://jw.nju.edu.cn/main.htm',
},
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('li.news');
ctx.state.data = {
title: `本科生院-${type_dict[type][1]}`,
link: type_dict[type][0],
item: list
.map((index, item) => {
item = $(item);
link: type_dict[type][4],
item:
data &&
data.data &&
data.data.map((item) => {
const ret = {
title: item.title,
author: item.publisher,
pubDate: timezone(parseDate(item.publishTime, 'YYYY-MM-DD HH:mm:ss'), +8),
link: item.url,
};
if (type === 'ggtz') {
return {
title: item.find('a').attr('title'),
category: item.find('div').first().text().split(''),
link: 'https://jw.nju.edu.cn' + item.find('a').attr('href'),
pubDate: timezone(parseDate(item.find('.news_meta').first().text(), 'YYYY-MM-DD'), +8),
};
} else {
return {
title: item.find('a').attr('title'),
link: 'https://jw.nju.edu.cn' + item.find('a').attr('href'),
pubDate: timezone(parseDate(item.find('.news_meta').first().text(), 'YYYY-MM-DD'), +8),
};
ret.category = item.f1;
}
})
.get(),
return ret;
}),
};
};

View File

@ -5,7 +5,7 @@ module.exports = {
'/gra': ['ret-1'],
'/hqjt': ['ret-1'],
'/jjc': ['ret-1'],
'/jw/:type': ['ret-1'],
'/jw/:type': ['cqjjjzr'],
'/rczp/:type': ['ret-1'],
'/scit/:type': ['ret-1'],
'/zbb/:type': ['ret-1'],