From 710f62f8e55abf356e0f5bbef300a8f453dd0afa Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Tue, 6 Jun 2023 02:32:27 +0100 Subject: [PATCH] Use stream extractor directly to fix no streams found error. --- .../me/kavin/piped/server/ServerLauncher.java | 26 +++++++++---- .../me/kavin/piped/utils/VideoHelpers.java | 37 +++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index 25ef627..97dc586 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -27,7 +27,8 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.hibernate.Session; import org.hibernate.StatelessSession; import org.jetbrains.annotations.NotNull; -import org.schabi.newpipe.extractor.stream.StreamInfo; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.xml.sax.InputSource; import java.io.ByteArrayInputStream; @@ -40,6 +41,7 @@ import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.http.HttpHeaders.*; import static io.activej.http.HttpMethod.*; 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; public class ServerLauncher extends MultithreadedHttpServerLauncher { @@ -96,15 +98,25 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { Multithreading.runAsync(() -> { try { Sentry.setExtra("videoId", videoId); - StreamInfo info = StreamInfo.getInfo(url); + var extractor = YOUTUBE_SERVICE.getStreamExtractor(videoId); + extractor.fetchPage(); Multithreading.runAsync(() -> { - if (info.getUploadDate() != null && System.currentTimeMillis() - info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) { + + DateWrapper uploadDate; + + try { + uploadDate = extractor.getUploadDate(); + } catch (ParsingException e) { + throw new RuntimeException(e); + } + + if (uploadDate != null && System.currentTimeMillis() - uploadDate.offsetDateTime().toInstant().toEpochMilli() < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) { try { MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo( - StringUtils.substring(info.getUrl(), -11), StringUtils.substring(info.getUploaderUrl(), -24), - info.getName(), - info.getDuration(), info.getViewCount()) + StringUtils.substring(extractor.getUrl(), -11), StringUtils.substring(extractor.getUploaderUrl(), -24), + extractor.getName(), + extractor.getLength(), extractor.getViewCount()) ); } catch (Exception e) { ExceptionHandler.handle(e); @@ -112,7 +124,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } }); - VideoHelpers.handleNewVideo(info, entry.getPublishedDate().getTime(), null); + VideoHelpers.handleNewVideo(extractor, entry.getPublishedDate().getTime(), null); } catch (Exception e) { ExceptionHandler.handle(e); } diff --git a/src/main/java/me/kavin/piped/utils/VideoHelpers.java b/src/main/java/me/kavin/piped/utils/VideoHelpers.java index 0242e85..175f8ab 100644 --- a/src/main/java/me/kavin/piped/utils/VideoHelpers.java +++ b/src/main/java/me/kavin/piped/utils/VideoHelpers.java @@ -6,9 +6,12 @@ import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.obj.db.Video; import org.apache.commons.lang3.StringUtils; import org.hibernate.StatelessSession; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfoItem; +import java.util.Optional; import java.util.concurrent.TimeUnit; import static java.nio.charset.StandardCharsets.UTF_8; @@ -63,6 +66,40 @@ public class VideoHelpers { } } + public static void handleNewVideo(StreamExtractor extractor, long time, me.kavin.piped.utils.obj.db.Channel channel) throws ParsingException { + + if (channel == null) + channel = DatabaseHelper.getChannelFromId( + extractor.getUploaderUrl().substring("https://www.youtube.com/channel/".length())); + + long infoTime = Optional.ofNullable(extractor.getUploadDate()) + .map(date -> date.offsetDateTime().toInstant().toEpochMilli()) + .orElseGet(System::currentTimeMillis); + + if (channel != null + && (System.currentTimeMillis() - infoTime) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) { + + try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { + if (!DatabaseHelper.doesVideoExist(s, extractor.getId())) { + + Video video = new Video(extractor.getId(), extractor.getName(), extractor.getViewCount(), extractor.getLength(), + Math.max(infoTime, time), extractor.getThumbnailUrl(), extractor.isShortFormContent(), channel); + + var tr = s.beginTransaction(); + try { + s.insert(video); + tr.commit(); + } catch (Exception e) { + tr.rollback(); + ExceptionHandler.handle(e); + } + + } + } + } + + } + public static boolean isShort(String videoId) throws Exception { final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(