fix(route): fix shiep/jsjxy shiep/jwc (#14196)
This commit is contained in:
@@ -26,9 +26,9 @@ const config = {
|
||||
jjc: { title: '基建处', id: '327' },
|
||||
jjxy: { title: '继续教育学院(国际教育学院)', id: '2582' },
|
||||
jsjxfzzx: { title: '教师教学发展中心', id: '3909' },
|
||||
jsjxy: { title: '计算机科学与技术学院', id: 'xygg', listSelector: 'div.post-entry-2', pubDateSelector: 'span:nth-child(2)' },
|
||||
jsjxy: { title: '计算机科学与技术学院', id: 'xygg', listSelector: 'div.xylist', pubDateSelector: 'span:nth-child(2)' },
|
||||
jszyzx: { title: '技术转移中心', id: '4247' },
|
||||
jwc: { title: '教务处', id: '227' },
|
||||
jwc: { title: '教务处', id: '227', listSelector: 'div.text-list li', pubDateSelector: 'span.time' },
|
||||
jxfz: { title: '电力装备设计与制造虚拟仿真中心', id: '3330' },
|
||||
kczx: { title: '能源电力科创中心', id: '3946' },
|
||||
kyc: { title: '科研处/融合办', id: '834' },
|
||||
|
||||
+16
-13
@@ -10,7 +10,13 @@ module.exports = async (ctx) => {
|
||||
const type = ctx.params.type;
|
||||
|
||||
if (!Object.keys(config).includes(type)) {
|
||||
throw Error('Invalid type');
|
||||
throw Error(`Invalid type: ${type}`);
|
||||
}
|
||||
|
||||
const { listSelector = '.list_item', pubDateSelector = '.Article_PublishDate', descriptionSelector = '.wp_articlecontent', title } = config[type];
|
||||
|
||||
if (!title) {
|
||||
throw Error(`Invalid type: ${type}`);
|
||||
}
|
||||
|
||||
const host = `https://${type}.shiep.edu.cn`;
|
||||
@@ -20,23 +26,21 @@ module.exports = async (ctx) => {
|
||||
const response = await got(link);
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
const listSelector = config[type].listSelector || '.list_item';
|
||||
const pubDateSelector = config[type].pubDateSelector || '.Article_PublishDate';
|
||||
const descriptionSelector = config[type].descriptionSelector || '.wp_articlecontent';
|
||||
|
||||
const list = $(listSelector)
|
||||
.toArray()
|
||||
.filter((item) => {
|
||||
const date = dayjs($(item).find(pubDateSelector).text().trim());
|
||||
return date.isValid();
|
||||
})
|
||||
.map((item) => {
|
||||
item = $(item);
|
||||
const pubDateText = item.find(pubDateSelector).text().trim();
|
||||
const match = pubDateText.match(/\b(\d{4}-\d{2}-\d{2})\b/);
|
||||
return {
|
||||
title: item.find('a').attr('title') || item.find('a').text(),
|
||||
title: item.find('a').attr('title') || item.find('h3').text() || item.find('a').text(),
|
||||
link: new URL(item.find('a').attr('href'), host).href,
|
||||
pubDate: parseDate(item.find(pubDateSelector).text().trim(), 'YYYY-MM-DD'),
|
||||
pubDate: match ? parseDate(match[0], 'YYYY-MM-DD') : null,
|
||||
};
|
||||
})
|
||||
.filter((item) => {
|
||||
const date = dayjs(item.pubDate);
|
||||
return date.isValid();
|
||||
});
|
||||
|
||||
const items = await Promise.all(
|
||||
@@ -62,9 +66,8 @@ module.exports = async (ctx) => {
|
||||
);
|
||||
|
||||
ctx.state.data = {
|
||||
title: '上海电力大学-' + config[type].title,
|
||||
title: `上海电力大学-${title}`,
|
||||
link,
|
||||
description: '上海电力大学-' + config[type].title,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user