mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2025-04-29 16:30:29 +05:30
Merge pull request #445 from TeamPiped/criteria-updates
CriteriaUpdate for video updates.
This commit is contained in:
commit
c2248a1bdc
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -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);
|
|
||||||
|
|
||||||
if (video != null) {
|
|
||||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||||
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
|
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) {
|
} 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);
|
|
||||||
|
|
||||||
if (video != null) {
|
|
||||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||||
updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName());
|
if (!updateVideo(s, id, info.getViewCount(), info.getDuration(), info.getName())) {
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handleNewVideo(info, time, null);
|
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();
|
||||||
s.update(video);
|
long updated = s.createMutationQuery(cu).executeUpdate();
|
||||||
tr.commit();
|
tr.commit();
|
||||||
}
|
|
||||||
|
return updated > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
if (video != null)
|
|
||||||
VideoHelpers.updateVideo(s, video,
|
|
||||||
info.getViews(),
|
info.getViews(),
|
||||||
info.getDuration(),
|
info.getDuration(),
|
||||||
info.getTitle());
|
info.getTitle())) {
|
||||||
else if ((channel = DatabaseHelper.getChannelFromId(s, info.getUploaderId())) != null) {
|
VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(),
|
||||||
VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(), System.currentTimeMillis(), channel);
|
System.currentTimeMillis(), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user