diff --git a/lib/routes/youtube/api/google.ts b/lib/routes/youtube/api/google.ts index 453b1b267..32fbcf3be 100644 --- a/lib/routes/youtube/api/google.ts +++ b/lib/routes/youtube/api/google.ts @@ -162,3 +162,28 @@ export const getDataByChannelId = async ({ channelId, embed, filterShorts }: { c }), }; }; + +export const getDataByPlaylistId = async ({ playlistId, embed }: { playlistId: string; embed: boolean }): Promise => { + 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, + }; + }), + }; +}; diff --git a/lib/routes/youtube/api/youtubei.ts b/lib/routes/youtube/api/youtubei.ts index 27dc74a43..e0777e6d0 100644 --- a/lib/routes/youtube/api/youtubei.ts +++ b/lib/routes/youtube/api/youtubei.ts @@ -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 => { + 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, }, ], }; diff --git a/lib/routes/youtube/playlist.ts b/lib/routes/youtube/playlist.ts index de9ee2a0a..a981c7512 100644 --- a/lib/routes/youtube/playlist.ts +++ b/lib/routes/youtube/playlist.ts @@ -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 relevant config'); - } 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; }