From 1135e20c3db76204994135da04f942ab48a7cb26 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Wed, 6 Apr 2022 15:05:18 +0100 Subject: [PATCH] Add support for Piped playlists in RSS. --- .../me/kavin/piped/utils/ResponseHelper.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index fab5d2c..4463825 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -373,7 +373,58 @@ public class ResponseHelper { } - public static byte[] playlistRSSResponse(String playlistId) + public static byte[] playlistRSSResponse(String playlistId) throws ExtractionException, IOException, FeedException { + + if (StringUtils.isBlank(playlistId)) + return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse()); + + if (playlistId.matches("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")) + return playlistPipedRSSResponse(playlistId); + + return playlistYouTubeRSSResponse(playlistId); + } + + private static byte[] playlistPipedRSSResponse(String playlistId) + throws FeedException { + + try (Session s = DatabaseSessionFactory.createSession()) { + var cb = s.getCriteriaBuilder(); + var cq = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class); + var root = cq.from(me.kavin.piped.utils.obj.db.Playlist.class); + root.fetch("videos") + .fetch("channel", JoinType.LEFT); + root.fetch("owner", JoinType.LEFT); + cq.select(root); + cq.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId))); + var query = s.createQuery(cq); + var pl = query.uniqueResult(); + + final List entries = new ObjectArrayList<>(); + + SyndFeed feed = new SyndFeedImpl(); + feed.setFeedType("rss_2.0"); + feed.setTitle(pl.getName()); + feed.setAuthor(pl.getOwner().getUsername()); + feed.setDescription(String.format("%s - Piped", pl.getName())); + feed.setLink(Constants.FRONTEND_URL + "/playlist?list=" + pl.getPlaylistId()); + feed.setPublishedDate(new Date()); + + for (var video : pl.getVideos()) { + SyndEntry entry = new SyndEntryImpl(); + entry.setAuthor(video.getChannel().getUploader()); + entry.setLink(Constants.FRONTEND_URL + "/video?id=" + video.getId()); + entry.setUri(Constants.FRONTEND_URL + "/video?id=" + video.getId()); + entry.setTitle(video.getTitle()); + entries.add(entry); + } + + feed.setEntries(entries); + + return new SyndFeedOutput().outputString(feed).getBytes(UTF_8); + } + } + + private static byte[] playlistYouTubeRSSResponse(String playlistId) throws IOException, ExtractionException, FeedException { final PlaylistInfo info = PlaylistInfo.getInfo("https://www.youtube.com/playlist?list=" + playlistId);