feat: add route of uestc bbs (#16892)

* feat: add route of uestc bbs

* Update lib/routes/uestc/bbs.ts

路由应该是/uestc/bbs/newthread?

* amend: 按意见修改变量名

Signed-off-by: Jinkin <nadph513@outlook.com>

* fix: namespace url

---------

Signed-off-by: Jinkin <nadph513@outlook.com>
This commit is contained in:
Jinkin
2024-09-27 00:37:58 +08:00
committed by GitHub
parent f107701de1
commit 57df3f698e
3 changed files with 106 additions and 1 deletions
+8
View File
@@ -284,6 +284,10 @@ export type Config = {
authenticationSecret?: string[];
authToken?: string[];
};
uestc: {
bbs_cookie?: string;
bbs_auth_str?: string;
};
weibo: {
app_key?: string;
app_secret?: string;
@@ -668,6 +672,10 @@ const calculateValue = () => {
authenticationSecret: envs.TWITTER_AUTHENTICATION_SECRET?.split(','),
authToken: envs.TWITTER_AUTH_TOKEN?.split(','),
},
uestc: {
bbsCookie: envs.UESTC_BBS_COOKIE,
bbsAuthStr: envs.UESTC_BBS_AUTH_STR,
},
weibo: {
app_key: envs.WEIBO_APP_KEY,
app_secret: envs.WEIBO_APP_SECRET,
+97
View File
@@ -0,0 +1,97 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch'; // 统一使用的请求库
import { parseDate } from '@/utils/parse-date'; // 解析日期的工具函数
import timezone from '@/utils/timezone';
import { config } from '@/config';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import ConfigNotFoundError from '@/errors/types/config-not-found';
export const route: Route = {
path: '/bbs/:types?',
name: '清水河畔',
maintainers: ['huyyi'],
categories: ['university'],
url: 'bbs.uestc.edu.cn',
example: '/uestc/bbs/newthread',
parameters: { types: '选择内容类型(多选`,`分割),可选值:[newreply,newthread,digest,life,hotlist]。默认为所有。' },
features: {
requireConfig: [
{
name: 'UESTC_BBS_COOKIE',
optional: false,
description: '河畔的cookie',
},
{
name: 'UESTC_BBS_AUTH_KEY',
optional: false,
description: '河畔Header中的authorization字段',
},
],
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
description: `
:::tip
仅支持自建,您需要设置以下配置才能正常使用:
- 河畔cookie: \`UESTC_BBS_COOKIE\`
- Header中的授权字段: \`UESTC_BBS_AUTH_KEY\`
:::
`,
radar: [
{
source: ['bbs.uestc.edu.cn/*'],
target: '/bbs/newthread',
},
],
handler: async (ctx) => {
const { bbsCookie, bbsAuthStr } = config.uestc;
if (!bbsCookie || !bbsAuthStr) {
throw new ConfigNotFoundError('未配置 Cookie 或 Authorization。请检查配置。');
}
const { types = 'newreply,newthread,digest,life,hotlist' } = ctx.req.param();
const data = await ofetch(`https://bbs.uestc.edu.cn/star/api/v1/index?top_list=${types}`, {
headers: {
Cookie: bbsCookie,
Referer: 'https://bbs.uestc.edu.cn/new',
authorization: bbsAuthStr,
},
});
const itemsRaw = Object.entries(data.data.top_list).flatMap(([label, items]) => items.map((item) => ({ ...item, label })));
const items = await Promise.all(
itemsRaw.map((item) =>
cache.tryGet(`https://bbs.uestc.edu.cn/forum.php?mod=viewthread&tid=${item.thread_id}`, async () => {
const response = await ofetch(`https://bbs.uestc.edu.cn/forum.php?mod=viewthread&tid=${item.thread_id}`, {
headers: {
Cookie: bbsCookie,
Referer: 'https://bbs.uestc.edu.cn',
authorization: bbsAuthStr,
},
});
const $ = load(response);
item.description = $('div#postlist').html();
return {
title: item.subject,
link: `https://bbs.uestc.edu.cn/thread/${item.thread_id}`,
author: item.author,
category: item.label,
img: item.icon,
pubDate: timezone(parseDate(item.dateline), +8),
description: item.description,
};
})
)
);
return {
// 源标题
title: '清水河畔',
// 源链接
link: 'https://bbs.uestc.edu.cn/new',
// 源文章
item: items,
};
},
};
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '电子科技大学',
url: 'gr.uestc.edu.cn',
url: 'www.uestc.edu.cn',
};