refactor(routes/hrbust/jwzx): refactor jwzx to adapt new route standard (#18111)

This commit is contained in:
cnk
2025-01-13 22:28:10 +08:00
committed by GitHub
parent 34964881d5
commit f0cf4daa8b
3 changed files with 68 additions and 103 deletions
+68 -37
View File
@@ -1,28 +1,25 @@
import { Route } from '@/types';
import { Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import utils from './utils';
const typeMap = {
351: {
name: '名师风采',
},
353: {
name: '热点新闻',
},
354: {
name: '教务公告',
},
355: {
name: '教学新闻',
},
};
import ofetch from '@/utils/ofetch';
import timezone from '@/utils/timezone';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/jwzx/:type?/:page?',
categories: ['university'],
name: '教务处',
url: 'jwzx.hrbust.edu.cn',
maintainers: ['LenaNouzen', 'cscnk52'],
handler,
example: '/hrbust/jwzx',
parameters: { type: '分类名,默认为教务公告', page: '文章数,默认为12' },
parameters: { type: '分类编号,默认为 354(教务公告),具体见下表', page: '文章数,默认为 12' },
description: `::: tip
由于源站未提供精确时间,只能抓取日期粒度的时间。
:::
| 339 | 340 | 342 | 346 | 351 | 353 | 354 | 355 | 442 | 443 | 444 | 445 | 2106 | 2332 | 2333 | 2334 | 2335 | 2336 | 2730 | 2855 | 2857 | 2859 | 3271 | 3508 | 3519 | 3981 | 4057 | 4058 | 4059 | 4060 | 4061 | 4062 |
|------|------|------|------|------|------|------|------|------|------|------|------|--------|------|------|------|------|------|----------|------|------|------|------|---------|-------------|------|------|------|------|------|------|------|
| 组织机构 | 工作职责 | 专业设置 | 教务信箱 | 名师风采 | 热点新闻 | 教务公告 | 教学新闻 | 教学管理 | 教务管理 | 学籍管理 | 实践教学 | 系统使用动画 | 教学管理 | 教务管理 | 学籍管理 | 实验教学 | 实践教学 | 教研论文教材认定 | 教学管理 | 学籍管理 | 实践教学 | 网络教学 | 多媒体教室管理 | 实验教学与实验室管理 | 教学成果 | 国创计划 | 学科竞赛 | 微专业 | 众创空间 | 示范基地 | 学生社团 |`,
categories: ['university'],
features: {
requireConfig: false,
requirePuppeteer: false,
@@ -30,36 +27,70 @@ export const route: Route = {
supportBT: false,
supportPodcast: false,
supportScihub: false,
supportRadar: true,
},
name: '教务处',
maintainers: ['LenaNouzen'],
handler,
description: `| 名师风采 | 热点新闻 | 教务公告 | 教学新闻 |
| -------- | -------- | -------- | -------- |
| 351 | 353 | 354 | 355 |`,
radar: [
{
source: ['jwzx.hrbust.edu.cn/homepage/index.do'],
target: '/jwzx',
},
],
view: ViewType.Notifications,
};
async function handler(ctx) {
const page = ctx.req.param('page') || '12';
const base = utils.columnIdBase(ctx.req.param('type')) + '&pagingNumberPer=' + page;
const res = await got(base);
const info = utils.fetchAllArticle(res.data, utils.JWZXBASE);
const JWZXBASE = 'http://jwzx.hrbust.edu.cn/homepage/';
const { type = 354, page = 12 } = ctx.req.param();
const url = JWZXBASE + 'infoArticleList.do?columnId=' + type + '&pagingNumberPer=' + page;
const details = await Promise.all(info.map((e) => utils.detailPage(e, cache)));
const response = await ofetch(url);
const $ = load(response);
// ctx.set('json', {
// info,
// };
const bigTitle = $('.columnTitle .wow span').text().trim();
const list = $('div.articleList li')
.toArray()
.map((item) => {
const element = $(item);
const link = new URL(element.find('a').attr('href'), JWZXBASE).href;
const title = element.find('a').text().trim();
const pubDateText = element.find('span').text().trim();
const pubDate = timezone(parseDate(pubDateText), +8);
return {
title,
link,
pubDate,
};
});
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
if (!item.link.startsWith(JWZXBASE)) {
item.description = '本文需跳转,请点击标题后阅读';
return item;
}
const response = await ofetch(item.link);
const $ = load(response);
const body = $('div.body');
body.find('[style]').removeAttr('style');
body.find('font').contents().unwrap();
body.find(':empty').remove();
body.html(body.html()?.replaceAll(' ', ''));
body.find('[align]').removeAttr('align');
item.description = body.html();
if (item.description === null) {
item.description = '解析正文失败';
}
return item;
})
)
);
return {
title: '哈理工教务处' + typeMap[ctx.req.param('type') || 354].name,
link: base,
item: details,
title: `哈尔滨理工大学教务处 - ${bigTitle}`,
link: JWZXBASE,
item: items,
};
}
@@ -1 +0,0 @@
{{@ desc }}
-65
View File
@@ -1,65 +0,0 @@
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import { load } from 'cheerio';
import got from '@/utils/got';
import { art } from '@/utils/render';
import path from 'node:path';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
// const base = 'http://hrbust.edu.cn';
const jwzxBase = 'http://jwzx.hrbust.edu.cn/homepage/';
const columnIdBase = (id) => (id ? `${jwzxBase}infoArticleList.do?columnId=${id}` : `${jwzxBase}infoArticleList.do?columnId=354`);
const renderDesc = (desc) =>
art(path.join(__dirname, 'templates/description.art'), {
desc,
});
const detailPage = (e, cache) =>
cache.tryGet(e.detailPage, async () => {
const result = await got(e.detailPage);
const $ = load(result.data);
const title = $('div#article h2').text().trim();
const desc =
$('div.body').html() &&
$('div.body')
.html()
.replaceAll(/style="(.*?)"/g, '')
.trim();
const pubDate = timezone(parseDate($('div#articleInfo ul li').first().text().replaceAll('发布日期:', '').trim()), +8);
return {
title: e.title || title,
description: renderDesc(desc),
link: e.detailPage,
pubDate: e.pubDate || pubDate,
};
});
const fetchAllArticle = (data, base) => {
const $ = load(data);
const article = $('.articleList li div');
const info = article
.map((i, e) => {
const c = load(e);
const r = {
title: c('a').first().text().trim(),
detailPage: new URL(c('a').attr('href'), base).href,
pubDate: timezone(parseDate(c('span').first().text().trim()), +8),
};
return r;
})
.get();
return info;
};
export default {
// BASE: base,
JWZXBASE: jwzxBase,
columnIdBase,
fetchAllArticle,
detailPage,
renderDesc,
};