Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 4 additions & 0 deletions lib/routes/dut/defaults.ts → lib/routes/dlut/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default {
// 默认 -> 新闻快递 http://teach.dlut.edu.cn/xwkd/xwkd.htm
teach: 'xwkd/xwkd',

// 软件学院:https://ss.dlut.edu.cn
// 默认 -> 本科生通知 https://ss.dlut.edu.cn/rcpy/bkspy/bkstz.htm
ss: 'rcpy/bkspy/bkstz',

// 人事处:http://perdep.dlut.edu.cn
// 默认 -> 通知公告 http://perdep.dlut.edu.cn/tzgg/tzgg.htm
perdep: 'tzgg/tzgg',
Expand Down
73 changes: 56 additions & 17 deletions lib/routes/dut/index.ts → lib/routes/dlut/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { load } from 'cheerio';

import InvalidParameterError from '@/errors/types/invalid-parameter';
import type { Route } from '@/types';
import type { DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
Expand All @@ -10,21 +10,41 @@
import defaults from './defaults';
import shortcuts from './shortcuts';

type DlutItem = DataItem & {
link: string;
};

export const route: Route = {
path: ['/*/*', '/:0?'],
name: 'Unknown',
path: ['/:site/:category{.+}', '/:site?'],
name: '站点栏目',
maintainers: [],
handler,
example: '/dlut/ss/bkstz',
parameters: {
site: '站点,如 `news`、`teach`、`ss`,默认为 `news`',
category: '栏目路径或快捷名称,如 `bkstz`,默认为对应站点的默认栏目',
},
description: '大连理工大学各站点栏目,支持直接填写栏目路径或使用内置快捷名称。',
categories: ['university'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
};

async function handler(ctx) {
const site = ctx.params[0] ?? 'news';
const { site = 'news' } = ctx.req.param();
if (!isValidHost(site)) {
throw new InvalidParameterError('Invalid site');
}

let items;
let category = ctx.params[1] ?? (Object.hasOwn(defaults, site) ? defaults[site] : '');
let { category = Object.hasOwn(defaults, site) ? defaults[site] : '' } = ctx.req.param();
category = Object.hasOwn(shortcuts, site) && Object.hasOwn(shortcuts[site], category) ? shortcuts[site][category] : category;

const rootUrl = `https://${site}.dlut.edu.cn`;
Expand All @@ -37,23 +57,42 @@

const $ = load(response.data);

if (site === 'panjin') {
items = $('a.news').slice(0, -4);
} else if (site === 'fldpj') {
items = $('li[id^="line_u9"]').find('a');
} else {
$('.Next, .rjxw_left, .pb_sys_common').remove();
items = $('.txt, .itemlist, .wall, .list, .list01, .ny_list, .rjxw_right, .rj_yjs_con, .c_hzjl_list1, .winstyle67894, .winstyle80936, .winstyle50738, #lili').find('a');
switch (site) {
case 'panjin': {

Check failure

Code scanning / oxlint

unicorn(switch-case-braces) Error

Unnecessary braces in case clause.
Remove Braces for case clause.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
items = $('a.news').slice(0, -4);

break;
}
case 'fldpj': {

Check failure

Code scanning / oxlint

unicorn(switch-case-braces) Error

Unnecessary braces in case clause.
Remove Braces for case clause.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
items = $('li[id^="line_u9"]').find('a');

break;
}
case 'ss': {

Check failure

Code scanning / oxlint

unicorn(switch-case-braces) Error

Unnecessary braces in case clause.
Remove Braces for case clause.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
items = $('.list04 .item a');

break;
}
default: {

Check failure

Code scanning / oxlint

unicorn(switch-case-braces) Error

Unnecessary braces in case clause.
Remove Braces for case clause.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
$('.Next, .rjxw_left, .pb_sys_common').remove();
items = $('.txt, .itemlist, .wall, .list, .list01, .ny_list, .rjxw_right, .rj_yjs_con, .c_hzjl_list1, .winstyle67894, .winstyle80936, .winstyle50738, #lili').find('a');
}
}

items = items
.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 50)
.toArray()
.map((item) => {
.flatMap((item) => {
item = $(item);

const result = {
link: item.attr('href').startsWith('http') ? item.attr('href') : `${rootUrl}/${item.attr('href').replace(/^[./]+/, '')}`,
const href = item.attr('href');
if (!href) {
return [];
}

const result: DlutItem = {
title: '',
link: href.startsWith('http') ? href : `${rootUrl}/${href.replace(/^[./]+/, '')}`,
};

if (site === 'fldpj') {
Expand All @@ -67,13 +106,13 @@
dateMatch = item.parent().parent().text().match(dateRegex);
}

result.title = item.text().trim() === '' ? item.next().text() : item.text();
result.title = item.attr('title') ?? (item.find('h2').text() || item.text());
if (dateMatch) {
result.pubDate = parseDate(dateMatch[1].replaceAll(/年|月/g, '-'));
}
}

return result;
return [result];
});

items = await Promise.all(
Expand Down
File renamed without changes.
10 changes: 9 additions & 1 deletion lib/routes/dut/shortcuts.ts → lib/routes/dlut/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ export default {
},
teach: {
xwkd: 'xwkd/xwkd',
zytg: 'zytg/zytg',
zytg: 'index',
bhgs: 'zytg/bhgs',
},
ss: {
bkstz: 'rcpy/bkspy/bkstz',
cxsjhdtz: 'rcpy/cxsj/hdtz',
gjjl: 'gjhzjl/gjjl',
gjjltzgg: 'gjhzjl/tzgg',
xsgztzgg: 'xsgz/tzgg',
xshd: 'xsgz/xshd',
},
perdep: {
tzgg: 'tzgg/tzgg',
gzdt: 'gzdt/gzdt1',
Expand Down