fix(route): 西安交通大学 教务处 (#15182)

* fix(route): 西安交通大学 教务处

* Update lib/routes/xjtu/dean.ts

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

---------
This commit is contained in:
Yunfi 2024-04-12 17:49:00 +08:00 committed by GitHub
parent a4c1639596
commit 8875432155
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 23 deletions

View File

@ -1,6 +1,6 @@
import { Route } from '@/types';
import { Route, Data, DataItem } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
@ -32,8 +32,10 @@ const parseContent = (htmlString) => {
export const route: Route = {
path: '/dean/:subpath{.+}',
name: 'Unknown',
maintainers: [],
name: '教务处',
maintainers: ['hoilc'],
example: '/xjtu/dean/jxxx/jxtz2',
description: '打开一个类似 <https://dean.xjtu.edu.cn/jxxx/jxtz2.htm> 的网址,在 `.cn` 后的内容就是 subpath此例中是 `jxxx/jxtz2`',
handler,
};
@ -43,38 +45,35 @@ async function handler(ctx) {
const url = `http://dean.xjtu.edu.cn/${subpath.replaceAll('.htm', '')}.htm`;
const base = url.split('/').slice(0, -1).join('/');
const list_response = await got(url);
const $ = load(list_response.data);
const list_response = await ofetch(url);
const $ = load(list_response);
const subname = $('em.ma-nav a')
.slice(1)
.map(function () {
return $(this).text();
})
.get()
const subName = $('#ny-main > div.ny-tit > div > span')
.toArray()
.map((item) => $(item).text())
.join(' - ');
const list = $('.list_main_content > .list-li')
const list = $('#ny-main > div.ny.wp > ul > li')
.toArray()
.map((item) => {
.map((item: any) => {
item = $(item);
const title = item.find('a').attr('title');
const title = item.find('a').text();
const link = new URL(item.find('a').attr('href'), base).href;
return {
title,
link,
pubDate: timezone(parseDate(item.find('.list_time').text(), 'YYYY-MM-DD'), +8),
pubDate: timezone(parseDate(item.find('span').text(), 'YYYY-MM-DD'), +8),
};
});
const out = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
list.map((item: DataItem) =>
cache.tryGet(item.link!, async () => {
try {
const response = await got(item.link);
const result = parseContent(response.data);
const response = await ofetch(item.link!);
const result = parseContent(response);
item.description = result.description;
item.description = result.description ?? undefined;
item.author = result.author;
} catch {
return item;
@ -85,8 +84,8 @@ async function handler(ctx) {
);
return {
title: `西安交大教务处 - ${subname}`,
title: `西安交大教务处 - ${subName}`,
link: url,
item: out.filter((item) => item !== ''),
};
} as Data;
}