From abd19aa94d784b9326d367cf6e75b7d9e3b25794 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Wed, 23 Nov 2022 21:52:01 +0000 Subject: [PATCH] Better event send handling. --- .../me/kavin/piped/server/ServerLauncher.java | 28 +++++++++++++++- .../me/kavin/piped/utils/VideoHelpers.java | 32 ++----------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index 4098ad5..07a9b9b 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -18,18 +18,22 @@ import me.kavin.piped.server.handlers.auth.AuthPlaylistHandlers; import me.kavin.piped.server.handlers.auth.FeedHandlers; import me.kavin.piped.server.handlers.auth.UserHandlers; import me.kavin.piped.utils.*; +import me.kavin.piped.utils.obj.MatrixHelper; +import me.kavin.piped.utils.obj.federation.FederatedVideoInfo; import me.kavin.piped.utils.resp.*; import org.apache.commons.lang3.StringUtils; 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.xml.sax.InputSource; import java.io.ByteArrayInputStream; import java.net.InetSocketAddress; import java.util.Objects; import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.http.HttpHeaders.*; @@ -77,7 +81,29 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { if (DatabaseHelper.doesVideoExist(s, StringUtils.substring(url, -11))) continue; } - VideoHelpers.handleNewVideo(url, entry.getPublishedDate().getTime(), null); + Multithreading.runAsync(() -> { + try { + StreamInfo info = StreamInfo.getInfo(url); + + Multithreading.runAsync(() -> { + if (info.getUploadDate() != null && System.currentTimeMillis() - info.getUploadDate().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()) + ); + } catch (Exception e) { + ExceptionHandler.handle(e); + } + } + }); + + VideoHelpers.handleNewVideo(info, 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 10fe52e..42d696e 100644 --- a/src/main/java/me/kavin/piped/utils/VideoHelpers.java +++ b/src/main/java/me/kavin/piped/utils/VideoHelpers.java @@ -1,9 +1,7 @@ package me.kavin.piped.utils; import me.kavin.piped.consts.Constants; -import me.kavin.piped.utils.obj.MatrixHelper; import me.kavin.piped.utils.obj.db.Video; -import me.kavin.piped.utils.obj.federation.FederatedVideoInfo; import org.apache.commons.lang3.StringUtils; import org.hibernate.StatelessSession; import org.schabi.newpipe.extractor.stream.StreamInfo; @@ -22,20 +20,6 @@ public class VideoHelpers { public static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel) { - Multithreading.runAsync(() -> { - if (info.getUploadDate() != null && System.currentTimeMillis() - info.getUploadDate().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()) - ); - } catch (Exception e) { - ExceptionHandler.handle(e); - } - } - }); - if (channel == null) channel = DatabaseHelper.getChannelFromId( info.getUploaderUrl().substring("https://www.youtube.com/channel/".length())); @@ -68,23 +52,13 @@ public class VideoHelpers { } } - public static void updateVideo(String id, StreamInfoItem item, long time) { - Multithreading.runAsync(() -> { - try { - if (!updateVideo(id, item.getViewCount(), item.getDuration(), item.getName())) { - handleNewVideo(item.getUrl(), time, null); - } - } catch (Exception e) { - ExceptionHandler.handle(e); - } - }); - } - public static void updateVideo(String id, StreamInfo info, long time) { Multithreading.runAsync(() -> { try { if (!updateVideo(id, info.getViewCount(), info.getDuration(), info.getName())) { - handleNewVideo(info, time, null); + var channel = DatabaseHelper.getChannelFromId(StringUtils.substring(info.getUploaderUrl(), -24)); + if (channel != null) + handleNewVideo(info, time, channel); } } catch (Exception e) { ExceptionHandler.handle(e);