Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add route for university: CNU JWC #17709

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions lib/routes/cnu/jwc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Route } from '@/types';
import { parseDate } from '@/utils/parse-date';
import got from '@/utils/got';
import { load } from 'cheerio';
import cache from '@/utils/cache';

const BASE_URL = 'https://jwc.cnu.edu.cn/tzgg/index.htm';

Expand All @@ -11,13 +12,13 @@ export const route: Route = {
example: '/cnu/jwc',
radar: [
{
source: [BASE_URL],
source: [new URL(BASE_URL).host],
},
],
name: '首都师范大学教务处',
TonyRL marked this conversation as resolved.
Show resolved Hide resolved
TonyRL marked this conversation as resolved.
Show resolved Hide resolved
maintainers: ['Aicnal'],
handler,
url: BASE_URL,
url: new URL(BASE_URL).host + new URL(BASE_URL).pathname, // host + pathname
};

async function handler() {
Expand Down Expand Up @@ -51,33 +52,24 @@ async function handler() {
.filter(Boolean);

const items = await Promise.all(
list.map(async (item) => {
try {
list.map((item) =>
cache.tryGet(item.link, async () => {
// Cache the detail page
const detailResponse = await got({ method: 'get', url: item.link });
const content = load(detailResponse.data);
const paragraphs = content('body p')
.toArray() // Convert to an array first
.filter((el) => {
const text = content(el).text();
return !/分享到:|版权所有|地址:|E-mail:|网站地图|ICP备|京公网安备/.test(text);
})
.map((el) => {
content(el).find('img').remove(); // Remove <img> tags
return content(el).html()?.trim();
})
const paragraphs = content(
'body p:not(:contains("分享到:")):not(:contains("版权所有")):not(:contains("地址:")):not(:contains("E-mail:")):not(:contains("网站地图")):not(:contains("ICP备")):not(:contains("京公网安备"))'
)
.toArray()
.map((el) => content(el).html()?.trim())
.join('<br/><br/>');

return {
...item,
description: paragraphs || '暂无内容',
};
} catch (error) {
return {
...item,
description: `内容获取失败: ${error.message}`,
};
}
})
})
)
);

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/cnu/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '首都师范大学教务处',
Copy link
Author

Choose a reason for hiding this comment

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

I have changed the name of namespace.

name: '首都师范大学',
url: 'jwc.cnu.edu.cn',
lang: 'zh-CN',
};
Loading