Merge pull request #445 from TeamPiped/criteria-updates

CriteriaUpdate for video updates.
This commit is contained in:
Kavin 2022-11-17 15:59:59 +00:00 committed by GitHub
commit c2248a1bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 47 deletions

View File

@ -106,7 +106,7 @@ public class ChannelHandlers {
.filter(v -> v.getId().equals(id)) .filter(v -> v.getId().equals(id))
.findFirst(); .findFirst();
if (video.isPresent()) { if (video.isPresent()) {
VideoHelpers.updateVideo(s, video.get(), item); VideoHelpers.updateVideo(s, id, item);
} else { } else {
VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel); 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) { public static PlaylistVideo getPlaylistVideoFromId(SharedSessionContract s, String id) {
CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<PlaylistVideo> cr = cb.createQuery(PlaylistVideo.class); 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(() -> { Multithreading.runAsync(() -> {
try { try {
Video video = DatabaseHelper.getVideoFromId(id); try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (!updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName())) {
if (video != null) { handleNewVideo(item.getUrl(), time, null);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
} }
} else if (addIfNotExistent) {
handleNewVideo("https://www.youtube.com/watch?v=" + id, time, null);
} }
} catch (Exception e) { } catch (Exception e) {
@ -84,47 +80,43 @@ public class VideoHelpers {
public static void updateVideo(String id, StreamInfo info, long time) { public static void updateVideo(String id, StreamInfo info, long time) {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
Video video = DatabaseHelper.getVideoFromId(id); try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (!updateVideo(s, id, info.getViewCount(), info.getDuration(), info.getName())) {
if (video != null) { handleNewVideo(info, time, null);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName());
} }
} else {
handleNewVideo(info, time, null);
} }
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }
}); });
} }
public static void updateVideo(StatelessSession s, Video video, StreamInfoItem item) { public static void updateVideo(StatelessSession s, String id, StreamInfoItem item) {
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName()); 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); if (duration > 0) {
changed = true; cu.set(root.get("duration"), duration);
} }
if (!video.getTitle().equals(title)) { if (title != null) {
video.setTitle(title); cu.set(root.get("title"), title);
changed = true;
} }
if (views > video.getViews()) { if (views > 0) {
video.setViews(views); cu.set(root.get("views"), views);
changed = true;
} }
if (changed) { var tr = s.beginTransaction();
var tr = s.beginTransaction(); long updated = s.createMutationQuery(cu).executeUpdate();
s.update(video); tr.commit();
tr.commit();
} return updated > 0;
} }
} }

View File

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