mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
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:
+49
-23
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{{@ description }}
|
||||
Reference in New Issue
Block a user