Skip to content

Commit

Permalink
refactor(StreamsExtractor): improve local feed extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
FineFindus committed Nov 9, 2024
1 parent 3be7d9e commit 42f4589
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions app/src/main/java/com/github/libretube/api/StreamsExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ import com.github.libretube.api.obj.Subtitle
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.NewPipeDownloaderImpl
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import kotlinx.coroutines.withContext
import kotlinx.datetime.toKotlinInstant
import org.schabi.newpipe.extractor.NewPipe
import org.schabi.newpipe.extractor.ServiceList
Expand Down Expand Up @@ -53,16 +57,18 @@ object StreamsExtractor {
// NewPipe.getService(ServiceList.YouTube.serviceId)
// }

private const val MAX_CONCURRENT_REQUESTS = 32

init {
NewPipe.init(NewPipeDownloaderImpl())
}

suspend fun extractFeed(subscriptions: List<String>): List<StreamItem> {
val maxConcurrentRequests = 15
val semaphore = Semaphore(maxConcurrentRequests)
val semaphore = Semaphore(MAX_CONCURRENT_REQUESTS)
val service = NewPipe.getService(ServiceList.YouTube.serviceId)
return runBlocking {
val feed = coroutineScope {
subscriptions.map { subscription ->
async {
try {
semaphore.withPermit {
extractSubscription(service, subscription)
Expand All @@ -72,34 +78,39 @@ object StreamsExtractor {
emptyList()
}
}
}.flatten().sortedByDescending { it.uploaded }
}
}.awaitAll().flatten().sortedByDescending { it.uploaded }
return feed;
}

private fun extractSubscription(service: StreamingService, subscription: String): List<StreamItem> {
val channelInfo = ChannelInfo.getInfo(
service,
"${ShareDialog.YOUTUBE_FRONTEND_URL}/channel/$subscription"
)
val videosTab = channelInfo.tabs.firstOrNull { it.contentFilters.contains(ChannelTabs.VIDEOS) } ?: return emptyList()
val tabInfo = ChannelTabInfo.getInfo(service, videosTab)
return tabInfo.relatedItems.filterIsInstance<StreamInfoItem>().take(20).map {
StreamItem(
url = it.url.replace(ShareDialog.YOUTUBE_FRONTEND_URL, ""),
type = StreamItem.TYPE_STREAM,
title = it.name,
thumbnail = it.thumbnails.lastOrNull()?.url,
uploaderName = it.uploaderName,
uploaderUrl = it.uploaderUrl,
uploaderAvatar = channelInfo.avatars.lastOrNull()?.url,
uploadedDate = it.uploadDate?.offsetDateTime().toString(),
duration = it.duration,
views = it.viewCount,
uploaderVerified = it.isUploaderVerified,
uploaded = it.uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0L,
shortDescription = it.shortDescription,
isShort = it.isShortFormContent

private suspend fun extractSubscription(service: StreamingService, subscription: String): List<StreamItem> {
return withContext(Dispatchers.IO) {
val channelInfo = ChannelInfo.getInfo(
service,
"${ShareDialog.YOUTUBE_FRONTEND_URL}/channel/$subscription"
)
val videosTab = channelInfo.tabs.firstOrNull { it.contentFilters.contains(ChannelTabs.VIDEOS) }
?: return@withContext emptyList()
val tabInfo = ChannelTabInfo.getInfo(service, videosTab)

tabInfo.relatedItems.filterIsInstance<StreamInfoItem>().take(5).map {
StreamItem(
url = it.url.replace(ShareDialog.YOUTUBE_FRONTEND_URL, ""),
type = StreamItem.TYPE_STREAM,
title = it.name,
thumbnail = it.thumbnails.lastOrNull()?.url,
uploaderName = it.uploaderName,
uploaderUrl = it.uploaderUrl,
uploaderAvatar = channelInfo.avatars.lastOrNull()?.url,
uploadedDate = it.uploadDate?.offsetDateTime().toString(),
duration = it.duration,
views = it.viewCount,
uploaderVerified = it.isUploaderVerified,
uploaded = it.uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0L,
shortDescription = it.shortDescription,
isShort = it.isShortFormContent
)
}
}
}

Expand Down

0 comments on commit 42f4589

Please sign in to comment.