feat(route/bilibili): config to include subtitles or not (#19834)
* feat(route/bilibili): config to include subtitles or not * update * update
This commit is contained in:
@@ -102,6 +102,7 @@ export type Config = {
|
||||
cookies: Record<string, string | undefined>;
|
||||
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,
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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('<br>');
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user