diff --git a/src/main/java/me/kavin/piped/utils/CollectionUtils.java b/src/main/java/me/kavin/piped/utils/CollectionUtils.java new file mode 100644 index 0000000..1537706 --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/CollectionUtils.java @@ -0,0 +1,60 @@ +package me.kavin.piped.utils; + +import me.kavin.piped.utils.obj.ChannelItem; +import me.kavin.piped.utils.obj.ContentItem; +import me.kavin.piped.utils.obj.PlaylistItem; +import me.kavin.piped.utils.obj.StreamItem; +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.channel.ChannelInfoItem; +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; +import org.schabi.newpipe.extractor.stream.StreamInfoItem; + +import java.util.List; + +import static me.kavin.piped.utils.URLUtils.rewriteURL; +import static me.kavin.piped.utils.URLUtils.substringYouTube; + +public class CollectionUtils { + + public static List collectRelatedItems(List items) { + return items + .stream() + .parallel() + .map(item -> { + if (item instanceof StreamInfoItem) + return collectRelatedStream(item); + else if (item instanceof PlaylistInfoItem) + return collectRelatedPlaylist(item); + else if (item instanceof ChannelInfoItem) + return collectRelatedChannel(item); + else + throw new RuntimeException("Unknown item type: " + item.getClass().getName()); + }).toList(); + } + + private static StreamItem collectRelatedStream(Object o) { + + StreamInfoItem item = (StreamInfoItem) o; + + return new StreamItem(substringYouTube(item.getUrl()), item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUploaderName(), substringYouTube(item.getUploaderUrl()), + rewriteURL(item.getUploaderAvatarUrl()), item.getTextualUploadDate(), item.getShortDescription(), item.getDuration(), + item.getViewCount(), item.getUploadDate() != null ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() : -1, item.isUploaderVerified(), item.isShortFormContent()); + } + + private static PlaylistItem collectRelatedPlaylist(Object o) { + + PlaylistInfoItem item = (PlaylistInfoItem) o; + + return new PlaylistItem(substringYouTube(item.getUrl()), item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getUploaderName(), item.getPlaylistType().name(), item.getStreamCount()); + } + + private static ChannelItem collectRelatedChannel(Object o) { + + ChannelInfoItem item = (ChannelInfoItem) o; + + return new ChannelItem(substringYouTube(item.getUrl()), item.getName(), rewriteURL(item.getThumbnailUrl()), + item.getDescription(), item.getSubscriberCount(), item.getStreamCount(), item.isVerified()); + } +} diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 4f273c0..49aaaee 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -31,7 +31,6 @@ import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage; import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.channel.ChannelInfo; -import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.extractor.channel.ChannelTabInfo; import org.schabi.newpipe.extractor.comments.CommentsInfo; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; @@ -42,7 +41,6 @@ import org.schabi.newpipe.extractor.kiosk.KioskInfo; import org.schabi.newpipe.extractor.kiosk.KioskList; import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.playlist.PlaylistInfo; -import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.search.SearchInfo; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YouTubeChannelTabHandler; import org.schabi.newpipe.extractor.stream.StreamInfo; @@ -60,6 +58,7 @@ import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE; import static me.kavin.piped.consts.Constants.mapper; +import static me.kavin.piped.utils.CollectionUtils.collectRelatedItems; import static me.kavin.piped.utils.URLUtils.*; import static org.schabi.newpipe.extractor.NewPipe.getPreferredContentCountry; import static org.schabi.newpipe.extractor.NewPipe.getPreferredLocalization; @@ -1798,46 +1797,4 @@ public class ResponseHelper { } } - - private static List collectRelatedItems(List items) { - return items - .stream() - .parallel() - .map(item -> { - if (item instanceof StreamInfoItem) - return collectRelatedStream(item); - else if (item instanceof PlaylistInfoItem) - return collectRelatedPlaylist(item); - else if (item instanceof ChannelInfoItem) - return collectRelatedChannel(item); - else - throw new RuntimeException("Unknown item type: " + item.getClass().getName()); - }).toList(); - } - - private static StreamItem collectRelatedStream(Object o) { - - StreamInfoItem item = (StreamInfoItem) o; - - return new StreamItem(substringYouTube(item.getUrl()), item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUploaderName(), substringYouTube(item.getUploaderUrl()), - rewriteURL(item.getUploaderAvatarUrl()), item.getTextualUploadDate(), item.getShortDescription(), item.getDuration(), - item.getViewCount(), item.getUploadDate() != null ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() : -1, item.isUploaderVerified(), item.isShortFormContent()); - } - - private static PlaylistItem collectRelatedPlaylist(Object o) { - - PlaylistInfoItem item = (PlaylistInfoItem) o; - - return new PlaylistItem(substringYouTube(item.getUrl()), item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getUploaderName(), item.getPlaylistType().name(), item.getStreamCount()); - } - - private static ChannelItem collectRelatedChannel(Object o) { - - ChannelInfoItem item = (ChannelInfoItem) o; - - return new ChannelItem(substringYouTube(item.getUrl()), item.getName(), rewriteURL(item.getThumbnailUrl()), - item.getDescription(), item.getSubscriberCount(), item.getStreamCount(), item.isVerified()); - } }