Skip to content

Commit

Permalink
feat(routes/shu): add routes for SHU's Int'l Dept, Grad School, and C…
Browse files Browse the repository at this point in the history
…ampus Highlights

- Corrected the root URL in `index.ts`.
- Added routes for:
  - SHU's International Department (Int'l Dept).
  - Graduate School (Grad School).
  - Campus Highlights.
- Noted the unavailability of the policy in `jwb.ts` with a comment in `index.ts`.
  • Loading branch information
GhhG123 committed Nov 27, 2024
1 parent fce3dd9 commit 8b48453
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 50 deletions.
91 changes: 91 additions & 0 deletions lib/routes/shu/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio'; // cheerio@1.0.0
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const notice_type = {
tzgg: { title: '上海大学国际部港澳台-通知公告', url: 'https://global.shu.edu.cn/cd/tzgg/3.htm' },
};

export const route: Route = {
path: '/global/:type?',
categories: ['university'],
example: '/shu/global/tzgg',
parameters: { type: '分类,默认为通知公告' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['global.shu.edu.cn/'],
target: '/global',
},
],
name: '国际部港澳台办公室',
maintainers: ['GhhG123'],
handler,
url: 'global.shu.edu.cn/',
description: `| 通知公告 |
| -------- |
| tzgg |`,
};

async function handler(ctx) {
const type = ctx.req.param('type') ?? 'tzgg';
const rootUrl = 'https://global.shu.edu.cn';

// 发起 HTTP GET 请求
const response = await got({
method: 'get',

/* headers: {
'user-agent': UA,
cookie: await getCookie(ctx),
}, */
url: notice_type[type].url,
});

const $ = load(response.data);

const list = $('div.only-list1 ul li') // 定位到HTML结构中的li元素
.map((_, el) => {
const item = $(el); // 使用Cheerio包装每个li元素
const rawLink = item.find('a').attr('href');
const pubDate = item.find('span').text().trim(); // 提取日期

return {
title: item.find('a').text().trim(), // 获取标题
link: rawLink ? new URL(rawLink, rootUrl).href : rootUrl, // 生成完整链接
pubDate: timezone(parseDate(pubDate, 'YYYY年MM月DD日'), +8), // 解析并转换日期
description: '', // 没有提供简要描述,设为空字符串
};
})
.get();

Check warning

Code scanning / ESLint

Disallow specified syntax Warning

Please use .toArray() instead.

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link); // 获取详情页内容
const content = load(detailResponse.data); // 使用cheerio解析内容

item.description = content('div.ej_main').html() || item.description; // 提取内容区详情

return item; // 返回完整的item
})
)
);

return {
title: notice_type[type].title,
description: notice_type[type].title,
link: notice_type[type].url,
item: items,
};
}
93 changes: 93 additions & 0 deletions lib/routes/shu/gs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio'; // cheerio@1.0.0
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const notice_type = {
zhxw: { title: '上海大学研究生院-综合新闻', url: 'https://gs.shu.edu.cn/xwlb/zh.htm' }, // 综合新闻
pygl: { title: '上海大学研究生院-培养管理', url: 'https://gs.shu.edu.cn/xwlb/py.htm' }, // local //BUG error: Request https://gs1.shu.edu.cn:8080/py/KCBInfo.asp fail: TypeError: fetch failed
gjjl: { title: '上海大学研究生院-国际交流', url: 'https://gs.shu.edu.cn/xwlb/gjjl.htm' },
};

export const route: Route = {
path: '/gs/:type?',
categories: ['university'],
example: '/shu/gs/zhxw',
parameters: { type: '分类,默认为学术公告' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['gs.shu.edu.cn/'],
target: '/gs',
},
],
name: '研究生院',
maintainers: ['GhhG123'],
handler,
url: 'gs.shu.edu.cn/',
description: `| 综合新闻 | 培养管理 | 国际交流 |
| -------- | --------- | --------- |
| zhxw | pygl | gjjl |`,
};

async function handler(ctx) {
const type = ctx.req.param('type') ?? 'zhxw';
const rootUrl = 'https://gs.shu.edu.cn';

// 发起 HTTP GET 请求
const response = await got({
method: 'get',

/* headers: {
'user-agent': UA,
cookie: await getCookie(ctx),
}, */
url: notice_type[type].url,
});

const $ = load(response.data);

const list = $('div.only-list1 ul li') // 定位到HTML结构中的li元素
.map((_, el) => {
const item = $(el); // 使用Cheerio包装每个li元素
const rawLink = item.find('a').attr('href');
const pubDate = item.find('span').text().trim(); // 提取日期

return {
title: item.find('a').text().trim(), // 获取标题
link: rawLink ? new URL(rawLink, rootUrl).href : rootUrl, // 生成完整链接
pubDate: timezone(parseDate(pubDate, 'YYYY年MM月DD日'), +8), // 解析并转换日期
description: '', // 没有提供简要描述,设为空字符串
};
})
.get();

Check warning

Code scanning / ESLint

Disallow specified syntax Warning

Please use .toArray() instead.

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link); // 获取详情页内容
const content = load(detailResponse.data); // 使用cheerio解析内容

item.description = content('div.ej_main').html() || item.description; // 提取内容区详情

return item; // 返回完整的item
})
)
);

return {
title: notice_type[type].title,
description: notice_type[type].title,
link: notice_type[type].url,
item: items,
};
}
98 changes: 56 additions & 42 deletions lib/routes/shu/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { load } from 'cheerio'; // cheerio@1.0.0
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const host = 'https://www.shu.edu.cn/';
const alias = new Map([
['news', 'zhxw'], // 综合新闻
['research', 'kydt1'], // 科研动态
['kydt', 'kydt1'], // 科研动态
['notice', 'tzgg'], // 通知公告
['important', 'zyxw'], // 重要新闻
]);
const notice_type = {
tzgg: { title: '上海大学 - 通知公告', url: 'https://www.shu.edu.cn/tzgg.htm' },
zyxw: { title: '上海大学 - 重要新闻', url: 'https://www.shu.edu.cn/zyxw.htm' },
};

export const route: Route = {
path: '/:type?',
path: '/news/:type?',
categories: ['university'],
example: '/shu/news',
parameters: { type: '消息类型,默认为`news`' },
example: '/shu/news/tzgg',
parameters: { type: '分类,默认为通知公告' },
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -28,50 +25,67 @@ export const route: Route = {
},
radar: [
{
source: ['www.shu.edu.cn/:type'],
target: '/:type',
source: ['www.shu.edu.cn/'],
target: '/news',
},
],
name: '官网信息',
maintainers: ['lonelyion'],
name: '官网通知公告',
maintainers: ['GhhG123', 'lonelyion'],
handler,
description: `| 综合新闻 | 科研动态 | 通知公告 | 重要新闻 |
| -------- | -------- | -------- | --------- |
| news | research | notice | important |`,
url: 'www.shu.edu.cn/',
description: `| 通知公告 | 重要新闻 |
| -------- | --------- |
| tzgg | zyxw |`,
};

async function handler(ctx) {
const type = ctx.req.param('type') || 'news';
const link = `https://www.shu.edu.cn/${alias.get(type) || type}.htm`;
const respond = await got.get(link);
const $ = load(respond.data);
const title = $('title').text();
const list = $('.ej_main .list')
.find('li')
.slice(0, 5)
.toArray()
.map((ele) => ({
title: $(ele).find('.bt').text(),
link: new URL($(ele).find('a').attr('href'), host).href,
date: $(ele).find('.sj').text(),
}));
const type = ctx.req.param('type') ?? 'tzgg';
const rootUrl = 'https://www.shu.edu.cn';

// 发起 HTTP GET 请求
const response = await got({
method: 'get',

/* headers: {
'user-agent': UA,
cookie: await getCookie(ctx),
}, */
url: notice_type[type].url,
});

const $ = load(response.data);

const all = await Promise.all(
const list = $('div.list ul li') // 以下获取信息需要根据网页结构定制
// For cheerio 1.x.x . The item parameter in the .map callback is now explicitly typed as a Cheerio<Element>, not just Element. --fixed
.map((_, el) => {
const item = $(el); // Wrap `el` in a Cheerio object
const rawLink = item.find('a').attr('href');
return {
title: item.find('p.bt').text().trim(),
link: rawLink ? new URL(rawLink, rootUrl).href : rootUrl,
pubDate: timezone(parseDate(item.find('p.sj').text().trim(), 'YYYY.MM.DD'), +8),
description: item.find('p.zy').text().trim(),
};
})
.get();

Check warning

Code scanning / ESLint

Disallow specified syntax Warning

Please use .toArray() instead.

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const response = await got.get(item.link);
const $ = load(response.data);
item.author = $('.xx>:nth-child(2)').text().trim().slice(3); // 投稿:xxx
item.pubDate = parseDate(item.date, 'YYYY.MM.DD');
item.description = $('.v_news_content').html() || item.title;
const detailResponse = await got(item.link);
const content = load(detailResponse.data);

item.description = content('div.ej_main').html() || item.description;

return item;
})
)
);

return {
title,
link,
item: all,
title: notice_type[type].title,
description: notice_type[type].title,
link: notice_type[type].url,
item: items,
};
}
14 changes: 7 additions & 7 deletions lib/routes/shu/jwb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ const host = 'https://jwb.shu.edu.cn/';
const alias = new Map([
['notice', 'tzgg'], // 通知公告
['news', 'xw'], // 新闻动态
['policy', 'zcwj'], // 政策文件
//['policy', 'zcwj'], // 政策文件 //BUG

Check failure

Code scanning / ESLint

Enforce consistent spacing after the `//` or `/*` in a comment Error

Expected space or tab after '//' in comment.
]);

export const route: Route = {
path: ['/jwc/:type?', '/jwb/:type?'],
path: ['/jwb/:type?'],
radar: [
{
source: ['www.shu.edu.cn/:type'],
target: '/:type',
source: ['www.shu.edu.cn/index'],
target: '/:type?',
},
],
name: 'Unknown',
maintainers: [],
name: '教务部',
maintainers: ['GhhG123'],
handler,
description: `| 通知通告 | 新闻 | 政策文件 |
description: `| 通知通告 | 新闻 | 政策文件(bug) |
| -------- | ---- | -------- |
| notice | news | policy |`,
};
Expand Down
3 changes: 2 additions & 1 deletion lib/routes/shu/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '上海大学',
url: 'jwb.shu.edu.cn',
url: 'www.shu.edu.cn',
description: '上海大学相关网网站',
lang: 'zh-CN',
};
Loading

0 comments on commit 8b48453

Please sign in to comment.