feat(route/bilibili): implement subtitle caching and rate limiting for video subtitles

- Added a rate limiter for subtitle requests to prevent exceeding API limits.
- Integrated subtitle fetching into the ranking and weekly recommendation routes, allowing for optional subtitle attachments based on configuration.
- Updated item mapping to include subtitles when available.
This commit is contained in:
DIYgod
2025-09-01 21:38:57 +08:00
parent 4523d37cbd
commit eca3b714ea
3 changed files with 57 additions and 23 deletions
+12
View File
@@ -6,6 +6,17 @@ import { config } from '@/config';
import logger from '@/utils/logger';
import { getPuppeteerPage } from '@/utils/puppeteer';
import { JSDOM } from 'jsdom';
import { RateLimiterMemory, RateLimiterQueue } from 'rate-limiter-flexible';
const subtitleLimiter = new RateLimiterMemory({
points: 5,
duration: 1,
execEvenly: true,
});
const subtitleLimiterQueue = new RateLimiterQueue(subtitleLimiter, {
maxQueueSize: 4800,
});
const getCookie = (disableConfig = false) => {
if (Object.keys(config.bilibili.cookies).length > 0 && !disableConfig) {
@@ -246,6 +257,7 @@ const getVideoSubtitle = async (
const cid = await getCidFromId(undefined, 1, bvid);
const cookie = await getCookie();
return cache.tryGet(`bili-video-subtitle-${bvid}`, async () => {
await subtitleLimiterQueue.removeTokens(1);
const response = await got(`https://api.bilibili.com/x/player/wbi/v2?bvid=${bvid}&cid=${cid}`, {
headers: {
Referer: `https://www.bilibili.com/video/${bvid}`,
+23 -17
View File
@@ -2,6 +2,8 @@ import { Route, ViewType } from '@/types';
import got from '@/utils/got';
import utils, { getVideoUrl } from './utils';
import { parseDuration } from '@/utils/helpers';
import { config } from '@/config';
import cache from './cache';
// https://www.bilibili.com/v/popular/rank/all
@@ -212,22 +214,26 @@ async function handler(ctx) {
return {
title: `bilibili 排行榜-${ridChinese}`,
link,
item: list.map((item) => ({
title: item.title,
description: utils.renderUGCDescription(embed, item.pic, item.description || item.title, item.aid, undefined, item.bvid),
pubDate: item.create && new Date(item.create).toUTCString(),
author: item.author,
link: !item.create || (new Date(item.create).getTime() / 1000 > utils.bvidTime && item.bvid) ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
image: item.pic,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
duration_in_seconds: parseDuration(item.duration),
},
]
: undefined,
})),
item: list.map(async (item) => {
const subtitles = !config.bilibili.excludeSubtitles && item.bvid ? await cache.getVideoSubtitleAttachment(item.bvid) : [];
return {
title: item.title,
description: utils.renderUGCDescription(embed, item.pic, item.description || item.title, item.aid, undefined, item.bvid),
pubDate: item.create && new Date(item.create).toUTCString(),
author: item.author,
link: !item.create || (new Date(item.create).getTime() / 1000 > utils.bvidTime && item.bvid) ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.aid}`,
image: item.pic,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
duration_in_seconds: parseDuration(item.duration),
},
...subtitles,
]
: undefined,
};
}),
};
}
+22 -6
View File
@@ -1,6 +1,9 @@
import { Route } from '@/types';
import got from '@/utils/got';
import utils from './utils';
import utils, { getVideoUrl } from './utils';
import { parseDuration } from '@/utils/helpers';
import { config } from '@/config';
import cache from './cache';
export const route: Route = {
path: '/weekly/:embed?',
@@ -46,10 +49,23 @@ async function handler(ctx) {
title: 'B站每周必看',
link: 'https://www.bilibili.com/h5/weekly-recommend',
description: 'B站每周必看',
item: data.map((item) => ({
title: item.title,
description: utils.renderUGCDescription(embed, item.cover, `${weekly_name} ${item.title} - ${item.rcmd_reason}`, item.param, undefined, item.bvid),
link: weekly_number > 60 && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.param}`,
})),
item: data.map(async (item) => {
const subtitles = !config.bilibili.excludeSubtitles && item.bvid ? await cache.getVideoSubtitleAttachment(item.bvid) : [];
return {
title: item.title,
description: utils.renderUGCDescription(embed, item.cover, `${weekly_name} ${item.title} - ${item.rcmd_reason}`, item.param, undefined, item.bvid),
link: weekly_number > 60 && item.bvid ? `https://www.bilibili.com/video/${item.bvid}` : `https://www.bilibili.com/video/av${item.param}`,
attachments: item.bvid
? [
{
url: getVideoUrl(item.bvid),
mime_type: 'text/html',
duration_in_seconds: parseDuration(item.cover_right_text_1),
},
...subtitles,
]
: undefined,
};
}),
};
}