Skip to content

Commit

Permalink
feat(route): 添加北京邮电大学教务处的路由 (DIYgod#16729)
Browse files Browse the repository at this point in the history
* 添加bupt教务管理通知管理路由

* 添加bupt教务管理新闻资讯路由

* 修改bupt教务管理新闻资讯路由的描述

* 整合路由

* 修复对const变量赋值的错误

* 微调代码

* fix:Fixed a bug that caused duplicate requests for the same link

* fix: route name

---------
  • Loading branch information
Yoruet authored Sep 15, 2024
1 parent 87723d1 commit 41a1e49
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions lib/routes/bupt/jwc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import timezone from '@/utils/timezone';
import { parseDate } from '@/utils/parse-date';
import type { Context } from 'hono';

export const route: Route = {
path: '/jwc/:type',
categories: ['university'],
example: '/bupt/jwc/tzgg',
parameters: {
type: {
type: 'string',
optional: false,
description: '信息类型,可选值:tzgg(通知公告),xwzx(新闻资讯)',
},
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['jwc.bupt.edu.cn/tzgg1.htm'],
target: '/jwc/tzgg',
},
{
source: ['jwc.bupt.edu.cn/xwzx2.htm'],
target: '/jwc/xwzx',
},
],
name: '教务处',
maintainers: ['Yoruet'],
handler,
url: 'https://jwc.bupt.edu.cn/',
};

async function handler(ctx: Context) {
let type = ctx.req.param('type'); // 默认类型为通知公告
if (!type) {
type = 'tzgg';
}
const rootUrl = 'https://jwc.bupt.edu.cn';
let currentUrl;
let pageTitle;

if (type === 'tzgg') {
currentUrl = `${rootUrl}/tzgg1.htm`;
pageTitle = '通知公告';
} else if (type === 'xwzx') {
currentUrl = `${rootUrl}/xwzx2.htm`;
pageTitle = '新闻资讯';
} else {
throw new Error('Invalid type parameter');
}

const response = await got({
method: 'get',
url: currentUrl,
});

const $ = load(response.data);

const list = $('.txt-elise')
.map((_, item) => {
const $item = $(item);
const $link = $item.find('a');
// Skip elements without links or with empty href
if ($link.length === 0 || !$link.attr('href')) {
return null;
}
return {
title: $link.text().trim(),
link: rootUrl + '/' + $link.attr('href'),
};
})
.get()
.filter(Boolean);

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});

const content = load(detailResponse.data);

// 选择包含新闻内容的元素
const newsContent = content('.v_news_content');

// 移除不必要的标签,比如 <p> 和 <span> 中无用的内容
newsContent.find('p, span, strong').each(function () {
const element = content(this);
const text = element.text().trim();

// 删除没有有用文本的元素,防止空元素被保留
if (text === '') {
element.remove();
} else {
// 去除多余的嵌套标签,但保留其内容
element.replaceWith(text);
}
});

// 清理后的内容转换为文本
const cleanedDescription = newsContent.text().trim();

// 提取并格式化发布时间
item.description = cleanedDescription;
item.pubDate = timezone(parseDate(content('.info').text().replace('发布时间:', '').trim()), +8);

return item;
})
)
);

return {
title: `北京邮电大学教务处 - ${pageTitle}`,
link: currentUrl,
item: items,
};
}

0 comments on commit 41a1e49

Please sign in to comment.