fix(route/bilibili/video): fallback to dynamic api (#19058)

* fix(route/bilibili/video): fallback to dynamic api

* update

* update
This commit is contained in:
Stephen Zhou 2025-05-09 01:19:28 +08:00 committed by GitHub
parent be620d2348
commit e1ae2b0200
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 79 additions and 27 deletions

View File

@ -295,7 +295,11 @@ export const renderOGVDescription = (embed: boolean, img: string, description: s
return rendered;
};
export const getVideoUrl = (bvid?: string) => (bvid ? `https://www.bilibili.com/blackboard/newplayer.html?isOutside=true&autoplay=true&danmaku=true&muted=false&highQuality=true&bvid=${bvid}` : undefined);
export function getVideoUrl(bvid: string): string;
export function getVideoUrl(bvid?: string): string | undefined;
export function getVideoUrl(bvid?: string): string | undefined {
return bvid ? `https://www.bilibili.com/blackboard/newplayer.html?isOutside=true&autoplay=true&danmaku=true&muted=false&highQuality=true&bvid=${bvid}` : undefined;
}
export const getLiveUrl = (roomId?: string) => (roomId ? `https://www.bilibili.com/blackboard/live/live-activity-player.html?cid=${roomId}` : undefined);
export default {

View File

@ -1,8 +1,12 @@
import { Route, ViewType } from '@/types';
import type { Context } from 'hono';
import JSONbig from 'json-bigint';
import { DataItem, Route, ViewType } from '@/types';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import cache from './cache';
import utils, { getVideoUrl } from './utils';
import logger from '@/utils/logger';
import type { BilibiliWebDynamicResponse } from './api-interface';
export const route: Route = {
path: '/user/video/:uid/:embed?',
@ -29,7 +33,7 @@ export const route: Route = {
handler,
};
async function handler(ctx) {
async function handler(ctx: Context) {
const uid = ctx.req.param('uid');
const embed = !ctx.req.param('embed');
const cookie = await cache.getCookie();
@ -50,37 +54,81 @@ async function handler(ctx) {
},
});
const data = response.data;
let fallbackItem: DataItem[] | undefined;
if (data.code) {
logger.error(JSON.stringify(data.data));
throw new Error(`Got error code ${data.code} while fetching: ${data.message}`);
try {
// Fallback to get video from dynamic API
const params = utils.addDmVerifyInfo(`host_mid=${uid}&platform=web&features=itemOpusStyle,listOnlyfans,opusBigCover,onlyfansVote`, utils.getDmImgList());
const response = await got(`https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?${params}`, {
headers: {
Referer: `https://space.bilibili.com/${uid}/`,
Cookie: cookie,
},
});
const body = JSONbig.parse(response.body);
if (body?.code === -352) {
throw new Error('Request failed, please try again.');
}
const items = (body as BilibiliWebDynamicResponse)?.data?.items.filter((item) => item.modules.module_dynamic?.major?.type === 'MAJOR_TYPE_ARCHIVE');
if (items && items.length > 0) {
fallbackItem = items.map((item) => {
const dynamic = item?.modules?.module_dynamic?.major?.archive;
const author = item?.modules?.module_author;
const aid = dynamic?.aid;
const bvid = dynamic?.bvid;
const data: DataItem = {
title: dynamic?.title ?? '',
description: !aid && !bvid ? '' : utils.renderUGCDescription(embed, '', '', aid, undefined, bvid),
pubDate: author?.pub_ts ? parseDate(author.pub_ts, 'X') : undefined,
link: author?.pub_ts && author.pub_ts > utils.bvidTime && bvid ? `https://www.bilibili.com/video/${bvid}` : `https://www.bilibili.com/video/av${aid}`,
author: name ?? undefined,
attachments: bvid
? [
{
url: getVideoUrl(bvid),
mime_type: 'text/html',
},
]
: undefined,
};
return data;
});
}
} catch {
logger.error(JSON.stringify(data.data));
throw new Error(`Got error code ${data.code} while fetching: ${data.message}`);
}
}
return {
title: `${name} 的 bilibili 空间`,
link: `https://space.bilibili.com/${uid}`,
description: `${name} 的 bilibili 空间`,
image: face,
logo: face,
icon: face,
image: face ?? undefined,
logo: face ?? undefined,
icon: face ?? undefined,
item:
data.data &&
data.data.list &&
data.data.list.vlist &&
data.data.list.vlist.map((item) => ({
title: item.title,
description: utils.renderUGCDescription(embed, item.pic, item.description, item.aid, undefined, item.bvid),
pubDate: new Date(item.created * 1000).toUTCString(),
link: item.created > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
author: name,
comments: item.comment,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
},
]
: undefined,
})),
fallbackItem ??
(data.data &&
data.data.list &&
data.data.list.vlist &&
data.data.list.vlist.map((item) => ({
title: item.title,
description: utils.renderUGCDescription(embed, item.pic, item.description, item.aid, undefined, item.bvid),
pubDate: new Date(item.created * 1000).toUTCString(),
link: item.created > utils.bvidTime && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
author: name,
comments: item.comment,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
},
]
: undefined,
}))),
};
}