From 5a8f5ec8d50aa3d94fd8fb9202472483dcab0a81 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 5 Jul 2021 00:27:18 +0530 Subject: [PATCH] Add Playlist RSS (#80) * WIP Playlist rss. * Complete adding support. --- .../java/me/kavin/piped/ServerLauncher.java | 7 ++++ .../me/kavin/piped/utils/ResponseHelper.java | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 1ebfbc4..19cb45f 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -122,6 +122,13 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e); } + })).map("/rss/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> { + try { + return getJsonResponse(ResponseHelper.playlistRSSResponse(request.getPathParameter("playlistId")), + "public, s-maxage=600"); + } catch (Exception e) { + return getErrorResponse(e); + } })).map("/suggestions", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(ResponseHelper.suggestionsResponse(request.getQueryParameter("query")), diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 55e6c80..4aff0d7 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -7,6 +7,7 @@ import java.net.URL; import java.net.http.HttpRequest; import java.net.http.HttpRequest.BodyPublishers; import java.net.http.HttpResponse.BodyHandlers; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -36,6 +37,12 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem; import com.fasterxml.jackson.core.JsonProcessingException; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; +import com.rometools.rome.feed.synd.SyndEntry; +import com.rometools.rome.feed.synd.SyndEntryImpl; +import com.rometools.rome.feed.synd.SyndFeed; +import com.rometools.rome.feed.synd.SyndFeedImpl; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.SyndFeedOutput; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import me.kavin.piped.consts.Constants; @@ -284,6 +291,35 @@ public class ResponseHelper { } + public static final byte[] playlistRSSResponse(String playlistId) + throws IOException, ExtractionException, InterruptedException, FeedException { + + final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId); + + final List entries = new ObjectArrayList<>(); + + SyndFeed feed = new SyndFeedImpl(); + feed.setFeedType("rss_2.0"); + feed.setTitle(info.getName()); + feed.setAuthor(info.getUploaderName()); + feed.setLink(info.getUrl()); + feed.setDescription(String.format("%s - Piped", info.getName())); + + info.getRelatedItems().forEach(o -> { + StreamInfoItem item = o; + SyndEntry entry = new SyndEntryImpl(); + entry.setAuthor(item.getUploaderName()); + entry.setUri(item.getUrl()); + entry.setTitle(item.getName()); + entries.add(entry); + }); + + feed.setEntries(entries); + + return new SyndFeedOutput().outputString(feed).getBytes(StandardCharsets.UTF_8); + + } + public static final byte[] suggestionsResponse(String query) throws JsonProcessingException, IOException, ExtractionException {