diff --git a/lib/routes/youtube/channel.ts b/lib/routes/youtube/channel.ts index 4c04cb81d..386f396e1 100644 --- a/lib/routes/youtube/channel.ts +++ b/lib/routes/youtube/channel.ts @@ -69,7 +69,11 @@ async function handler(ctx) { throw new InvalidParameterError(`Invalid YouTube channel ID. \nYou may want to use /youtube/user/:id instead.`); } - const playlistId = filterShorts ? 'UULF' + id.slice(2) : (await utils.getChannelWithId(id, 'contentDetails', cache)).data.items[0].contentDetails.relatedPlaylists.uploads; // Default uploads playlist + // Get original uploads playlist ID if needed + const originalPlaylistId = filterShorts ? null : (await utils.getChannelWithId(id, 'contentDetails', cache)).data.items[0].contentDetails.relatedPlaylists.uploads; + + // Use the utility function to get the appropriate playlist ID based on filterShorts setting + const playlistId = filterShorts ? utils.getPlaylistWithShortsFilter(id) : originalPlaylistId; const data = (await utils.getPlaylistItems(playlistId, 'snippet', cache)).data.items; diff --git a/lib/routes/youtube/user.ts b/lib/routes/youtube/user.ts index 5d0fe5a11..a9efe79b7 100644 --- a/lib/routes/youtube/user.ts +++ b/lib/routes/youtube/user.ts @@ -9,11 +9,20 @@ import ConfigNotFoundError from '@/errors/types/config-not-found'; import NotFoundError from '@/errors/types/not-found'; export const route: Route = { - path: '/user/:username/:embed?', + path: '/user/:username/:routeParams?', categories: ['social-media', 'popular'], view: ViewType.Videos, example: '/youtube/user/@JFlaMusic', - parameters: { username: 'YouTuber handle with @', embed: 'Default to embed the video, set to any value to disable embedding' }, + parameters: { + username: 'YouTuber handle with @', + routeParams: 'Extra parameters, see the table below', + }, + description: `:::tip Parameter +| Name | Description | Default | +| ---------- | ----------------------------------------------------------------------------------- | ------- | +| embed | Whether to embed the video, fill in any value to disable embedding | embed | +| filterShorts | Whether to filter out shorts from the feed, fill in any falsy value to show shorts | true | +:::`, features: { requireConfig: [ { @@ -34,7 +43,7 @@ export const route: Route = { }, ], name: 'Channel with user handle', - maintainers: ['DIYgod'], + maintainers: ['DIYgod', 'pseudoyu'], handler, }; @@ -43,7 +52,17 @@ async function handler(ctx) { throw new ConfigNotFoundError('YouTube RSS is disabled due to the lack of relevant config'); } const username = ctx.req.param('username'); - const embed = !ctx.req.param('embed'); + + // Parse route parameters + const routeParams = ctx.req.param('routeParams'); + const params = new URLSearchParams(routeParams); + + // Get embed parameter + const embed = !params.get('embed'); + + // Get filterShorts parameter (default to true if not specified) + const filterShortsStr = params.get('filterShorts'); + const filterShorts = filterShortsStr === null || filterShortsStr === '' || filterShortsStr === 'true'; let userHandleData; if (username.startsWith('@')) { @@ -72,16 +91,26 @@ async function handler(ctx) { }; }); } - const playlistId = - userHandleData?.playlistId || - (await (async () => { + + // Get the appropriate playlist ID based on filterShorts setting + const playlistId = await (async () => { + if (userHandleData?.playlistId) { + const origPlaylistId = userHandleData.playlistId; + + return utils.getPlaylistWithShortsFilter(origPlaylistId, filterShorts); + } else { const channelData = await utils.getChannelWithUsername(username, 'contentDetails', cache); const items = channelData.data.items; + if (!items) { throw new NotFoundError(`The channel https://www.youtube.com/user/${username} does not exist.`); } - return items[0].contentDetails.relatedPlaylists.uploads; - })()); + + const channelId = items[0].id; + + return filterShorts ? utils.getPlaylistWithShortsFilter(channelId, filterShorts) : items[0].contentDetails.relatedPlaylists.uploads; + } + })(); const playlistItems = await utils.getPlaylistItems(playlistId, 'snippet', cache); if (!playlistItems) { diff --git a/lib/routes/youtube/utils.ts b/lib/routes/youtube/utils.ts index 0b468409a..d04c2af12 100644 --- a/lib/routes/youtube/utils.ts +++ b/lib/routes/youtube/utils.ts @@ -154,6 +154,22 @@ export const getLive = (id, cache) => }); export const getVideoUrl = (id: string) => `https://www.youtube-nocookie.com/embed/${id}?controls=1&autoplay=1&mute=0`; +// Get the appropriate playlist ID with or without shorts +export const getPlaylistWithShortsFilter = (id: string, filterShorts = true): string => { + // If filtering shorts is enabled + if (filterShorts) { + if (id.startsWith('UC')) { + // For channel IDs (UC...), convert to playlist format without shorts (UULF...) + return 'UULF' + id.slice(2); + } else if (id.startsWith('UU')) { + // For playlist IDs (UU...), convert to playlist format without shorts (UULF...) + return 'UULF' + id.slice(2); + } + } + // If filterShorts is false or the ID format doesn't match known patterns, return original ID + return id; +}; + const youtubeUtils = { getPlaylistItems, getPlaylist, @@ -167,5 +183,6 @@ const youtubeUtils = { isYouTubeChannelId, getLive, getVideoUrl, + getPlaylistWithShortsFilter, }; export default youtubeUtils;