CriteriaUpdate for video updates.

This commit is contained in:
Kavin 2022-11-17 15:54:09 +00:00
parent 4d58ec42d3
commit ec89d042e3
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
4 changed files with 42 additions and 47 deletions

View file

@ -106,7 +106,7 @@ public class ChannelHandlers {
.filter(v -> v.getId().equals(id))
.findFirst();
if (video.isPresent()) {
VideoHelpers.updateVideo(s, video.get(), item);
VideoHelpers.updateVideo(s, id, item);
} else {
VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel);
}

View file

@ -96,6 +96,15 @@ public class DatabaseHelper {
}
}
public static boolean doesVideoExist(SharedSessionContract s, String id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Video> cr = cb.createQuery(Video.class);
Root<Video> root = cr.from(Video.class);
cr.select(root.get("id")).where(cb.equal(root.get("id"), id));
return s.createQuery(cr).uniqueResult() != null;
}
public static PlaylistVideo getPlaylistVideoFromId(SharedSessionContract s, String id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<PlaylistVideo> cr = cb.createQuery(PlaylistVideo.class);

View file

@ -62,17 +62,13 @@ public class VideoHelpers {
}
}
public static void updateVideo(String id, StreamInfoItem item, long time, boolean addIfNotExistent) {
public static void updateVideo(String id, StreamInfoItem item, long time) {
Multithreading.runAsync(() -> {
try {
Video video = DatabaseHelper.getVideoFromId(id);
if (video != null) {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (!updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName())) {
handleNewVideo(item.getUrl(), time, null);
}
} else if (addIfNotExistent) {
handleNewVideo("https://www.youtube.com/watch?v=" + id, time, null);
}
} catch (Exception e) {
@ -84,47 +80,43 @@ public class VideoHelpers {
public static void updateVideo(String id, StreamInfo info, long time) {
Multithreading.runAsync(() -> {
try {
Video video = DatabaseHelper.getVideoFromId(id);
if (video != null) {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName());
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (!updateVideo(s, id, info.getViewCount(), info.getDuration(), info.getName())) {
handleNewVideo(info, time, null);
}
} else {
handleNewVideo(info, time, null);
}
} catch (Exception e) {
ExceptionHandler.handle(e);
}
});
}
public static void updateVideo(StatelessSession s, Video video, StreamInfoItem item) {
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
public static void updateVideo(StatelessSession s, String id, StreamInfoItem item) {
updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName());
}
public static void updateVideo(StatelessSession s, Video video, long views, long duration, String title) {
public static boolean updateVideo(StatelessSession s, String id, long views, long duration, String title) {
boolean changed = false;
var cb = s.getCriteriaBuilder();
var cu = cb.createCriteriaUpdate(Video.class);
var root = cu.from(Video.class);
cu.where(cb.equal(root.get("id"), id));
if (duration > 0 && video.getDuration() != duration) {
video.setDuration(duration);
changed = true;
if (duration > 0) {
cu.set(root.get("duration"), duration);
}
if (!video.getTitle().equals(title)) {
video.setTitle(title);
changed = true;
if (title != null) {
cu.set(root.get("title"), title);
}
if (views > video.getViews()) {
video.setViews(views);
changed = true;
if (views > 0) {
cu.set(root.get("views"), views);
}
if (changed) {
var tr = s.beginTransaction();
s.update(video);
tr.commit();
}
var tr = s.beginTransaction();
long updated = s.createMutationQuery(cu).executeUpdate();
tr.commit();
return updated > 0;
}
}

View file

@ -2,7 +2,6 @@ package me.kavin.piped.utils.matrix;
import com.fasterxml.jackson.databind.JsonNode;
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.db.Channel;
import me.kavin.piped.utils.obj.federation.FederatedChannelInfo;
import me.kavin.piped.utils.obj.federation.FederatedVideoInfo;
import okhttp3.MediaType;
@ -119,8 +118,6 @@ public class SyncRunner implements Runnable {
if (!initial_sync && events.size() > 0) {
System.out.println("Got " + events.size() + " events");
for (var event : events) {
var type = event.get("type").asText();
@ -141,15 +138,12 @@ public class SyncRunner implements Runnable {
FederatedVideoInfo info = mapper.treeToValue(content, FederatedVideoInfo.class);
Multithreading.runAsync(() -> {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
var video = DatabaseHelper.getVideoFromId(s, info.getVideoId());
Channel channel;
if (video != null)
VideoHelpers.updateVideo(s, video,
info.getViews(),
info.getDuration(),
info.getTitle());
else if ((channel = DatabaseHelper.getChannelFromId(s, info.getUploaderId())) != null) {
VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(), System.currentTimeMillis(), channel);
if (!VideoHelpers.updateVideo(s, info.getVideoId(),
info.getViews(),
info.getDuration(),
info.getTitle())) {
VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(),
System.currentTimeMillis(), null);
}
}
});