Skip to content

Commit 30e6bf2

Browse files
committed
feat(rss feed): add optional query parameter 'filter' ('shorts' or 'videos')
1 parent 55040de commit 30e6bf2

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/main/java/me/kavin/piped/server/ServerLauncher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ AsyncServlet mainServlet(Executor executor) {
315315
}
316316
})).map(GET, "/feed/rss", AsyncServlet.ofBlocking(executor, request -> {
317317
try {
318-
return getRawResponse(FeedHandlers.feedResponseRSS(request.getQueryParameter("authToken")),
318+
return getRawResponse(FeedHandlers.feedResponseRSS(request.getQueryParameter("authToken"),
319+
request.getQueryParameter("filter")),
319320
"application/atom+xml", "public, s-maxage=120");
320321
} catch (Exception e) {
321322
return getErrorResponse(e, request.getPath());
@@ -339,7 +340,8 @@ AsyncServlet mainServlet(Executor executor) {
339340
})).map(GET, "/feed/unauthenticated/rss", AsyncServlet.ofBlocking(executor, request -> {
340341
try {
341342
return getRawResponse(FeedHandlers.unauthenticatedFeedResponseRSS(
342-
getArray(request.getQueryParameter("channels"))
343+
getArray(request.getQueryParameter("channels")),
344+
request.getQueryParameter("filter")
343345
), "application/atom+xml", "public, s-maxage=120");
344346
} catch (Exception e) {
345347
return getErrorResponse(e, request.getPath());

src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.hibernate.StatelessSession;
2222
import org.schabi.newpipe.extractor.channel.ChannelInfo;
2323

24+
import javax.annotation.Nullable;
2425
import java.io.IOException;
2526
import java.util.*;
2627
import java.util.concurrent.TimeUnit;
@@ -119,7 +120,7 @@ public static byte[] feedResponse(String session) throws IOException {
119120
return null;
120121
}
121122

122-
public static byte[] feedResponseRSS(String session) throws FeedException {
123+
public static byte[] feedResponseRSS(String session, @Nullable String filter) throws FeedException {
123124

124125
if (StringUtils.isBlank(session))
125126
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter"));
@@ -131,6 +132,7 @@ public static byte[] feedResponseRSS(String session) throws FeedException {
131132
SyndFeed feed = FeedHelpers.createRssFeed(user.getUsername());
132133

133134
final List<SyndEntry> entries = FeedHelpers.generateAuthenticatedFeed(s, user.getId(), 100)
135+
.filter(FeedHelpers.createFeedFilter(filter))
134136
.map(video -> {
135137
var channel = video.getChannel();
136138
return ChannelHelpers.createEntry(video, channel);
@@ -173,7 +175,7 @@ public static byte[] unauthenticatedFeedResponse(String[] channelIds) throws Exc
173175
}
174176
}
175177

176-
public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws Exception {
178+
public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds, @Nullable String filter) throws Exception {
177179

178180
Set<String> filteredChannels = Arrays.stream(channelIds)
179181
.filter(ChannelHelpers::isValidId)
@@ -183,7 +185,9 @@ public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws
183185
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("No valid channel IDs provided"));
184186

185187
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
186-
List<Video> videos = FeedHelpers.generateUnauthenticatedFeed(s, filteredChannels, 100).toList();
188+
List<Video> videos = FeedHelpers.generateUnauthenticatedFeed(s, filteredChannels, 100)
189+
.filter(FeedHelpers.createFeedFilter(filter))
190+
.toList();
187191

188192
List<SyndEntry> entries = videos.stream()
189193
.map(video -> ChannelHelpers.createEntry(video, video.getChannel()))

src/main/java/me/kavin/piped/utils/FeedHelpers.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Comparator;
1717
import java.util.Date;
1818
import java.util.Set;
19+
import java.util.function.Predicate;
1920
import java.util.stream.Stream;
2021

2122
import static me.kavin.piped.utils.URLUtils.rewriteURL;
@@ -79,6 +80,14 @@ public static SyndFeed createRssFeed(@Nullable String username) {
7980
return feed;
8081
}
8182

83+
public static Predicate<Video> createFeedFilter(@Nullable String filter) {
84+
return video -> switch (filter) {
85+
case "shorts" -> video.isShort();
86+
case "videos" -> !video.isShort();
87+
case null, default -> true;
88+
};
89+
}
90+
8291
public static Stream<SubscriptionChannel> generateSubscriptionsList(Stream<Channel> channels) {
8392
return channels.parallel()
8493
.filter(channel -> channel.getUploader() != null)

0 commit comments

Comments
 (0)