Merge pull request #446 from TeamPiped/video-insert-projection

Don't query full video anywhere
This commit is contained in:
Kavin 2022-11-17 16:27:25 +00:00 committed by GitHub
commit 12ac82393e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 13 deletions

View file

@ -22,6 +22,7 @@ import me.kavin.piped.utils.resp.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@ -72,8 +73,10 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
for (var entry : feed.getEntries()) { for (var entry : feed.getEntries()) {
String url = entry.getLinks().get(0).getHref(); String url = entry.getLinks().get(0).getHref();
if (DatabaseHelper.getVideoFromId(StringUtils.substring(url, -11)) != null) try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
continue; if (DatabaseHelper.doesVideoExist(s, StringUtils.substring(url, -11)))
continue;
}
VideoHelpers.handleNewVideo(url, entry.getPublishedDate().getTime(), null); VideoHelpers.handleNewVideo(url, entry.getPublishedDate().getTime(), null);
} }
}); });

View file

@ -43,22 +43,23 @@ public class VideoHelpers {
long infoTime = info.getUploadDate() != null ? info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() long infoTime = info.getUploadDate() != null ? info.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis(); : System.currentTimeMillis();
Video video = null; if (channel != null
if (channel != null && (video = DatabaseHelper.getVideoFromId(info.getId())) == null
&& (System.currentTimeMillis() - infoTime) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) { && (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()) { try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
var tr = s.beginTransaction(); if (!DatabaseHelper.doesVideoExist(s, info.getId())) {
s.insert(video);
tr.commit(); 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); updateVideo(info.getId(), info, time);
} }
} }