feat(route): dxy board (#17294)

* feat(route): dxy board

* fix: update types
This commit is contained in:
Tony
2024-10-24 16:04:31 -07:00
committed by GitHub
parent 9a651febef
commit 97d0369d2e
5 changed files with 320 additions and 19 deletions
+96
View File
@@ -0,0 +1,96 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { phoneBaseUrl, webBaseUrl, generateNonce, sign, getPost } from './utils';
import { config } from '@/config';
import { BoardInfo, PostListData } from './types';
export const route: Route = {
path: '/bbs/board/:boardId',
categories: ['bbs'],
example: '/dxy/bbs/board/46',
parameters: { specialId: '板块 ID,可在对应板块页 URL 中找到' },
name: '板块',
maintainers: ['TonyRL'],
radar: [
{
source: ['www.dxy.cn/bbs/newweb/pc/category/:boardIdId'],
target: '/bbs/board/:boardIdId',
},
{
source: ['3g.dxy.cn/bbs/board/:boardIdId'],
target: '/bbs/board/:boardIdId',
},
],
handler,
};
async function handler(ctx) {
const { boardId } = ctx.req.param();
const { limit = '20' } = ctx.req.query();
const boardDetail = (await cache.tryGet(`dxy:board:detail:${boardId}`, async () => {
const detailParams = {
boardId,
timestamp: Date.now(),
noncestr: generateNonce(8, 'number'),
};
const detail = await ofetch(`${phoneBaseUrl}/bbsapi/bbs/board/detail`, {
query: {
...detailParams,
sign: sign(detailParams),
},
});
if (detail.code !== 'success') {
throw new Error(detail.message);
}
return detail.data;
})) as BoardInfo;
const boardList = (await cache.tryGet(
`dxy:board:list:${boardId}`,
async () => {
const listParams = {
boardId,
postType: '0',
orderType: '1',
pageNum: '1',
pageSize: limit,
timestamp: Date.now(),
noncestr: generateNonce(8, 'number'),
};
const recommendList = await ofetch(`${phoneBaseUrl}/bbsapi/bbs/board/post/list`, {
query: {
...listParams,
sign: sign(listParams),
},
});
if (recommendList.code !== 'success') {
throw new Error(recommendList.message);
}
return recommendList.data;
},
config.cache.routeExpire,
false
)) as PostListData;
const list = boardList.result.map((item) => ({
title: item.subject,
author: item.postUser.nickname,
category: [boardDetail.title],
link: `${webBaseUrl}/bbs/newweb/pc/post/${item.postId}`,
postId: item.postId,
}));
const items = await Promise.all(list.map((item) => getPost(item, cache.tryGet)));
return {
title: boardDetail.title,
description: `${boardDetail.postCount} 內容 ${boardDetail.followCount} 关注`,
link: `${webBaseUrl}/bbs/newweb/pc/category/${boardId}`,
image: boardDetail.boardAvatar,
item: items,
};
}
+1 -1
View File
@@ -89,7 +89,7 @@ async function handler(ctx) {
description: postInfo.simpleBody,
pubDate: parseDate(createdTime, 'x'),
author: postInfo.postUser.nickname,
category: postInfo.boardInfo.title,
category: [postInfo.boardInfo.title],
link: `${webBaseUrl}/bbs/newweb/pc/post/${entityId}`,
postId: entityId,
};
+11 -10
View File
@@ -1,9 +1,10 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { phoneBaseUrl, webBaseUrl, generateNonce, sign, getPost } from './utils';
import { config } from '@/config';
import { RecommendListData, SpecialBoardDetail } from './types';
export const route: Route = {
path: '/bbs/special/:specialId',
@@ -27,7 +28,7 @@ async function handler(ctx) {
const specialId = ctx.req.param('specialId');
const { limit = '10' } = ctx.req.query();
const specialDetail = await cache.tryGet(`dxy:special:detail:${specialId}`, async () => {
const specialDetail = (await cache.tryGet(`dxy:special:detail:${specialId}`, async () => {
const detailParams = {
specialId,
requestType: 'h5',
@@ -35,8 +36,8 @@ async function handler(ctx) {
noncestr: generateNonce(8, 'number'),
};
const { data: detail } = await got(`${phoneBaseUrl}/newh5/bbs/special/detail`, {
searchParams: {
const detail = await ofetch(`${phoneBaseUrl}/newh5/bbs/special/detail`, {
query: {
...detailParams,
sign: sign(detailParams),
},
@@ -45,9 +46,9 @@ async function handler(ctx) {
throw new Error(detail.message);
}
return detail.data;
});
})) as SpecialBoardDetail;
const recommendList = await cache.tryGet(
const recommendList = (await cache.tryGet(
`dxy:special:recommend-list-v3:${specialId}`,
async () => {
const listParams = {
@@ -59,8 +60,8 @@ async function handler(ctx) {
noncestr: generateNonce(8, 'number'),
};
const { data: recommendList } = await got(`${phoneBaseUrl}/newh5/bbs/special/post/recommend-list-v3`, {
searchParams: {
const recommendList = await ofetch(`${phoneBaseUrl}/newh5/bbs/special/post/recommend-list-v3`, {
query: {
...listParams,
sign: sign(listParams),
},
@@ -72,7 +73,7 @@ async function handler(ctx) {
},
config.cache.routeExpire,
false
);
)) as RecommendListData;
const list = recommendList.result.map((item) => {
const { postInfo, dataTime, entityId } = item;
@@ -81,7 +82,7 @@ async function handler(ctx) {
description: postInfo.simpleBody,
pubDate: parseDate(dataTime, 'x'),
author: postInfo.postUser.nickname,
category: postInfo.postSpecial.specialName,
category: [postInfo.postSpecial.specialName],
link: `${webBaseUrl}/bbs/newweb/pc/post/${entityId}`,
postId: entityId,
};
+195
View File
@@ -0,0 +1,195 @@
interface BoardPerm {
errCode: number;
}
interface PostPerm {
errCode: number;
}
export interface BoardInfo {
id: number;
boardId: number;
title: string;
shortTitle: string;
postCount: number;
followCount: number;
boardAvatar: string;
boardPerm: BoardPerm;
followStatus: boolean;
}
interface PostUser {
userId: number;
avatar: string;
username: string;
nickname: string;
bbsStatus: number;
userTitle: UserTitle;
}
interface PostDetail {
postId: number;
boardId: number;
reads: number;
pv: number;
subject: string;
postUser: PostUser;
screenUrlList: string[];
videoScreen: boolean;
isEditorRecommend: boolean;
}
export interface PostListData {
pageNum: number;
pageSize: number;
total: number;
result: PostDetail[];
empty: boolean;
}
interface UserTitle {
type: number;
titles: string[];
}
interface TagInfo {
tagName: string;
}
interface PostSpecial {
specialId: number;
specialName: string;
followCount: number;
postCount: number;
followStatus: boolean;
personalSpecial: boolean;
avatar: string;
customAvatar: string;
backgroundColor: string;
type: number;
}
interface Video {
videoId: number;
videoUrl: string;
cover: string;
size: number;
duration: number;
auditState: number;
userVideoState: number;
}
interface Post {
id: number;
rootId: number;
createTime: number;
simpleBody: string;
subject: string;
body: string;
coverPic: string;
isVideoCover: boolean;
isVideoCoverPic: boolean;
postTime: number;
reads: number;
pv: number;
replies: number;
qualityPost: boolean;
contentType: number;
postStatus: number;
signatured: number;
userVote: boolean;
postSpecial: PostSpecial;
isEditorRecommend: boolean;
isTop: boolean;
ipLocation: string;
isCurrentUser: boolean;
anonymous: boolean;
boardInfo: BoardInfo;
votes: number;
archived: boolean;
approved: boolean;
video?: Video;
postPerm: PostPerm;
showStatus: boolean;
postUser: PostUser;
hintInfos: false[];
isDisableRepost: boolean;
tagInfos: TagInfo[];
isVoteActivityPost: boolean;
isShowCaseTag: boolean;
isPaidContent: boolean;
isNeedPay: boolean;
}
export interface PostData {
code: string;
message: string;
data: Post;
}
interface SpecialAdmin {
userId: number;
username: string;
nickname: string;
avatar: string;
followers: number;
bbsVotes: number;
level: number;
talentStatus: number;
levelStatus: number;
professional: boolean;
enterpriseStatus: boolean;
identificationTitle: string;
blueVip: boolean;
talentBoard: false[];
userTitle: UserTitle;
enterpriseName: string;
}
export interface SpecialBoardDetail {
id: number;
name: string;
content: string;
categoryId: number;
type: number;
submitted: number;
status: number;
rangeed: number;
userId: number;
followCount: number;
postCount: number;
backgroundColor: string;
pushMessage: number;
bestTime: number;
updateTime: number;
createTime: number;
modifyTime: number;
followStatus: boolean;
tabOption: number;
specialAvatar: string;
pushStatus: boolean;
specialAdmins: SpecialAdmin[];
isContribute: boolean;
post: false[];
isOpenPostEntrance: boolean;
}
interface RecommendPost {
entityId: number;
entityType: number;
dataTime: number;
sortValue: number;
recommendReason: string;
postInfo: Post;
feedType: number;
source: string;
pointMap: Record<string, unknown>;
globalId: string;
}
export interface RecommendListData {
pageNum: number;
pageSize: number;
total: number;
result: RecommendPost[];
empty: boolean;
}
+17 -8
View File
@@ -1,7 +1,8 @@
import CryptoJS from 'crypto-js';
import got from '@/utils/got';
import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';
import * as cheerio from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { PostData } from './types';
const APP_SIGN_KEY = '4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3';
const phoneBaseUrl = 'https://3g.dxy.cn';
@@ -39,8 +40,8 @@ const getPost = (item, tryGet) =>
noncestr: generateNonce(8, 'number'),
};
const { data: post } = await got('https://www.dxy.cn/bbs/newweb/post/detail', {
searchParams: {
const post = await ofetch<PostData>('https://www.dxy.cn/bbs/newweb/post/detail', {
query: {
...postParams,
sign: sign(postParams),
},
@@ -49,16 +50,24 @@ const getPost = (item, tryGet) =>
throw new Error(post.message);
}
const $ = load(post.data.body, null, false);
const $ = cheerio.load(post.data.body, null, false);
$('img').each((_, img) => {
img = $(img);
img.removeAttr('data-osrc');
img.removeAttr('data-hsrc');
if (img.data('hsrc')) {
img.attr('src', img.data('hsrc'));
img.removeAttr('data-hsrc');
}
if (img.data('osrc')) {
img.attr('src', img.data('osrc'));
img.removeAttr('data-osrc');
}
});
item.description = $.html();
item.updated = parseDate(post.data.lastEditTime, 'x');
item.pubDate = parseDate(post.data.createTime, 'x');
item.updated = post.data.lastEditTime ? parseDate(post.data.lastEditTime, 'x') : item.pubDate;
item.category = [...new Set([...item.category, ...post.data.tagInfos.map((tag) => tag.tagName)])];
return item;
});