From 1c9016d12ee352f3e66f1a13f80a7a8e2ce78b9e Mon Sep 17 00:00:00 2001 From: SUEPbot <114787369+SUEPbot@users.noreply.github.com> Date: Thu, 19 Jan 2023 15:41:22 +0800 Subject: [PATCH] feat(route): get full text for shiep (#11645) * feat(route): get full text for shiep * refactor(route): use WHATWG URL API instead of deprecated url.resolve in shiep * refactor(route): cache the whole return result * feat(route): leave a notice if item is redirected to the intranet Co-authored-by: SUEPbot --- lib/v2/shiep/index.js | 72 ++++++++++++++++++-------- lib/v2/shiep/templates/description.art | 1 + 2 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 lib/v2/shiep/templates/description.art diff --git a/lib/v2/shiep/index.js b/lib/v2/shiep/index.js index 0d3d28971b..815a588be8 100644 --- a/lib/v2/shiep/index.js +++ b/lib/v2/shiep/index.js @@ -1,6 +1,9 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const dayjs = require('dayjs'); +const path = require('path'); const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); module.exports = async (ctx) => { const type = ctx.params.type; @@ -34,32 +37,55 @@ module.exports = async (ctx) => { throw Error('Invalid type'); } - const id = ctx.params.id ?? config[type].id; - - const response = await got({ - method: 'get', - url: `https://${type}.shiep.edu.cn/${id}/list.htm`, - }); - const data = response.data; - const $ = cheerio.load(data); - const class_name = 'list_item'; + const host = `https://${type}.shiep.edu.cn`; + const id = ctx.params.id || config[type].id; const pubDateClass = type === 'dxxy' ? 'div[class="article-publishdate"]' : 'span[class="Article_PublishDate"]'; // 适配电子与信息工程学院网站改版 - const list = $(`.${class_name}`); + + const response = await got(`${host}/${id}/list.htm`); + + const $ = cheerio.load(response.data); + + const items = $('.list_item') + .toArray() + .filter((item) => { + const date = dayjs($(item).find(pubDateClass).text().trim()); + return date.isValid(); + }) + .map((item) => { + item = $(item); + return { + title: item.find('a').attr('title') || item.find('a').text(), + link: new URL(item.find('a').attr('href'), host).href, + pubDate: parseDate(item.find(pubDateClass).text().trim(), 'YYYY-MM-DD'), + }; + }); + + await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + try { + const response = await got(item.link); + const $ = cheerio.load(response.data); + + if ($('.wp_articlecontent').length > 0) { + item.description = art(path.resolve(__dirname, 'templates/description.art'), { + description: $('.wp_articlecontent').html(), + }); + } else { + item.description = '请进行统一身份认证后查看内容'; + } + } catch (e) { + item.description = '请在校内或通过校园VPN查看内容'; + } + return item; + }) + ) + ); ctx.state.data = { title: '上海电力大学-' + config[type].title, - link: `https://${type}.shiep.edu.cn/${id}/list.htm`, - item: - list && - list - .map((index, item) => { - item = $(item); - return { - title: item.find('a').text(), - link: item.find('a').attr('href'), - pubDate: parseDate(item.find(pubDateClass).text().trim(), 'YYYY-MM-DD'), - }; - }) - .get(), + link: `${host}/${id}/list.htm`, + description: '上海电力大学-' + config[type].title, + item: items, }; }; diff --git a/lib/v2/shiep/templates/description.art b/lib/v2/shiep/templates/description.art new file mode 100644 index 0000000000..d1254e8e79 --- /dev/null +++ b/lib/v2/shiep/templates/description.art @@ -0,0 +1 @@ +{{@ description }}