feat(route/youtube): only get subtitle for jsonfeed (#20375)

This commit is contained in:
Tony 2025-10-26 22:56:09 +08:00 committed by GitHub
parent e1fe935494
commit 1cc197a104
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 9 deletions

View File

@ -54,7 +54,7 @@ if (config.youtube && config.youtube.clientId && config.youtube.clientSecret &&
export { youtubeOAuth2Client, exec };
export const getDataByUsername = async ({ username, embed, filterShorts }: { username: string; embed: boolean; filterShorts: boolean }): Promise<Data> => {
export const getDataByUsername = async ({ username, embed, filterShorts, isJsonFeed }: { username: string; embed: boolean; filterShorts: boolean; isJsonFeed: boolean }): Promise<Data> => {
let userHandleData;
if (username.startsWith('@')) {
userHandleData = await cache.tryGet(`youtube:handle:${username}`, async () => {
@ -109,7 +109,7 @@ export const getDataByUsername = async ({ username, embed, filterShorts }: { use
}
const videoIds = playlistItems.data.items.map((item) => item.snippet.resourceId.videoId);
const videoDetails = await utils.getVideos(videoIds.join(','), 'contentDetails', cache);
const subtitlesMap = await getSrtAttachmentBatch(videoIds);
const subtitlesMap = isJsonFeed ? await getSrtAttachmentBatch(videoIds) : {};
return {
title: `${userHandleData?.channelName || username} - YouTube`,
@ -123,7 +123,7 @@ export const getDataByUsername = async ({ username, embed, filterShorts }: { use
const videoId = snippet.resourceId.videoId;
const img = utils.getThumbnail(snippet.thumbnails);
const detail = videoDetails?.data.items.find((d) => d.id === videoId);
const srtAttachments = subtitlesMap[videoId] || [];
const srtAttachments = subtitlesMap ? subtitlesMap[videoId] || [] : [];
return {
title: snippet.title,

View File

@ -14,16 +14,16 @@ export const getChannelIdByUsername = (username: string) =>
return navigationEndpoint.payload.browseId;
});
export const getDataByUsername = async ({ username, embed, filterShorts }: { username: string; embed: boolean; filterShorts: boolean }): Promise<Data> => {
export const getDataByUsername = async ({ username, embed, filterShorts, isJsonFeed }: { username: string; embed: boolean; filterShorts: boolean; isJsonFeed: boolean }): Promise<Data> => {
const channelId = (await getChannelIdByUsername(username)) as string;
return getDataByChannelId({ channelId, embed, filterShorts });
return getDataByChannelId({ channelId, embed, filterShorts, isJsonFeed });
};
export const getDataByChannelId = async ({ channelId, embed }: { channelId: string; embed: boolean; filterShorts: boolean }): Promise<Data> => {
export const getDataByChannelId = async ({ channelId, embed, isJsonFeed }: { channelId: string; embed: boolean; filterShorts: boolean; isJsonFeed: boolean }): Promise<Data> => {
const innertube = await innertubePromise;
const channel = await innertube.getChannel(channelId);
const videos = await channel.getVideos();
const videoSubtitles = await getSrtAttachmentBatch(videos.videos.filter((video) => 'video_id' in video).map((video) => video.video_id));
const videoSubtitles = isJsonFeed ? await getSrtAttachmentBatch(videos.videos.filter((video) => 'video_id' in video).map((video) => video.video_id)) : {};
return {
title: `${channel.metadata.title || channelId} - YouTube`,
@ -35,7 +35,7 @@ export const getDataByChannelId = async ({ channelId, embed }: { channelId: stri
videos.videos
.filter((video) => 'video_id' in video)
.map((video) => {
const srtAttachments = videoSubtitles[video.video_id] || [];
const srtAttachments = isJsonFeed ? videoSubtitles[video.video_id] || [] : [];
const img = 'best_thumbnail' in video ? video.best_thumbnail?.url : 'thumbnails' in video ? video.thumbnails?.[0]?.url : undefined;
return {

View File

@ -57,10 +57,12 @@ async function handler(ctx) {
const filterShortsStr = params.get('filterShorts');
const filterShorts = filterShortsStr === null || filterShortsStr === '' || filterShortsStr === 'true';
const isJsonFeed = ctx.req.query('format') === 'json';
const data = await callApi({
googleApi: getDataByUsernameGoogle,
youtubeiApi: getDataByUsernameYoutubei,
params: { username, embed, filterShorts },
params: { username, embed, filterShorts, isJsonFeed },
});
return data;