feat(route/youtube): filter shorts in channel with handle route

This commit is contained in:
pseudoyu
2025-04-14 16:21:35 +08:00
parent 636f8ffff3
commit b0ab27df92
3 changed files with 60 additions and 10 deletions
+5 -1
View File
@@ -69,7 +69,11 @@ async function handler(ctx) {
throw new InvalidParameterError(`Invalid YouTube channel ID. \nYou may want to use <code>/youtube/user/:id</code> 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;
+38 -9
View File
@@ -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 <a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>');
}
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) {
+17
View File
@@ -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;