From a15444645a94559efcbfdd19a6f8fc5b22dec3ad Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 17 Nov 2022 16:24:39 +0000 Subject: [PATCH] Don't query full video anywhere. --- .../me/kavin/piped/server/ServerLauncher.java | 7 ++++-- .../me/kavin/piped/utils/VideoHelpers.java | 23 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index 4d482f4..4098ad5 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -22,6 +22,7 @@ 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.xml.sax.InputSource; @@ -72,8 +73,10 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { Multithreading.runAsync(() -> { for (var entry : feed.getEntries()) { String url = entry.getLinks().get(0).getHref(); - if (DatabaseHelper.getVideoFromId(StringUtils.substring(url, -11)) != null) - continue; + try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { + if (DatabaseHelper.doesVideoExist(s, StringUtils.substring(url, -11))) + continue; + } VideoHelpers.handleNewVideo(url, entry.getPublishedDate().getTime(), null); } }); diff --git a/src/main/java/me/kavin/piped/utils/VideoHelpers.java b/src/main/java/me/kavin/piped/utils/VideoHelpers.java index deb20b4..771c016 100644 --- a/src/main/java/me/kavin/piped/utils/VideoHelpers.java +++ b/src/main/java/me/kavin/piped/utils/VideoHelpers.java @@ -43,22 +43,23 @@ public class VideoHelpers { long infoTime = info.getUploadDate() != null ? info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() : System.currentTimeMillis(); - Video video = null; - - if (channel != null && (video = DatabaseHelper.getVideoFromId(info.getId())) == null + if (channel != null && (System.currentTimeMillis() - infoTime) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) { - - video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(), - Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel); - try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { - var tr = s.beginTransaction(); - s.insert(video); - tr.commit(); + if (!DatabaseHelper.doesVideoExist(s, info.getId())) { + + Video video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(), + Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel); + + var tr = s.beginTransaction(); + s.insert(video); + tr.commit(); + return; + } } - } else if (video != null) { updateVideo(info.getId(), info, time); + } }