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())));
Multithreading.runAsync(() -> {
try (Session s = DatabaseSessionFactory.createSession()) {
for (var entry : feed.getEntries()) {
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();
}
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) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Channel> cr = cb.createQuery(Channel.class);
@ -69,6 +76,13 @@ public class DatabaseHelper {
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) {
CriteriaBuilder cb = s.getCriteriaBuilder();
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 {
handleNewVideo(StreamInfo.getInfo(url), time, channel, s);
handleNewVideo(StreamInfo.getInfo(url), time, channel);
} catch (Exception 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)
channel = DatabaseHelper.getChannelFromId(s,
channel = DatabaseHelper.getChannelFromId(
info.getUploaderUrl().substring("https://www.youtube.com/channel/".length()));
long infoTime = info.getUploadDate() != null ? info.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
@ -1329,15 +1329,17 @@ public class ResponseHelper {
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)) {
video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
Math.max(infoTime, time), info.getThumbnailUrl(), channel);
try (Session s = DatabaseSessionFactory.createSession()) {
var tr = s.beginTransaction();
s.persist(video);
tr.commit();
}
} else if (video != null) {
updateVideo(info.getId(), info, time);
@ -1353,7 +1355,7 @@ public class ResponseHelper {
if (video != null) {
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
} 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) {
@ -1371,7 +1373,7 @@ public class ResponseHelper {
if (video != null) {
updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName());
} else {
handleNewVideo(info, time, null, s);
handleNewVideo(info, time, null);
}
} catch (Exception e) {
@ -1434,14 +1436,12 @@ public class ResponseHelper {
});
Multithreading.runAsync(() -> {
try (Session sess = DatabaseSessionFactory.createSession()) {
for (StreamInfoItem item : info.getRelatedItems()) {
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
handleNewVideo(item.getUrl(), time, channel, sess);
}
handleNewVideo(item.getUrl(), time, channel);
}
});