Fix for fetching channels without a videos tab.

This commit is contained in:
Kavin 2023-06-01 19:12:54 +01:00
parent 1891a2e6a8
commit bb3bb3889a
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
1 changed files with 80 additions and 71 deletions

View File

@ -41,30 +41,38 @@ public class ChannelHandlers {
final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/" + channelPath); final ChannelInfo info = ChannelInfo.getInfo("https://youtube.com/" + channelPath);
final ChannelTabInfo tabInfo = ChannelTabInfo.getInfo(YOUTUBE_SERVICE, collectPreloadedTabs(info.getTabs()).get(0)); final var preloadedVideosTab = collectPreloadedTabs(info.getTabs())
.stream()
.filter(tab -> tab.getContentFilters().contains(ChannelTabs.VIDEOS))
.findFirst();
final List<ContentItem> relatedStreams = collectRelatedItems(tabInfo.getRelatedItems()); final ChannelTabInfo tabInfo = preloadedVideosTab.isPresent() ?
ChannelTabInfo.getInfo(YOUTUBE_SERVICE, preloadedVideosTab.get()) :
null;
Multithreading.runAsync(() -> tabInfo.getRelatedItems() final List<ContentItem> relatedStreams = tabInfo != null ? collectRelatedItems(tabInfo.getRelatedItems()) : List.of();
.stream().filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast) if (tabInfo != null)
.forEach(infoItem -> { Multithreading.runAsync(() -> tabInfo.getRelatedItems()
if ( .stream().filter(StreamInfoItem.class::isInstance)
infoItem.getUploadDate() != null && .map(StreamInfoItem.class::cast)
System.currentTimeMillis() - infoItem.getUploadDate().offsetDateTime().toInstant().toEpochMilli() .forEach(infoItem -> {
< TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION) if (
) infoItem.getUploadDate() != null &&
try { System.currentTimeMillis() - infoItem.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo( < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)
StringUtils.substring(infoItem.getUrl(), -11), StringUtils.substring(infoItem.getUploaderUrl(), -24), )
infoItem.getName(), try {
infoItem.getDuration(), infoItem.getViewCount()) MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo(
); StringUtils.substring(infoItem.getUrl(), -11), StringUtils.substring(infoItem.getUploaderUrl(), -24),
} catch (IOException e) { infoItem.getName(),
throw new RuntimeException(e); infoItem.getDuration(), infoItem.getViewCount())
} );
}) } catch (IOException e) {
); throw new RuntimeException(e);
}
})
);
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
@ -74,65 +82,66 @@ public class ChannelHandlers {
} }
}); });
Multithreading.runAsync(() -> { if (tabInfo != null)
Multithreading.runAsync(() -> {
var channel = DatabaseHelper.getChannelFromId(info.getId()); var channel = DatabaseHelper.getChannelFromId(info.getId());
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (channel != null) { if (channel != null) {
ChannelHelpers.updateChannel(s, channel, info.getName(), info.getAvatarUrl(), info.isVerified()); ChannelHelpers.updateChannel(s, channel, info.getName(), info.getAvatarUrl(), info.isVerified());
Set<String> ids = tabInfo.getRelatedItems() Set<String> ids = tabInfo.getRelatedItems()
.stream() .stream()
.filter(StreamInfoItem.class::isInstance) .filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast) .map(StreamInfoItem.class::cast)
.filter(item -> { .filter(item -> {
long time = item.getUploadDate() != null long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis(); : System.currentTimeMillis();
return System.currentTimeMillis() - time < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION); return System.currentTimeMillis() - time < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION);
}) })
.map(item -> { .map(item -> {
try {
return YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl());
} catch (ParsingException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toUnmodifiableSet());
List<Video> videos = DatabaseHelper.getVideosFromIds(s, ids);
tabInfo.getRelatedItems()
.stream()
.filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast).forEach(item -> {
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if (System.currentTimeMillis() - time < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
try { try {
String id = YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl()); return YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl());
var video = videos.stream() } catch (ParsingException e) {
.filter(v -> v.getId().equals(id)) throw new RuntimeException(e);
.findFirst();
if (video.isPresent()) {
VideoHelpers.updateVideo(id, item);
} else {
VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel);
}
} catch (Exception e) {
ExceptionHandler.handle(e);
} }
}); })
.collect(Collectors.toUnmodifiableSet());
List<Video> videos = DatabaseHelper.getVideosFromIds(s, ids);
tabInfo.getRelatedItems()
.stream()
.filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast).forEach(item -> {
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if (System.currentTimeMillis() - time < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
try {
String id = YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl());
var video = videos.stream()
.filter(v -> v.getId().equals(id))
.findFirst();
if (video.isPresent()) {
VideoHelpers.updateVideo(id, item);
} else {
VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel);
}
} catch (Exception e) {
ExceptionHandler.handle(e);
}
});
}
} }
} });
});
String nextpage = null; String nextpage = null;
if (tabInfo.hasNextPage()) { if (tabInfo != null && tabInfo.hasNextPage()) {
Page page = tabInfo.getNextPage(); Page page = tabInfo.getNextPage();
nextpage = mapper.writeValueAsString(page); nextpage = mapper.writeValueAsString(page);
} }