Simplify session handling for new videos. (#299)

This commit is contained in:
Kavin 2022-07-02 21:30:19 +01:00 committed by GitHub
parent 0158744506
commit c8628dcf05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 23 deletions

View File

@ -63,11 +63,9 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
new InputSource(new ByteArrayInputStream(request.loadBody().getResult().asArray()))); new InputSource(new ByteArrayInputStream(request.loadBody().getResult().asArray())));
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try (Session s = DatabaseSessionFactory.createSession()) {
for (var entry : feed.getEntries()) { for (var entry : feed.getEntries()) {
ResponseHelper.handleNewVideo(entry.getLinks().get(0).getHref(), ResponseHelper.handleNewVideo(entry.getLinks().get(0).getHref(),
entry.getPublishedDate().getTime(), null, s); entry.getPublishedDate().getTime(), null);
}
} }
}); });

View File

@ -51,6 +51,13 @@ public class DatabaseHelper {
return s.createQuery(cr).uniqueResult(); return s.createQuery(cr).uniqueResult();
} }
public static Channel getChannelFromId(String id) {
try (Session s = DatabaseSessionFactory.createSession()) {
s.setHibernateFlushMode(FlushMode.MANUAL);
return getChannelFromId(s, id);
}
}
public static List<Channel> getChannelsFromIds(Session s, List<String> id) { public static List<Channel> getChannelsFromIds(Session s, List<String> id) {
CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Channel> cr = cb.createQuery(Channel.class); CriteriaQuery<Channel> cr = cb.createQuery(Channel.class);
@ -69,6 +76,13 @@ public class DatabaseHelper {
return s.createQuery(cr).uniqueResult(); return s.createQuery(cr).uniqueResult();
} }
public static Video getVideoFromId(String id) {
try (Session s = DatabaseSessionFactory.createSession()) {
s.setHibernateFlushMode(FlushMode.MANUAL);
return getVideoFromId(s, id);
}
}
public static PlaylistVideo getPlaylistVideoFromId(Session s, String id) { public static PlaylistVideo getPlaylistVideoFromId(Session 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

@ -1289,9 +1289,9 @@ public class ResponseHelper {
} }
} }
public static void handleNewVideo(String url, long time, me.kavin.piped.utils.obj.db.Channel channel, Session s) { public static void handleNewVideo(String url, long time, me.kavin.piped.utils.obj.db.Channel channel) {
try { try {
handleNewVideo(StreamInfo.getInfo(url), time, channel, s); handleNewVideo(StreamInfo.getInfo(url), time, channel);
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }
@ -1318,10 +1318,10 @@ public class ResponseHelper {
} }
} }
private static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel, Session s) { private static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel) {
if (channel == null) if (channel == null)
channel = DatabaseHelper.getChannelFromId(s, channel = DatabaseHelper.getChannelFromId(
info.getUploaderUrl().substring("https://www.youtube.com/channel/".length())); info.getUploaderUrl().substring("https://www.youtube.com/channel/".length()));
long infoTime = info.getUploadDate() != null ? info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() long infoTime = info.getUploadDate() != null ? info.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
@ -1329,15 +1329,17 @@ public class ResponseHelper {
Video video = null; Video video = null;
if (channel != null && (video = DatabaseHelper.getVideoFromId(s, info.getId())) == 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(), video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
Math.max(infoTime, time), info.getThumbnailUrl(), channel); Math.max(infoTime, time), info.getThumbnailUrl(), channel);
try (Session s = DatabaseSessionFactory.createSession()) {
var tr = s.beginTransaction(); var tr = s.beginTransaction();
s.persist(video); s.persist(video);
tr.commit(); tr.commit();
}
} else if (video != null) { } else if (video != null) {
updateVideo(info.getId(), info, time); updateVideo(info.getId(), info, time);
@ -1353,7 +1355,7 @@ public class ResponseHelper {
if (video != null) { if (video != null) {
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName()); updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
} else if (addIfNotExistent) { } else if (addIfNotExistent) {
handleNewVideo("https://www.youtube.com/watch?v=" + id, time, null, s); handleNewVideo("https://www.youtube.com/watch?v=" + id, time, null);
} }
} catch (Exception e) { } catch (Exception e) {
@ -1371,7 +1373,7 @@ public class ResponseHelper {
if (video != null) { if (video != null) {
updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName()); updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName());
} else { } else {
handleNewVideo(info, time, null, s); handleNewVideo(info, time, null);
} }
} catch (Exception e) { } catch (Exception e) {
@ -1434,14 +1436,12 @@ public class ResponseHelper {
}); });
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try (Session sess = DatabaseSessionFactory.createSession()) {
for (StreamInfoItem item : info.getRelatedItems()) { for (StreamInfoItem item : info.getRelatedItems()) {
long time = item.getUploadDate() != null long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis(); : System.currentTimeMillis();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
handleNewVideo(item.getUrl(), time, channel, sess); handleNewVideo(item.getUrl(), time, channel);
}
} }
}); });