Skip to content

Commit

Permalink
Merge pull request #782 from Bnyro/master
Browse files Browse the repository at this point in the history
feat(rss feed): add optional query parameter 'filter' ('shorts' or 'videos')
  • Loading branch information
Bnyro committed Mar 15, 2024
2 parents 55040de + 30e6bf2 commit 77e8d3c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand All @@ -131,6 +132,7 @@ public static byte[] feedResponseRSS(String session) throws FeedException {
SyndFeed feed = FeedHelpers.createRssFeed(user.getUsername());

final List<SyndEntry> entries = FeedHelpers.generateAuthenticatedFeed(s, user.getId(), 100)
.filter(FeedHelpers.createFeedFilter(filter))
.map(video -> {
var channel = video.getChannel();
return ChannelHelpers.createEntry(video, channel);
Expand Down Expand Up @@ -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<String> filteredChannels = Arrays.stream(channelIds)
.filter(ChannelHelpers::isValidId)
Expand All @@ -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<Video> videos = FeedHelpers.generateUnauthenticatedFeed(s, filteredChannels, 100).toList();
List<Video> videos = FeedHelpers.generateUnauthenticatedFeed(s, filteredChannels, 100)
.filter(FeedHelpers.createFeedFilter(filter))
.toList();

List<SyndEntry> entries = videos.stream()
.map(video -> ChannelHelpers.createEntry(video, video.getChannel()))
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/me/kavin/piped/utils/FeedHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Comparator;
import java.util.Date;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

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

public static Predicate<Video> createFeedFilter(@Nullable String filter) {
return video -> switch (filter) {
case "shorts" -> video.isShort();
case "videos" -> !video.isShort();
case null, default -> true;
};
}

public static Stream<SubscriptionChannel> generateSubscriptionsList(Stream<Channel> channels) {
return channels.parallel()
.filter(channel -> channel.getUploader() != null)
Expand Down

0 comments on commit 77e8d3c

Please sign in to comment.