Skip to content

Commit 25a49ce

Browse files
authored
Merge pull request #769 from Bnyro/rss-duration
feat: add channel info to rss feed if no videos found
2 parents cd4bc36 + 26844fc commit 25a49ce

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.lang3.StringUtils;
2323
import org.hibernate.Session;
2424
import org.hibernate.StatelessSession;
25+
import org.schabi.newpipe.extractor.channel.ChannelInfo;
2526

2627
import java.io.IOException;
2728
import java.util.*;
@@ -236,11 +237,11 @@ public static byte[] unauthenticatedFeedResponse(String[] channelIds) throws Exc
236237

237238
public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws Exception {
238239

239-
Set<String> filtered = Arrays.stream(channelIds)
240+
Set<String> filteredChannels = Arrays.stream(channelIds)
240241
.filter(ChannelHelpers::isValidId)
241242
.collect(Collectors.toUnmodifiableSet());
242243

243-
if (filtered.isEmpty())
244+
if (filteredChannels.isEmpty())
244245
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("No valid channel IDs provided"));
245246

246247
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
@@ -254,7 +255,7 @@ public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws
254255

255256
criteria.select(root)
256257
.where(cb.and(
257-
root.get("channel").get("id").in(filtered)
258+
root.get("channel").get("id").in(filteredChannels)
258259
))
259260
.orderBy(cb.desc(root.get("uploaded")));
260261

@@ -276,22 +277,28 @@ public static byte[] unauthenticatedFeedResponseRSS(String[] channelIds) throws
276277
var channel = video.getChannel();
277278
SyndEntry entry = ChannelHelpers.createEntry(video, channel);
278279
entries.add(entry);
280+
}
281+
282+
if (filteredChannels.size() == 1) {
283+
if (!videos.isEmpty()) {
284+
ChannelHelpers.addChannelInformation(feed, videos.get(0).getChannel());
285+
} else {
286+
String channelId = filteredChannels.stream().findFirst().get();
287+
final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
288+
var channel = DatabaseHelper.getChannelFromId(channelId);
279289

280-
if (filtered.size() == 1) {
281-
feed.setTitle("Piped - " + channel.getUploader());
282-
SyndImage channelIcon = new SyndImageImpl();
283-
channelIcon.setLink(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId());
284-
channelIcon.setTitle(channel.getUploader());
285-
channelIcon.setUrl(rewriteURL(channel.getUploaderAvatar()));
286-
feed.setIcon(channelIcon);
287-
feed.setImage(channelIcon);
290+
if (channel == null) channel = new Channel();
291+
292+
ChannelHelpers.updateChannel(s, channel, StringUtils.abbreviate(info.getName(), 100), info.getAvatars().isEmpty() ? null : info.getAvatars().getLast().getUrl(), info.isVerified());
293+
294+
ChannelHelpers.addChannelInformation(feed, channel);
288295
}
289296
}
290297

291298
feed.setEntries(entries);
292299

293-
updateSubscribedTime(filtered);
294-
addMissingChannels(filtered);
300+
updateSubscribedTime(filteredChannels);
301+
addMissingChannels(filteredChannels);
295302

296303
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
297304
}

src/main/java/me/kavin/piped/utils/ChannelHelpers.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import me.kavin.piped.utils.obj.db.Video;
99
import okhttp3.Request;
1010
import org.apache.commons.lang3.StringUtils;
11+
import org.apache.commons.lang3.time.DurationFormatUtils;
1112
import org.apache.commons.text.StringEscapeUtils;
1213
import org.hibernate.StatelessSession;
1314

@@ -86,7 +87,7 @@ public static SyndEntry createEntry(Video video, Channel channel) {
8687
entry.setTitle(video.getTitle());
8788
entry.setPublishedDate(new Date(video.getUploaded()));
8889

89-
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());
90+
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());
9091
content.setValue(contentText);
9192

9293
String thumbnailContent =
@@ -118,4 +119,14 @@ public static SyndEntry createEntry(Video video, Channel channel) {
118119

119120
return entry;
120121
}
122+
123+
public static void addChannelInformation(SyndFeed feed, Channel channel) {
124+
feed.setTitle("Piped - " + channel.getUploader());
125+
SyndImage channelIcon = new SyndImageImpl();
126+
channelIcon.setLink(Constants.FRONTEND_URL + "/channel/" + channel.getUploaderId());
127+
channelIcon.setTitle(channel.getUploader());
128+
channelIcon.setUrl(rewriteURL(channel.getUploaderAvatar()));
129+
feed.setIcon(channelIcon);
130+
feed.setImage(channelIcon);
131+
}
121132
}

0 commit comments

Comments
 (0)