Merge pull request #689 from TeamPiped/channel-save-video

Fix channel save new video handling
This commit is contained in:
Kavin 2023-08-30 13:43:22 +00:00 committed by GitHub
commit 27350d2a47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 7 deletions

View File

@ -210,6 +210,57 @@ public class ChannelHandlers {
List<ContentItem> items = collectRelatedItems(info.getRelatedItems());
Multithreading.runAsync(() -> {
var channel = DatabaseHelper.getChannelFromId(info.getId());
if (channel != null) {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
var streamInfoItems = info.getRelatedItems()
.stream()
.parallel()
.filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast)
.toList();
var channelIds = streamInfoItems
.stream()
.map(item -> {
try {
return YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl());
} catch (ParsingException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toUnmodifiableSet());
List<String> videoIdsPresent = DatabaseHelper.getVideosFromIds(s, channelIds)
.stream()
.map(Video::getId)
.toList();
streamInfoItems
.stream()
.parallel()
.forEach(item -> {
try {
String id = YOUTUBE_SERVICE.getStreamLHFactory().getId(item.getUrl());
if (videoIdsPresent.contains(id))
VideoHelpers.updateVideo(id, item);
else if (item.getUploadDate() != null) {
// shorts tab doesn't have upload date
// we don't want to fetch each video's upload date
long time = item.getUploadDate().offsetDateTime().toInstant().toEpochMilli();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
VideoHelpers.handleNewVideo(item.getUrl(), time, channel);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
});
String nextpage = null;
if (info.hasNextPage()) {
Page page = info.getNextPage();

View File

@ -214,9 +214,11 @@ public class DatabaseHelper {
CollectionUtils.collectPreloadedTabs(info.getTabs())
.stream()
.parallel()
.map(tab -> {
.mapMulti((tab, consumer) -> {
try {
return ChannelTabInfo.getInfo(YOUTUBE_SERVICE, tab).getRelatedItems();
ChannelTabInfo.getInfo(YOUTUBE_SERVICE, tab)
.getRelatedItems()
.forEach(consumer);
} catch (ExtractionException | IOException e) {
throw new RuntimeException(e);
}
@ -224,11 +226,11 @@ public class DatabaseHelper {
.filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast)
.forEach(item -> {
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
VideoHelpers.handleNewVideo(item.getUrl(), time, channel);
long time = item.getUploadDate() != null
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
: System.currentTimeMillis();
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
VideoHelpers.handleNewVideo(item.getUrl(), time, channel);
});
});