feat(route): update routes for uestc jwc and gr (#17092)

* feat(route): update routes for uestc jwc and gr

* fix: add missing path arg of uestc gr

* feat: update example of uestc gr
This commit is contained in:
mobyw 2024-10-14 01:32:49 +08:00 committed by GitHub
parent 21e9405b53
commit 5360e267eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 116 additions and 67 deletions

View File

@ -1,17 +1,38 @@
import { Route } from '@/types';
import { Data, DataItem, Route } 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';
import InvalidParameterError from '@/errors/types/invalid-parameter';
import type { Context } from 'hono';
const baseUrl = 'https://gr.uestc.edu.cn/tongzhi/';
const baseIndexUrl = 'https://gr.uestc.edu.cn';
const baseUrl = 'https://gr.uestc.edu.cn/';
const detailUrl = 'https://gr.uestc.edu.cn/';
const dateTimeRegex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/;
const typeUrlMap = {
important: 'tongzhi/',
teaching: 'tongzhi/119',
degree: 'tongzhi/129',
student: 'tongzhi/122',
practice: 'tongzhi/123',
};
const typeNameMap = {
important: '重要公告',
teaching: '教学管理',
degree: '学位管理',
student: '学生管理',
practice: '就业实践',
};
export const route: Route = {
path: '/gr',
path: '/gr/:type?',
categories: ['university'],
example: '/uestc/gr',
parameters: {},
example: '/uestc/gr/student',
parameters: { type: '默认为 `important`' },
features: {
requireConfig: false,
requirePuppeteer: false,
@ -29,46 +50,53 @@ export const route: Route = {
maintainers: ['huyyi', 'mobyw'],
handler,
url: 'gr.uestc.edu.cn/',
description: `\
| | | | | |
| --------- | -------- | -------- | -------- | -------- |
| important | teaching | degree | student | practice |`,
};
async function handler() {
const response = await got.get(baseIndexUrl);
async function handler(ctx: Context): Promise<Data> {
const type = ctx.req.param('type') || 'important';
if (type in typeUrlMap === false) {
throw new InvalidParameterError('type not supported');
}
const typeName = typeNameMap[type];
const $ = load(response.data);
const indexContent = await ofetch(baseUrl + typeUrlMap[type]);
const items = [];
$('[href^="/tongzhi/"]').each((_, item) => {
items.push(baseIndexUrl + item.attribs.href);
const $ = load(indexContent);
const entries = $('div.title').toArray();
const items = entries.map(async (entry) => {
const element = $(entry);
const newsTitle = element.find('a').text() ?? '';
const newsLink = detailUrl + element.find('a').attr('href');
const newsDetail = await cache.tryGet(newsLink, async () => {
const newsContent = await ofetch(newsLink);
const content = load(newsContent);
const basicInfo = content('div.topic_detail_header').find('div.info').text();
const match = dateTimeRegex.exec(basicInfo);
return {
title: newsTitle,
link: newsLink,
pubDate: match ? timezone(parseDate(match[1]), +8) : null,
description: content('div.content').html(),
};
});
return newsDetail;
});
const out = await Promise.all(
items.map(async (newsUrl) => {
const newsDetail = await cache.tryGet(newsUrl, async () => {
const result = await got.get(newsUrl);
const $ = load(result.data);
const title = '[' + $('.over').text() + '] ' + $('div.title').text();
const author = $('.info').text().split('|')[1].trim().substring(3);
const date = parseDate($('.info').text().split('|')[0].trim().substring(4));
const description = $('.content').html();
return {
title,
link: newsUrl,
author,
pubDate: date,
description,
};
});
return newsDetail;
})
);
const out = await Promise.all(items);
return {
title: '研究生院通知',
title: `研究生院通知(${typeName}`,
link: baseUrl,
description: '电子科技大学研究生院通知公告',
item: out,
description: `电子科技大学研究生院通知(${typeName}`,
item: out as DataItem[],
};
}

View File

@ -1,22 +1,33 @@
import { Route } from '@/types';
import got from '@/utils/got';
import { Data, DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import InvalidParameterError from '@/errors/types/invalid-parameter';
const dateRegex = /(20\d{2})\/(\d{2})\/(\d{2})/;
import type { Context } from 'hono';
const baseUrl = 'https://www.jwc.uestc.edu.cn/';
const detailUrl = 'https://www.jwc.uestc.edu.cn/info/';
const map = {
const dateTimeRegex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/;
const typeUrlMap = {
important: 'hard/?page=1',
student: 'list/256/?page=1',
teacher: 'list/255/?page=1',
teach: 'list/40/?page=1',
teaching: 'list/40/?page=1',
office: 'list/ff80808160bcf79c0160c010a8d20020/?page=1',
};
const typeNameMap = {
important: '重要公告',
student: '学生事务公告',
teacher: '教师事务公告',
teaching: '教学新闻',
office: '办公室',
};
export const route: Route = {
path: '/jwc/:type?',
categories: ['university'],
@ -32,51 +43,61 @@ export const route: Route = {
},
radar: [
{
source: ['jwc.uestc.edu.cn/'],
source: ['www.jwc.uestc.edu.cn/'],
target: '/jwc',
},
],
name: '教务处',
maintainers: ['achjqz', 'mobyw'],
handler,
url: 'jwc.uestc.edu.cn/',
description: `| 重要公告 | 学生事务公告 | 教师事务公告 | 教学新闻 | 办公室 |
url: 'www.jwc.uestc.edu.cn/',
description: `\
| | | | | |
| --------- | ------------ | ------------ | -------- | ------ |
| important | student | teacher | teach | office |`,
| important | student | teacher | teaching | office |`,
};
async function handler(ctx) {
async function handler(ctx: Context): Promise<Data> {
const type = ctx.req.param('type') || 'important';
const pageUrl = map[type];
if (!pageUrl) {
if (type in typeUrlMap === false) {
throw new InvalidParameterError('type not supported');
}
const typeName = typeNameMap[type];
const response = await got.get(baseUrl + pageUrl);
const indexContent = await ofetch(baseUrl + typeUrlMap[type]);
const $ = load(response.data);
const $ = load(indexContent);
const entries = $('div.textAreo.clearfix').toArray();
const items = $('div.textAreo.clearfix');
const items = entries.map(async (entry) => {
const element = $(entry);
const newsTitle = element.find('a').attr('title') ?? '';
const newsLink = detailUrl + element.find('a').attr('newsid');
const out = $(items)
.map((_, item) => {
item = $(item);
const newsTitle = item.find('a').attr('title');
const newsLink = detailUrl + item.find('a').attr('newsid');
const newsDate = parseDate(item.find('i').text().replace(dateRegex, '$1-$2-$3'));
const newsDetail = await cache.tryGet(newsLink, async () => {
const newsContent = await ofetch(newsLink);
const content = load(newsContent);
const basicInfo = content('div.detail_header').find('div.item').text();
const match = dateTimeRegex.exec(basicInfo);
return {
title: newsTitle,
link: newsLink,
pubDate: newsDate,
pubDate: match ? timezone(parseDate(match[1]), +8) : null,
description: content('div.NewText').html(),
};
})
.get();
});
return newsDetail;
});
const out = await Promise.all(items);
return {
title: '教务处通知',
title: `教务处通知(${typeName}`,
link: baseUrl,
description: '电子科技大学教务处通知',
item: out,
description: `电子科技大学教务处通知(${typeName}`,
item: out as DataItem[],
};
}