From bb3bb3889a5a0996a5e6b32a725edab876af65e6 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 1 Jun 2023 19:12:54 +0100 Subject: [PATCH] Fix for fetching channels without a videos tab. --- .../server/handlers/ChannelHandlers.java | 151 ++++++++++-------- 1 file changed, 80 insertions(+), 71 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java b/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java index a7a5f79..8e292bf 100644 --- a/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java @@ -41,30 +41,38 @@ public class ChannelHandlers { 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 relatedStreams = collectRelatedItems(tabInfo.getRelatedItems()); + final ChannelTabInfo tabInfo = preloadedVideosTab.isPresent() ? + ChannelTabInfo.getInfo(YOUTUBE_SERVICE, preloadedVideosTab.get()) : + null; - Multithreading.runAsync(() -> tabInfo.getRelatedItems() - .stream().filter(StreamInfoItem.class::isInstance) - .map(StreamInfoItem.class::cast) - .forEach(infoItem -> { - if ( - infoItem.getUploadDate() != null && - System.currentTimeMillis() - infoItem.getUploadDate().offsetDateTime().toInstant().toEpochMilli() - < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION) - ) - try { - MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo( - StringUtils.substring(infoItem.getUrl(), -11), StringUtils.substring(infoItem.getUploaderUrl(), -24), - infoItem.getName(), - infoItem.getDuration(), infoItem.getViewCount()) - ); - } catch (IOException e) { - throw new RuntimeException(e); - } - }) - ); + final List relatedStreams = tabInfo != null ? collectRelatedItems(tabInfo.getRelatedItems()) : List.of(); + + if (tabInfo != null) + Multithreading.runAsync(() -> tabInfo.getRelatedItems() + .stream().filter(StreamInfoItem.class::isInstance) + .map(StreamInfoItem.class::cast) + .forEach(infoItem -> { + if ( + infoItem.getUploadDate() != null && + System.currentTimeMillis() - infoItem.getUploadDate().offsetDateTime().toInstant().toEpochMilli() + < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION) + ) + try { + MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo( + StringUtils.substring(infoItem.getUrl(), -11), StringUtils.substring(infoItem.getUploaderUrl(), -24), + infoItem.getName(), + infoItem.getDuration(), infoItem.getViewCount()) + ); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + ); Multithreading.runAsync(() -> { 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 ids = tabInfo.getRelatedItems() - .stream() - .filter(StreamInfoItem.class::isInstance) - .map(StreamInfoItem.class::cast) - .filter(item -> { - long time = item.getUploadDate() != null - ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() - : System.currentTimeMillis(); - return System.currentTimeMillis() - time < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION); - }) - .map(item -> { - try { - return YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl()); - } catch (ParsingException e) { - throw new RuntimeException(e); - } - }) - .collect(Collectors.toUnmodifiableSet()); - - List