Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(LocalFeedExtraction): check if there are any new streams before refreshing channel #6945

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_UR
import org.schabi.newpipe.extractor.channel.ChannelInfo
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabInfo
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabs
import org.schabi.newpipe.extractor.feed.FeedInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem
import java.time.Duration
import java.time.Instant
Expand All @@ -34,9 +35,9 @@ class LocalFeedRepository : FeedRepository {
val oneDayAgo = nowMillis - Duration.ofDays(1).toMillis()

// only refresh if feed is empty or last refresh was more than a day ago
val lastRefresh =
val lastRefreshMillis =
PreferenceHelper.getLong(PreferenceKeys.LAST_FEED_REFRESH_TIMESTAMP_MILLIS, 0)
if (feed.isNotEmpty() && lastRefresh > oneDayAgo) {
if (feed.isNotEmpty() && lastRefreshMillis > oneDayAgo) {
return DatabaseHolder.Database.feedDao().getAll()
.map(SubscriptionsFeedItem::toStreamItem)
}
Expand All @@ -53,20 +54,29 @@ class LocalFeedRepository : FeedRepository {
for (channelIdChunk in channelIds.chunked(CHUNK_SIZE)) {
val collectedFeedItems = channelIdChunk.parallelMap { channelId ->
try {
getRelatedStreams(channelId)
getRelatedStreams(channelId, minimumDateMillis)
} catch (e: Exception) {
Log.e(channelId, e.stackTraceToString())
null
}
}.filterNotNull().flatten().map(StreamItem::toFeedItem)
.filter { it.uploaded > minimumDateMillis }

DatabaseHolder.Database.feedDao().insertAll(collectedFeedItems)
}
}

private suspend fun getRelatedStreams(channelId: String): List<StreamItem> {
val channelInfo = ChannelInfo.getInfo("$YOUTUBE_FRONTEND_URL/channel/${channelId}")
private suspend fun getRelatedStreams(channelId: String, minimumDateMillis: Long): List<StreamItem> {
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
val feedInfo = FeedInfo.getInfo(channelUrl)

val hasNewerUploads = feedInfo.relatedItems.any {
(it.uploadDate?.offsetDateTime()?.toInstant()?.toEpochMilli()
?: 0) > minimumDateMillis
}
if (!hasNewerUploads) return emptyList()

val channelInfo = ChannelInfo.getInfo(channelUrl)

val relevantInfoTabs = channelInfo.tabs.filter { tab ->
relevantTabs.any { tab.contentFilters.contains(it) }
}
Expand Down Expand Up @@ -95,7 +105,7 @@ class LocalFeedRepository : FeedRepository {
shortDescription = item.shortDescription,
isShort = item.isShortFormContent
)
}
}.filter { it.uploaded > minimumDateMillis }
}

companion object {
Expand Down
Loading