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: whu swrh #17689

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
132 changes: 132 additions & 0 deletions lib/routes/whu/swrh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 修改自计算机学院route
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { load } from 'cheerio';

const baseUrl = 'https://swrh.whu.edu.cn';

export const route: Route = {
path: '/swrh/:type',
categories: ['university'],
example: '/whu/swrh/2',
radar: [
{
source: ['swrh.whu.edu.cn/:type'],
target: '/swrh/:type',
},
],
parameters: { type: '公告类型,详见表格' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '水利水电学院公告',
maintainers: ['FanofZY'],
handler,
description: `| 公告类型 | 学院新闻 | 学术科研 | 通知公告 |
| -------- | -------- | -------- | -------- |
| 参数 | 0 | 1 | 2 |`,
};

async function handler(ctx) {
const type = Number.parseInt(ctx.req.param('type'));

let link;
switch (type) {
case 0:
link = `${baseUrl}/index/xyxw.htm`; // 学院新闻
break;

case 1:
link = `${baseUrl}/index/xsky.htm`; // 学术科研
break;

case 2:
link = `${baseUrl}/xxgk/tzgg.htm`; // 通知公告
break;

default:
throw new Error(`Unknown type: ${type}`);
}

const response = await got(link);
const $ = load(response.data);

let list;
if (type === 0) {
// 使用后一种 list 结构
list = $('div.my_box_nei')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.find('a b.am-text-truncate').text().trim(), // 获取标题
pubDate: item.find('a i').text().trim(), // 获取发布时间
link: new URL(item.find('a').attr('href'), baseUrl).href, // 构建完整链接
};
});
} else {
// 使用前一种 list 结构
list = $('div.list_txt.am-fr ul.am-list li')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.find('a span').text().trim(), // 获取标题
pubDate: item.find('a i').text().trim(), // 获取发布时间
link: new URL(item.find('a').attr('href'), baseUrl).href, // 构建完整链接
};
});
}
Fixed Show fixed Hide fixed

let items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
let response;
try {
// 实测发现有些链接无法访问
response = await got(item.link);
} catch {
return null;
TonyRL marked this conversation as resolved.
Show resolved Hide resolved
}
const $ = load(response.data);

if ($('.prompt').length) {
item.description = $('.prompt').html();
return item;
}

const content = $('.content');

content.find('img').each((_, e) => {
e = $(e);
if (e.attr('orisrc')) {
const newUrl = new URL(e.attr('orisrc'), baseUrl);
e.attr('src', newUrl.href);
e.removeAttr('orisrc');
e.removeAttr('vurl');
}
});

item.description = content.html();
TonyRL marked this conversation as resolved.
Show resolved Hide resolved
item.pubDate = $('meta[name="PubDate"]').length ? timezone(parseDate($('meta[name="PubDate"]').attr('content')), +8) : item.pubDate;

return item;
})
)
);
items = items.filter((item) => item !== null);

return {
title: $('title').first().text(),
link,
item: items,
};
}
Loading