Skip to content

Commit

Permalink
Merge pull request #737 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Jan 19, 2023
2 parents 35e03e3 + 1c9016d commit 006ad3d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
72 changes: 49 additions & 23 deletions lib/v2/shiep/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 change: 1 addition & 0 deletions lib/v2/shiep/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{@ description }}

1 comment on commit 006ad3d

@vercel
Copy link

@vercel vercel bot commented on 006ad3d Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.