From 30e6bf2a7d014f7dd9d83c8d708cbd31ea4fa71d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 15 Mar 2024 14:49:02 +0100 Subject: [PATCH] feat(rss feed): add optional query parameter 'filter' ('shorts' or 'videos') --- .../java/me/kavin/piped/server/ServerLauncher.java | 6 ++++-- .../kavin/piped/server/handlers/auth/FeedHandlers.java | 10 +++++++--- src/main/java/me/kavin/piped/utils/FeedHelpers.java | 9 +++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index 8af90285..6207f7c5 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -315,7 +315,8 @@ AsyncServlet mainServlet(Executor executor) { } })).map(GET, "/feed/rss", AsyncServlet.ofBlocking(executor, request -> { try { - return getRawResponse(FeedHandlers.feedResponseRSS(request.getQueryParameter("authToken")), + return getRawResponse(FeedHandlers.feedResponseRSS(request.getQueryParameter("authToken"), + request.getQueryParameter("filter")), "application/atom+xml", "public, s-maxage=120"); } catch (Exception e) { return getErrorResponse(e, request.getPath()); @@ -339,7 +340,8 @@ AsyncServlet mainServlet(Executor executor) { })).map(GET, "/feed/unauthenticated/rss", AsyncServlet.ofBlocking(executor, request -> { try { return getRawResponse(FeedHandlers.unauthenticatedFeedResponseRSS( - getArray(request.getQueryParameter("channels")) + getArray(request.getQueryParameter("channels")), + request.getQueryParameter("filter") ), "application/atom+xml", "public, s-maxage=120"); } catch (Exception e) { return getErrorResponse(e, request.getPath()); diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java index 136e0c1d..34f1977e 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java @@ -21,6 +21,7 @@ import org.hibernate.StatelessSession; import org.schabi.newpipe.extractor.channel.ChannelInfo; +import javax.annotation.Nullable; import java.io.IOException; import java.util.*; import java.util.concurrent.TimeUnit; @@ -119,7 +120,7 @@ public static byte[] feedResponse(String session) throws IOException { return null; } - public static byte[] feedResponseRSS(String session) throws FeedException { + public static byte[] feedResponseRSS(String session, @Nullable String filter) throws FeedException { if (StringUtils.isBlank(session)) ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter")); @@ -131,6 +132,7 @@ public static byte[] feedResponseRSS(String session) throws FeedException { SyndFeed feed = FeedHelpers.createRssFeed(user.getUsername()); final List entries = FeedHelpers.generateAuthenticatedFeed(s, user.getId(), 100) + .filter(FeedHelpers.createFeedFilter(filter)) .map(video -> { var channel = video.getChannel(); return ChannelHelpers.createEntry(video, channel); @@ -173,7 +175,7 @@ public static byte[] unauthenticatedFeedResponse(String[] channelIds) throws Exc } } - public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws Exception { + public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds, @Nullable String filter) throws Exception { Set filteredChannels = Arrays.stream(channelIds) .filter(ChannelHelpers::isValidId) @@ -183,7 +185,9 @@ public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("No valid channel IDs provided")); try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { - List