feat: add channel info to rss feed if no videos found

This commit is contained in:
Bnyro 2024-01-19 14:23:04 +01:00
parent c746794d74
commit 7b9e0ac432
2 changed files with 30 additions and 13 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

@ -118,4 +118,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);
}
} }