Skip to content

Commit

Permalink
feat(route): add 西南交通大学 (#18463)
Browse files Browse the repository at this point in the history
* feat(route): add 西南交通大学计算机学院

* fix: 修正西南交通大学URL

* feat(route): add 西南交通大学体育学院

* fix(route): 西南交通大学体育学院
  • Loading branch information
AzureG03 authored Feb 27, 2025
1 parent c8d9d1f commit 26d077c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/routes/swjtu/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '西南交通大学',
url: 'ctt.swjtu.edu.cn',
url: 'www.swjtu.edu.cn',
lang: 'zh-CN',
};
54 changes: 34 additions & 20 deletions lib/routes/swjtu/scai/bks.ts → lib/routes/swjtu/scai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { parseDate } from '@/utils/parse-date';
import ofetch from '@/utils/ofetch';

const rootURL = 'https://scai.swjtu.edu.cn';
const pageURL = `${rootURL}/web/page-module.html?mid=B730BEB095B31840`;

export const route: Route = {
path: '/scai/bks',
path: '/scai/:type',
categories: ['university'],
example: '/swjtu/scai/bks',
parameters: {},
parameters: { type: '通知类型' },
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -26,11 +25,32 @@ export const route: Route = {
},
],
name: '计算机与人工智能学院',
description: '本科生教育',
description: `
| 分区 | 参数 |
| ----------------- | ----------- |
| 本科生教育 | bks |
| 研究生教育 | yjs |
| 学生工作 | xsgz |
`,
maintainers: ['AzureG03', 'SuperJeason'],
handler,
};

const partition = {
bks: {
title: '本科生教育',
url: `${rootURL}/web/page-module.html?mid=B730BEB095B31840`,
},
yjs: {
title: '研究生教育',
url: `${rootURL}/web/page-module.html?mid=6A69B0E32021446B`,
},
xsgz: {
title: '学生工作',
url: `${rootURL}/web/page-module.html?mid=F3D3909EB1861B5D`,
},
};

const getItem = (item, cache) => {
const title = item.find('a').text();
const link = `${rootURL}${item.find('a').attr('href').slice(2)}`;
Expand All @@ -39,22 +59,14 @@ const getItem = (item, cache) => {
const res = await ofetch(link);
const $ = load(res);

// 尝试获取日期
let dateText = $('div.news-info span:nth-of-type(2)').text();
// 转教务通知时的时间获取方法
if (!dateText) {
dateText = $('div.news-top-bar span:nth-of-type(1)').text();
}
if (!dateText) {
// console.error('Date not found for', link);
return null; // 或者返回一个默认值
}

const dateMatch = dateText.match(/\d{4}(-|\/|.)\d{1,2}\1\d{1,2}/);
if (!dateMatch) {
// console.error('Invalid date format for', link);
return null; // 或者返回一个默认值
}
const pubDate = parseDate(dateMatch[0]);
// 'date' may be undefined. and 'parseDate' will return current time.
const date = dateText.match(/\d{4}(-|\/|.)\d{1,2}\1\d{1,2}/)?.[0];
const pubDate = parseDate(date);
const description = $('div.content-main').html();

return {
Expand All @@ -66,8 +78,10 @@ const getItem = (item, cache) => {
});
};

async function handler() {
const res = await ofetch(pageURL);
async function handler(ctx) {
const { type } = ctx.req.param();
const url = partition[type].url;
const res = await ofetch(url);

const $ = load(res);
const $list = $('div.list-top-item, div.item-wrapper');
Expand All @@ -80,8 +94,8 @@ async function handler() {
);

return {
title: '西南交大计算机学院-本科生教育',
link: pageURL,
title: `西南交大计院-${partition[type].title}`,
link: url,
item: items,
allowEmpty: true,
};
Expand Down
77 changes: 77 additions & 0 deletions lib/routes/swjtu/sports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { ofetch } from 'ofetch';

const rootURL = 'https://sports.swjtu.edu.cn';
const pageURL = `${rootURL}/xwzx.htm`;

export const route: Route = {
path: '/sports',
categories: ['university'],
example: '/swjtu/sports',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['sports.swjtu.edu.cn/'],
},
],
name: '体育学院',
description: '新闻资讯',
maintainers: ['AzureG03'],
handler,
};

const getItem = (item, cache) => {
const title = item.find('p.toe').text();
const link = `${rootURL}/${item.find('a').attr('href')}`;

return cache.tryGet(link, async () => {
const res = await ofetch(link);
const $ = load(res);

const pubDate = parseDate(
$('div.info span:nth-of-type(3)')
.text()
.slice(3)
.match(/\d{4}(-|\/|.)\d{1,2}\1\d{1,2}/)?.[0]
);
const description = $('div.detail-wrap').html();
return {
title,
pubDate,
link,
description,
};
});
};

async function handler() {
const res = await ofetch(pageURL);

const $ = load(res);
const $list = $('div.news-list > ul > li');

const items = await Promise.all(
$list.toArray().map((i) => {
const $item = $(i);
return getItem($item, cache);
})
);

return {
title: '西南交大体院-新闻资讯',
link: pageURL,
item: items,
allowEmpty: true,
};
}

0 comments on commit 26d077c

Please sign in to comment.