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 <SUEPbot@users.noreply.github.com>
This commit is contained in:
SUEPbot
2023-01-19 03:41:22 -04:00
committed by GitHub
co-authored by SUEPbot
parent 1f6221d8ec
commit 1c9016d12e
2 changed files with 50 additions and 23 deletions
+49 -23
View File
@@ -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,
};
};
+1
View File
@@ -0,0 +1 @@
{{@ description }}