feat: use youtubei api as fallback for youtube playlist

This commit is contained in:
DIYgod
2025-05-18 10:51:01 +08:00
parent 05bcf01bed
commit c6e0632580
3 changed files with 80 additions and 30 deletions
+25
View File
@@ -162,3 +162,28 @@ export const getDataByChannelId = async ({ channelId, embed, filterShorts }: { c
}),
};
};
export const getDataByPlaylistId = async ({ playlistId, embed }: { playlistId: string; embed: boolean }): Promise<Data> => {
const playlistTitle = (await utils.getPlaylist(playlistId, 'snippet', cache)).data.items[0].snippet.title;
const data = (await utils.getPlaylistItems(playlistId, 'snippet', cache)).data.items.filter((d) => d.snippet.title !== 'Private video' && d.snippet.title !== 'Deleted video');
return {
title: `${playlistTitle} by ${data[0].snippet.channelTitle} - YouTube`,
link: `https://www.youtube.com/playlist?list=${playlistId}`,
description: `${playlistTitle} by ${data[0].snippet.channelTitle}`,
item: data.map((item) => {
const snippet = item.snippet;
const videoId = snippet.resourceId.videoId;
const img = utils.getThumbnail(snippet.thumbnails);
return {
title: snippet.title,
description: utils.renderDescription(embed, videoId, img, utils.formatDescription(snippet.description)),
pubDate: parseDate(snippet.publishedAt),
link: `https://www.youtube.com/watch?v=${videoId}`,
author: snippet.videoOwnerChannelTitle,
image: img.url,
};
}),
};
};
+45 -1
View File
@@ -31,7 +31,7 @@ export const getDataByChannelId = async ({ channelId, embed }: { channelId: stri
item: videos.videos
.filter((video) => 'video_id' in video)
.map((video) => {
const img = 'thumbnail' in video ? video.thumbnail?.[0].url : undefined;
const img = 'best_thumbnail' in video ? video.best_thumbnail?.url : ('thumbnails' in video ? video.thumbnails?.[0]?.url : undefined);
return {
title: video.title.text || `YouTube Video ${video.video_id}`,
@@ -43,6 +43,50 @@ export const getDataByChannelId = async ({ channelId, embed }: { channelId: stri
{
url: getVideoUrl(video.video_id),
mime_type: 'text/html',
duration_in_seconds: video.duration && 'seconds' in video.duration ? video.duration.seconds : undefined,
},
],
};
}),
};
};
export const getDataByPlaylistId = async ({ playlistId, embed }: { playlistId: string; embed: boolean }): Promise<Data> => {
const innertube = await innertubePromise;
const playlist = await innertube.getPlaylist(playlistId);
const videos = await playlist.videos;
return {
title: `${playlist.info.title || playlistId} by ${playlist.info.author.name} - YouTube`,
link: `https://www.youtube.com/playlist?list=${playlistId}`,
image: playlist.info.thumbnails?.[0].url,
description: playlist.info.description || `${playlist.info.title} by ${playlist.info.author.name}`,
item: videos
.filter((video) => 'id' in video)
.map((video) => {
const img = 'best_thumbnail' in video ? video.best_thumbnail?.url : video.thumbnails?.[0]?.url;
return {
title: video.title.text || `YouTube Video ${video.id}`,
description: utils.renderDescription(embed, video.id, img, ''),
link: `https://www.youtube.com/watch?v=${video.id}`,
author:
'author' in video
? [
{
name: video.author.name,
url: video.author.url,
avatar: video.author.thumbnails?.[0]?.url,
},
]
: undefined,
image: img,
attachments: [
{
url: getVideoUrl(video.id),
mime_type: 'text/html',
duration_in_seconds: 'duration' in video && video.duration && 'seconds' in video.duration ? video.duration.seconds : undefined,
},
],
};
+10 -29
View File
@@ -1,9 +1,7 @@
import { Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import utils from './utils';
import { config } from '@/config';
import { parseDate } from '@/utils/parse-date';
import ConfigNotFoundError from '@/errors/types/config-not-found';
import { callApi } from './utils';
import { getDataByPlaylistId as getDataByPlaylistIdYoutubei } from './api/youtubei';
import { getDataByPlaylistId as getDataByPlaylistIdGoogle } from './api/google';
export const route: Route = {
path: '/playlist/:id/:embed?',
@@ -16,6 +14,7 @@ export const route: Route = {
{
name: 'YOUTUBE_KEY',
description: ' YouTube API Key, support multiple keys, split them with `,`, [API Key application](https://console.developers.google.com/)',
optional: true,
},
],
requirePuppeteer: false,
@@ -30,32 +29,14 @@ export const route: Route = {
};
async function handler(ctx) {
if (!config.youtube || !config.youtube.key) {
throw new ConfigNotFoundError('YouTube RSS is disabled due to the lack of <a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>');
}
const id = ctx.req.param('id');
const embed = !ctx.req.param('embed');
const playlistTitle = (await utils.getPlaylist(id, 'snippet', cache)).data.items[0].snippet.title;
const data = await callApi({
googleApi: getDataByPlaylistIdGoogle,
youtubeiApi: getDataByPlaylistIdYoutubei,
params: { playlistId: id, embed },
});
const data = (await utils.getPlaylistItems(id, 'snippet', cache)).data.items.filter((d) => d.snippet.title !== 'Private video' && d.snippet.title !== 'Deleted video');
return {
title: `${playlistTitle} by ${data[0].snippet.channelTitle} - YouTube`,
link: `https://www.youtube.com/playlist?list=${id}`,
description: `YouTube playlist ${playlistTitle} by ${data[0].snippet.channelTitle}`,
item: data.map((item) => {
const snippet = item.snippet;
const videoId = snippet.resourceId.videoId;
const img = utils.getThumbnail(snippet.thumbnails);
return {
title: snippet.title,
description: utils.renderDescription(embed, videoId, img, utils.formatDescription(snippet.description)),
pubDate: parseDate(snippet.publishedAt),
link: `https://www.youtube.com/watch?v=${videoId}`,
author: snippet.videoOwnerChannelTitle,
image: img.url,
};
}),
};
return data;
}