From 7b9e0ac4322b355a69d9e87d3d94a6a95c1eba69 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 19 Jan 2024 14:23:04 +0100 Subject: [PATCH 1/2] feat: add channel info to rss feed if no videos found --- .../server/handlers/auth/FeedHandlers.java | 33 +++++++++++-------- .../me/kavin/piped/utils/ChannelHelpers.java | 10 ++++++ 2 files changed, 30 insertions(+), 13 deletions(-) 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 f3884a6..ff169c5 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 @@ -22,6 +22,7 @@ import me.kavin.piped.utils.resp.SubscribeStatusResponse; import org.apache.commons.lang3.StringUtils; import org.hibernate.Session; import org.hibernate.StatelessSession; +import org.schabi.newpipe.extractor.channel.ChannelInfo; import java.io.IOException; import java.util.*; @@ -236,11 +237,11 @@ public class FeedHandlers { public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws Exception { - Set filtered = Arrays.stream(channelIds) + Set filteredChannels = Arrays.stream(channelIds) .filter(ChannelHelpers::isValidId) .collect(Collectors.toUnmodifiableSet()); - if (filtered.isEmpty()) + if (filteredChannels.isEmpty()) ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("No valid channel IDs provided")); try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { @@ -254,7 +255,7 @@ public class FeedHandlers { criteria.select(root) .where(cb.and( - root.get("channel").get("id").in(filtered) + root.get("channel").get("id").in(filteredChannels) )) .orderBy(cb.desc(root.get("uploaded"))); @@ -276,22 +277,28 @@ public class FeedHandlers { var channel = video.getChannel(); SyndEntry entry = ChannelHelpers.createEntry(video, channel); entries.add(entry); + } - if (filtered.size() == 1) { - feed.setTitle("Piped - " + channel.getUploader()); - SyndImage channelIcon = new SyndImageImpl(); - channelIcon.setLink(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId()); - channelIcon.setTitle(channel.getUploader()); - channelIcon.setUrl(rewriteURL(channel.getUploaderAvatar())); - feed.setIcon(channelIcon); - feed.setImage(channelIcon); + if (filteredChannels.size() == 1) { + if (!videos.isEmpty()) { + ChannelHelpers.addChannelInformation(feed, videos.get(0).getChannel()); + } else { + String channelId = filteredChannels.stream().findFirst().get(); + final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId); + var channel = DatabaseHelper.getChannelFromId(channelId); + + if (channel == null) channel = new Channel(); + + ChannelHelpers.updateChannel(s, channel, StringUtils.abbreviate(info.getName(), 100), info.getAvatars().isEmpty() ? null : info.getAvatars().getLast().getUrl(), info.isVerified()); + + ChannelHelpers.addChannelInformation(feed, channel); } } feed.setEntries(entries); - updateSubscribedTime(filtered); - addMissingChannels(filtered); + updateSubscribedTime(filteredChannels); + addMissingChannels(filteredChannels); return new SyndFeedOutput().outputString(feed).getBytes(UTF_8); } diff --git a/src/main/java/me/kavin/piped/utils/ChannelHelpers.java b/src/main/java/me/kavin/piped/utils/ChannelHelpers.java index a6495f5..e57d7bf 100644 --- a/src/main/java/me/kavin/piped/utils/ChannelHelpers.java +++ b/src/main/java/me/kavin/piped/utils/ChannelHelpers.java @@ -118,4 +118,14 @@ public class ChannelHelpers { return entry; } + + public static void addChannelInformation(SyndFeed feed, Channel channel) { + feed.setTitle("Piped - " + channel.getUploader()); + SyndImage channelIcon = new SyndImageImpl(); + channelIcon.setLink(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId()); + channelIcon.setTitle(channel.getUploader()); + channelIcon.setUrl(rewriteURL(channel.getUploaderAvatar())); + feed.setIcon(channelIcon); + feed.setImage(channelIcon); + } } From 26844fcc1d7d7bf9efd922b99c68b6e92efce545 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 19 Jan 2024 14:36:28 +0100 Subject: [PATCH 2/2] fix: format duration in rss feed --- src/main/java/me/kavin/piped/utils/ChannelHelpers.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/kavin/piped/utils/ChannelHelpers.java b/src/main/java/me/kavin/piped/utils/ChannelHelpers.java index e57d7bf..b20f363 100644 --- a/src/main/java/me/kavin/piped/utils/ChannelHelpers.java +++ b/src/main/java/me/kavin/piped/utils/ChannelHelpers.java @@ -8,6 +8,7 @@ import me.kavin.piped.utils.obj.db.Channel; import me.kavin.piped.utils.obj.db.Video; import okhttp3.Request; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.commons.text.StringEscapeUtils; import org.hibernate.StatelessSession; @@ -86,7 +87,7 @@ public class ChannelHelpers { entry.setTitle(video.getTitle()); entry.setPublishedDate(new Date(video.getUploaded())); - String contentText = String.format("Title: %s\nViews: %d\nId: %s\nDuration: %d\nIs YT Shorts: %b", video.getTitle(), video.getViews(), video.getId(), video.getDuration(), video.isShort()); + String contentText = String.format("Title: %s\nViews: %d\nId: %s\nDuration: %s\nIs YT Shorts: %b", video.getTitle(), video.getViews(), video.getId(), DurationFormatUtils.formatDuration(video.getDuration() * 1000, "[HH]':'mm':'ss"), video.isShort()); content.setValue(contentText); String thumbnailContent =