Merge pull request #769 from Bnyro/rss-duration

feat: add channel info to rss feed if no videos found
This commit is contained in:
Bnyro 2024-03-14 19:52:45 +01:00 committed by GitHub
commit 25a49ce137
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 14 deletions

View File

@ -22,6 +22,7 @@ import me.kavin.piped.utils.resp.SubscribeStatusResponse;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.channel.ChannelInfo;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@ -236,11 +237,11 @@ public class FeedHandlers {
public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws Exception { public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws Exception {
Set<String> filtered = Arrays.stream(channelIds) Set<String> filteredChannels = Arrays.stream(channelIds)
.filter(ChannelHelpers::isValidId) .filter(ChannelHelpers::isValidId)
.collect(Collectors.toUnmodifiableSet()); .collect(Collectors.toUnmodifiableSet());
if (filtered.isEmpty()) if (filteredChannels.isEmpty())
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("No valid channel IDs provided")); ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("No valid channel IDs provided"));
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
@ -254,7 +255,7 @@ public class FeedHandlers {
criteria.select(root) criteria.select(root)
.where(cb.and( .where(cb.and(
root.get("channel").get("id").in(filtered) root.get("channel").get("id").in(filteredChannels)
)) ))
.orderBy(cb.desc(root.get("uploaded"))); .orderBy(cb.desc(root.get("uploaded")));
@ -276,22 +277,28 @@ public class FeedHandlers {
var channel = video.getChannel(); var channel = video.getChannel();
SyndEntry entry = ChannelHelpers.createEntry(video, channel); SyndEntry entry = ChannelHelpers.createEntry(video, channel);
entries.add(entry); entries.add(entry);
}
if (filtered.size() == 1) { if (filteredChannels.size() == 1) {
feed.setTitle("Piped - " + channel.getUploader()); if (!videos.isEmpty()) {
SyndImage channelIcon = new SyndImageImpl(); ChannelHelpers.addChannelInformation(feed, videos.get(0).getChannel());
channelIcon.setLink(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId()); } else {
channelIcon.setTitle(channel.getUploader()); String channelId = filteredChannels.stream().findFirst().get();
channelIcon.setUrl(rewriteURL(channel.getUploaderAvatar())); final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
feed.setIcon(channelIcon); var channel = DatabaseHelper.getChannelFromId(channelId);
feed.setImage(channelIcon);
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); feed.setEntries(entries);
updateSubscribedTime(filtered); updateSubscribedTime(filteredChannels);
addMissingChannels(filtered); addMissingChannels(filteredChannels);
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8); return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
} }

View File

@ -8,6 +8,7 @@ import me.kavin.piped.utils.obj.db.Channel;
import me.kavin.piped.utils.obj.db.Video; import me.kavin.piped.utils.obj.db.Video;
import okhttp3.Request; import okhttp3.Request;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.text.StringEscapeUtils; import org.apache.commons.text.StringEscapeUtils;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
@ -86,7 +87,7 @@ public class ChannelHelpers {
entry.setTitle(video.getTitle()); entry.setTitle(video.getTitle());
entry.setPublishedDate(new Date(video.getUploaded())); 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); content.setValue(contentText);
String thumbnailContent = String thumbnailContent =
@ -118,4 +119,14 @@ public class ChannelHelpers {
return entry; 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);
}
} }