mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
Merge pull request #6945 from Bnyro/master
feat(LocalFeedExtraction): check if there are any new streams before refreshing channel
This commit is contained in:
commit
d987522697
@ -14,6 +14,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.ChannelInfo
|
||||||
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabInfo
|
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabInfo
|
||||||
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabs
|
import org.schabi.newpipe.extractor.channel.tabs.ChannelTabs
|
||||||
|
import org.schabi.newpipe.extractor.feed.FeedInfo
|
||||||
import org.schabi.newpipe.extractor.stream.StreamInfoItem
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
@ -41,9 +42,9 @@ class LocalFeedRepository : FeedRepository {
|
|||||||
val oneDayAgo = nowMillis - Duration.ofDays(1).toMillis()
|
val oneDayAgo = nowMillis - Duration.ofDays(1).toMillis()
|
||||||
|
|
||||||
// only refresh if feed is empty or last refresh was more than a day ago
|
// 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)
|
PreferenceHelper.getLong(PreferenceKeys.LAST_FEED_REFRESH_TIMESTAMP_MILLIS, 0)
|
||||||
if (feed.isNotEmpty() && lastRefresh > oneDayAgo) {
|
if (feed.isNotEmpty() && lastRefreshMillis > oneDayAgo) {
|
||||||
return DatabaseHolder.Database.feedDao().getAll()
|
return DatabaseHolder.Database.feedDao().getAll()
|
||||||
.map(SubscriptionsFeedItem::toStreamItem)
|
.map(SubscriptionsFeedItem::toStreamItem)
|
||||||
}
|
}
|
||||||
@ -60,20 +61,29 @@ class LocalFeedRepository : FeedRepository {
|
|||||||
for (channelIdChunk in channelIds.chunked(CHUNK_SIZE)) {
|
for (channelIdChunk in channelIds.chunked(CHUNK_SIZE)) {
|
||||||
val collectedFeedItems = channelIdChunk.parallelMap { channelId ->
|
val collectedFeedItems = channelIdChunk.parallelMap { channelId ->
|
||||||
try {
|
try {
|
||||||
getRelatedStreams(channelId)
|
getRelatedStreams(channelId, minimumDateMillis)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(channelId, e.stackTraceToString())
|
Log.e(channelId, e.stackTraceToString())
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}.filterNotNull().flatten().map(StreamItem::toFeedItem)
|
}.filterNotNull().flatten().map(StreamItem::toFeedItem)
|
||||||
.filter { it.uploaded > minimumDateMillis }
|
|
||||||
|
|
||||||
DatabaseHolder.Database.feedDao().insertAll(collectedFeedItems)
|
DatabaseHolder.Database.feedDao().insertAll(collectedFeedItems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun getRelatedStreams(channelId: String): List<StreamItem> {
|
private suspend fun getRelatedStreams(channelId: String, minimumDateMillis: Long): List<StreamItem> {
|
||||||
val channelInfo = ChannelInfo.getInfo("$YOUTUBE_FRONTEND_URL/channel/${channelId}")
|
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 ->
|
val relevantInfoTabs = channelInfo.tabs.filter { tab ->
|
||||||
relevantTabs.any { tab.contentFilters.contains(it) }
|
relevantTabs.any { tab.contentFilters.contains(it) }
|
||||||
}
|
}
|
||||||
@ -102,7 +112,7 @@ class LocalFeedRepository : FeedRepository {
|
|||||||
shortDescription = item.shortDescription,
|
shortDescription = item.shortDescription,
|
||||||
isShort = item.isShortFormContent
|
isShort = item.isShortFormContent
|
||||||
)
|
)
|
||||||
}
|
}.filter { it.uploaded > minimumDateMillis }
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user