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.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<String> filtered = Arrays.stream(channelIds)
Set<String> 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);
}

View file

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