Skip to content
Open
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
51 changes: 21 additions & 30 deletions YoutubeProvider/src/main/kotlin/recloudstream/YoutubeProvider.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package recloudstream

import android.os.Build
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.utils.*
import org.schabi.newpipe.extractor.ServiceList
import org.schabi.newpipe.extractor.kiosk.KioskExtractor
import org.schabi.newpipe.extractor.InfoItem
//import org.schabi.newpipe.extractor.localization.ContentCountry
import org.schabi.newpipe.extractor.stream.StreamInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem

class YoutubeProvider : MainAPI() {
override var mainUrl = "https://www.youtube.com"
Expand Down Expand Up @@ -191,6 +193,20 @@ class YoutubeProvider : MainAPI() {
}
}

private fun StreamInfoItem.toEpisode(): Episode {
return newEpisode(url) {
name = this@toEpisode.name
description = this@toEpisode.shortDescription
posterUrl = thumbnails.lastOrNull()?.url
runTime = this@toEpisode.duration.toInt() / 60

// toEpochSecond only works on Android SDK 26+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
date = this@toEpisode.uploadDate?.instant?.toEpochMilli()
Comment on lines +204 to +205

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't particularly like this for 2 reasons:

  • toEpochMilli (and all other java.time methods) only work on Android SDK 26+, so we currently don't show the date for SDK < 26, which is ugly.
  • I wanted to use addDate, but it only works with kotlinx.datetime types, not java.time types as we get from NewPipeExtractor

}
}
}

private suspend fun loadChannel(url: String): LoadResponse {
val extractor = ServiceList.YouTube.getChannelExtractor(url)
extractor.fetchPage()
Expand All @@ -208,36 +224,21 @@ class YoutubeProvider : MainAPI() {
val episodes = mutableListOf<Episode>()

var page = videosExtractor.initialPage
episodes.addAll(page.items.map { item ->
newEpisode(item.url) {
name = item.name
posterUrl = item.thumbnails.lastOrNull()?.url
}
})
episodes.addAll(page.items.filterIsInstance<StreamInfoItem>().map { it.toEpisode() })

// Limit the number of pages fetched to prevent massive API overhead
var pagesLoaded = 1
val maxPagesToLoad = 5

while (page.hasNextPage() && pagesLoaded < maxPagesToLoad) {
page = videosExtractor.getPage(page.nextPage)
episodes.addAll(page.items.map { item ->
newEpisode(item.url) {
name = item.name
posterUrl = item.thumbnails.lastOrNull()?.url
}
})
episodes.addAll(page.items.filterIsInstance<StreamInfoItem>().map { it.toEpisode() })
pagesLoaded++
}

// while (page.hasNextPage()) {
// page = videosExtractor.getPage(page.nextPage)
// episodes.addAll(page.items.map { item ->
// newEpisode(item.url) {
// name = item.name
// posterUrl = item.thumbnails.lastOrNull()?.url
// }
// })
// episodes.addAll(page.items.filterIsInstance<StreamInfoItem>().map { it.toEpisode() })
// }

return newTvSeriesLoadResponse(
Expand Down Expand Up @@ -273,24 +274,14 @@ class YoutubeProvider : MainAPI() {
val episodes = mutableListOf<Episode>()

var page = extractor.getInitialPage()
episodes.addAll(page.items.map { item ->
newEpisode(item.url) {
name = item.name
posterUrl = item.thumbnails.lastOrNull()?.url
}
})
episodes.addAll(page.items.map { it.toEpisode() })

var pagesLoaded = 1
val maxPagesToLoad = 5

while (page.hasNextPage() && pagesLoaded < maxPagesToLoad) {
page = extractor.getPage(page.nextPage)
episodes.addAll(page.items.map { item ->
newEpisode(item.url) {
name = item.name
posterUrl = item.thumbnails.lastOrNull()?.url
}
})
episodes.addAll(page.items.map { it.toEpisode() })
pagesLoaded++
}

Expand Down