Skip to content

Commit d987522

Browse files
authored
Merge pull request #6945 from Bnyro/master
feat(LocalFeedExtraction): check if there are any new streams before refreshing channel
2 parents b50661d + 48f230e commit d987522

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_UR
1414
import org.schabi.newpipe.extractor.channel.ChannelInfo
1515
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabInfo
1616
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabs
17+
import org.schabi.newpipe.extractor.feed.FeedInfo
1718
import org.schabi.newpipe.extractor.stream.StreamInfoItem
1819
import java.time.Duration
1920
import java.time.Instant
@@ -41,9 +42,9 @@ class LocalFeedRepository : FeedRepository {
4142
val oneDayAgo = nowMillis - Duration.ofDays(1).toMillis()
4243

4344
// only refresh if feed is empty or last refresh was more than a day ago
44-
val lastRefresh =
45+
val lastRefreshMillis =
4546
PreferenceHelper.getLong(PreferenceKeys.LAST_FEED_REFRESH_TIMESTAMP_MILLIS, 0)
46-
if (feed.isNotEmpty() && lastRefresh > oneDayAgo) {
47+
if (feed.isNotEmpty() && lastRefreshMillis > oneDayAgo) {
4748
return DatabaseHolder.Database.feedDao().getAll()
4849
.map(SubscriptionsFeedItem::toStreamItem)
4950
}
@@ -60,20 +61,29 @@ class LocalFeedRepository : FeedRepository {
6061
for (channelIdChunk in channelIds.chunked(CHUNK_SIZE)) {
6162
val collectedFeedItems = channelIdChunk.parallelMap { channelId ->
6263
try {
63-
getRelatedStreams(channelId)
64+
getRelatedStreams(channelId, minimumDateMillis)
6465
} catch (e: Exception) {
6566
Log.e(channelId, e.stackTraceToString())
6667
null
6768
}
6869
}.filterNotNull().flatten().map(StreamItem::toFeedItem)
69-
.filter { it.uploaded > minimumDateMillis }
7070

7171
DatabaseHolder.Database.feedDao().insertAll(collectedFeedItems)
7272
}
7373
}
7474

75-
private suspend fun getRelatedStreams(channelId: String): List<StreamItem> {
76-
val channelInfo = ChannelInfo.getInfo("$YOUTUBE_FRONTEND_URL/channel/${channelId}")
75+
private suspend fun getRelatedStreams(channelId: String, minimumDateMillis: Long): List<StreamItem> {
76+
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
77+
val feedInfo = FeedInfo.getInfo(channelUrl)
78+
79+
val hasNewerUploads = feedInfo.relatedItems.any {
80+
(it.uploadDate?.offsetDateTime()?.toInstant()?.toEpochMilli()
81+
?: 0) > minimumDateMillis
82+
}
83+
if (!hasNewerUploads) return emptyList()
84+
85+
val channelInfo = ChannelInfo.getInfo(channelUrl)
86+
7787
val relevantInfoTabs = channelInfo.tabs.filter { tab ->
7888
relevantTabs.any { tab.contentFilters.contains(it) }
7989
}
@@ -102,7 +112,7 @@ class LocalFeedRepository : FeedRepository {
102112
shortDescription = item.shortDescription,
103113
isShort = item.isShortFormContent
104114
)
105-
}
115+
}.filter { it.uploaded > minimumDateMillis }
106116
}
107117

108118
companion object {

0 commit comments

Comments
 (0)