diff --git a/lib/config.ts b/lib/config.ts index e6f3dfea9..f9f2501fb 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -102,6 +102,7 @@ export type Config = { cookies: Record; dmImgList?: string; dmImgInter?: string; + excludeSubtitles?: boolean; }; bitbucket: { username?: string; @@ -573,6 +574,7 @@ const calculateValue = () => { cookies: bilibili_cookies, dmImgList: envs.BILIBILI_DM_IMG_LIST, dmImgInter: envs.BILIBILI_DM_IMG_INTER, + excludeSubtitles: toBoolean(envs.BILIBILI_EXCLUDE_SUBTITLES, false), }, bitbucket: { username: envs.BITBUCKET_USERNAME, diff --git a/lib/routes/bilibili/cache.ts b/lib/routes/bilibili/cache.ts index b69dc73d0..810063880 100644 --- a/lib/routes/bilibili/cache.ts +++ b/lib/routes/bilibili/cache.ts @@ -271,6 +271,15 @@ const getVideoSubtitle = async ( }); }; +const getVideoSubtitleAttachment = async (bvid: string) => { + const subtitles = await getVideoSubtitle(bvid); + return subtitles.map((subtitle) => ({ + url: `data:text/plain;charset=utf-8,${encodeURIComponent(subtitle.content)}`, + mime_type: 'text/srt', + title: `字幕 - ${subtitle.lan_doc}`, + })); +}; + const getAidFromBvid = async (bvid) => { const key = `bili-cid-from-bvid-${bvid}`; let aid = await cache.get(key); @@ -353,4 +362,5 @@ export default { getArticleDataFromCvid, getRenderData, getVideoSubtitle, + getVideoSubtitleAttachment, }; diff --git a/lib/routes/bilibili/dynamic.ts b/lib/routes/bilibili/dynamic.ts index b4f9b3519..a5bb1df4c 100644 --- a/lib/routes/bilibili/dynamic.ts +++ b/lib/routes/bilibili/dynamic.ts @@ -8,6 +8,7 @@ import { fallback, queryToBoolean } from '@/utils/readable-social'; import cacheIn from './cache'; import { BilibiliWebDynamicResponse, Item2, Modules } from './api-interface'; import { parseDuration } from '@/utils/helpers'; +import { config } from '@/config'; export const route: Route = { path: '/user/dynamic/:uid/:routeParams?', @@ -289,6 +290,7 @@ async function handler(ctx) { const data = item.modules; const origin = item?.orig?.modules; + const bvid = data?.module_dynamic?.major?.archive?.bvid; // link let link = ''; @@ -388,6 +390,8 @@ async function handler(ctx) { .filter(Boolean) .join('
'); + const subtitles = !config.bilibili.excludeSubtitles && bvid ? await cacheIn.getVideoSubtitleAttachment(bvid) : []; + return { title: title || originalDescription, description: descriptions, @@ -403,6 +407,7 @@ async function handler(ctx) { mime_type: 'text/html', duration_in_seconds: data.module_dynamic?.major?.archive?.duration_text ? parseDuration(data.module_dynamic.major.archive.duration_text) : undefined, }, + ...subtitles, ] : undefined, }; diff --git a/lib/routes/bilibili/video.ts b/lib/routes/bilibili/video.ts index edb71460c..91eccd8c0 100644 --- a/lib/routes/bilibili/video.ts +++ b/lib/routes/bilibili/video.ts @@ -4,6 +4,7 @@ import logger from '@/utils/logger'; import type { Context } from 'hono'; import cache from './cache'; import utils, { getVideoUrl } from './utils'; +import { config } from '@/config'; export const route: Route = { path: '/user/video/:uid/:embed?', @@ -69,7 +70,7 @@ async function handler(ctx: Context) { data.data.list.vlist && (await Promise.all( data.data.list.vlist.map(async (item) => { - const subtitles = await cache.getVideoSubtitle(item.bvid); + 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.aid, undefined, item.bvid), @@ -83,11 +84,7 @@ async function handler(ctx: Context) { url: getVideoUrl(item.bvid), mime_type: 'text/html', }, - ...subtitles.map((subtitle) => ({ - url: `data:text/plain;charset=utf-8,${encodeURIComponent(subtitle.content)}`, - mime_type: 'text/srt', - title: `字幕 - ${subtitle.lan_doc}`, - })), + ...subtitles, ] : undefined, };