From 86dfd066d4123827711f680724e9650d44bcb6f9 Mon Sep 17 00:00:00 2001 From: SUEPbot <114787369+SUEPbot@users.noreply.github.com> Date: Mon, 8 Jan 2024 12:06:09 +0800 Subject: [PATCH] fix(route): fix shiep/jsjxy shiep/jwc (#14196) --- lib/v2/shiep/config.js | 4 ++-- lib/v2/shiep/index.js | 29 ++++++++++++++++------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/v2/shiep/config.js b/lib/v2/shiep/config.js index 8694d39a4..7051f3bd1 100644 --- a/lib/v2/shiep/config.js +++ b/lib/v2/shiep/config.js @@ -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' }, diff --git a/lib/v2/shiep/index.js b/lib/v2/shiep/index.js index f189ddc90..044c4c41c 100644 --- a/lib/v2/shiep/index.js +++ b/lib/v2/shiep/index.js @@ -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, }; };