-
Notifications
You must be signed in to change notification settings - Fork 58
add YoutubeProvider #35
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
Open
KaifTaufiq
wants to merge
7
commits into
recloudstream:master
Choose a base branch
from
KaifTaufiq:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+377
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e79906
bump gradle plugin version
KaifTaufiq ee5faa2
include NewPipeExtractor
KaifTaufiq a8cabdf
Add YoutubeProvider
KaifTaufiq b05865c
remove unwanted leftover Code
KaifTaufiq 48ea34c
handle Channel & Playlists
KaifTaufiq 491871b
Refactor mainPage to static definition
KaifTaufiq 6495b84
Limit pagination requests to prevent API overload
KaifTaufiq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Use an integer for version numbers | ||
| version = 1 | ||
|
|
||
| cloudstream { | ||
| // All of these properties are optional, you can safely remove any of them. | ||
|
|
||
| description = "Watch Youtube in Cloudstream" | ||
| authors = listOf("KaifTaufiq") | ||
|
|
||
| /** | ||
| * Status int as one of the following: | ||
| * 0: Down | ||
| * 1: Ok | ||
| * 2: Slow | ||
| * 3: Beta-only | ||
| **/ | ||
| status = 1 // Will be 3 if unspecified | ||
|
|
||
| tvTypes = listOf("Other", "Live", "TvSeries") | ||
| iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/YouTube_full-color_icon_%282017%29.svg/3840px-YouTube_full-color_icon_%282017%29.svg.png" | ||
|
|
||
| isCrossPlatform = true | ||
| } |
11 changes: 11 additions & 0 deletions
11
YoutubeProvider/src/main/kotlin/recloudstream/YoutubePlugin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package recloudstream | ||
|
|
||
| import com.lagradost.cloudstream3.plugins.BasePlugin | ||
| import com.lagradost.cloudstream3.plugins.CloudstreamPlugin | ||
|
|
||
| @CloudstreamPlugin | ||
| class YoutubePlugin : BasePlugin() { | ||
| override fun load() { | ||
| registerMainAPI(YoutubeProvider()) | ||
| } | ||
| } |
341 changes: 341 additions & 0 deletions
341
YoutubeProvider/src/main/kotlin/recloudstream/YoutubeProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,341 @@ | ||
| package recloudstream | ||
|
|
||
| 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 | ||
|
|
||
| class YoutubeProvider : MainAPI() { | ||
| override var mainUrl = "https://www.youtube.com" | ||
| override var name = "YouTube" | ||
| override var lang = "en" | ||
| override val hasMainPage = true | ||
| override val hasQuickSearch = true | ||
| override val supportedTypes = setOf( | ||
| TvType.Others, | ||
| TvType.Live, | ||
| TvType.TvSeries | ||
| ) | ||
|
|
||
| private val service = ServiceList.YouTube | ||
|
|
||
| // Clean, static definition of the main page tabs using the available kiosks | ||
| override val mainPage = mainPageOf( | ||
| "Trending" to "Trending", | ||
| "trending_movies_and_shows" to "Movies & Shows", | ||
| "trending_music" to "Music", | ||
| "trending_gaming" to "Gaming", | ||
| "trending_podcasts_episodes" to "Podcasts", | ||
| "live" to "Live" | ||
| ) | ||
|
|
||
| // Cache to store pagination state (nextPage tokens). | ||
| // Cloudstream requests sequential integers for pagination (page 1, 2, 3...), | ||
| // but NewPipe requires the specific token object from the previous page | ||
| // to fetch the next batch of results. | ||
| private val pageCache = mutableMapOf<String, org.schabi.newpipe.extractor.Page?>() | ||
| override suspend fun getMainPage( | ||
| page: Int, | ||
| request: MainPageRequest | ||
| ): HomePageResponse { | ||
| val key = request.data | ||
| if (page == 1) pageCache.remove(key) | ||
|
|
||
| val extractor = getKioskExtractor(request.data) | ||
|
|
||
| // val userCountry = Locale.getDefault().country | ||
| // extractor.forceContentCountry(ContentCountry(userCountry.ifBlank { "US" })) | ||
|
|
||
| val pageData = try { | ||
| if (page == 1) { | ||
| extractor.fetchPage() | ||
|
|
||
| extractor.initialPage.also { | ||
| pageCache[key] = it.nextPage | ||
| } | ||
| } else { | ||
| val next = pageCache[key] ?: return newHomePageResponse(emptyList(), false) | ||
| extractor.getPage(next).also { | ||
| pageCache[key] = it.nextPage | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| return newHomePageResponse(emptyList(), false) | ||
| } | ||
|
|
||
| val results = pageData.items.map { | ||
| it.toSearchResponse() | ||
| } | ||
|
|
||
| val headerName = try { | ||
| extractor.name.ifEmpty { request.name } | ||
| } catch (e: Exception) { | ||
| request.name | ||
| }.ifEmpty { "Trending" } | ||
|
|
||
| return newHomePageResponse( | ||
| listOf( | ||
| HomePageList( | ||
| headerName, | ||
| results, | ||
| true | ||
| ) | ||
| ), | ||
| pageData.hasNextPage() | ||
| ) | ||
| } | ||
|
|
||
| private val searchPageCache = mutableMapOf<String, org.schabi.newpipe.extractor.Page?>() | ||
| override suspend fun search(query: String, page: Int): SearchResponseList { | ||
| val extractor = service.getSearchExtractor(query) | ||
|
|
||
| // Localisation to be handled later | ||
| // extractor.forceContentCountry(ContentCountry(Locale.getDefault().country)) | ||
|
|
||
| val pageData = if (!searchPageCache.containsKey(query)) { | ||
| extractor.fetchPage() | ||
| extractor.initialPage.also { | ||
| searchPageCache[query] = it.nextPage | ||
| } | ||
| } else { | ||
| val next = searchPageCache[query] ?: return newSearchResponseList(emptyList(), false) | ||
| extractor.getPage(next).also { | ||
| searchPageCache[query] = it.nextPage | ||
| } | ||
| } | ||
|
|
||
| val results = pageData.items.map { | ||
| it.toSearchResponse() | ||
| } | ||
|
|
||
| return newSearchResponseList( | ||
| results, | ||
| pageData.hasNextPage() | ||
| ) | ||
| } | ||
|
|
||
| private fun getKioskExtractor(kioskId: String?): KioskExtractor<out InfoItem> { | ||
| return if (kioskId.isNullOrBlank()) { | ||
| service.kioskList.getDefaultKioskExtractor(null) | ||
| } else { | ||
| service.kioskList.getExtractorById(kioskId, null) | ||
| } | ||
| } | ||
|
|
||
| private fun InfoItem.toSearchResponse(): SearchResponse { | ||
| return newMovieSearchResponse( | ||
| name ?: "Unknown", | ||
| url ?: "", | ||
| TvType.Others | ||
| ) { | ||
| posterUrl = thumbnails.lastOrNull()?.url | ||
| } | ||
| } | ||
|
|
||
| override suspend fun load(url: String): LoadResponse { | ||
| val urlType = getUrlType(url) | ||
|
|
||
| return when (urlType) { | ||
| UrlType.Video -> loadVideo(url) | ||
| UrlType.Channel -> loadChannel(url) | ||
| UrlType.Playlist -> loadPlaylist(url) | ||
| UrlType.Unknown -> throw RuntimeException("Unsupported YouTube URL") | ||
| } | ||
| } | ||
|
|
||
| private enum class UrlType { | ||
| Video, Channel, Playlist, Unknown | ||
| } | ||
|
|
||
| private fun getUrlType(url: String): UrlType { | ||
| return when { | ||
| url.contains("/watch?v=") || url.contains("youtu.be/") -> UrlType.Video | ||
| url.contains("/channel/") || url.contains("/@") || url.contains("/c/") -> UrlType.Channel | ||
| url.contains("/playlist?list=") || url.contains("/watch?v=") && url.contains("&list=") -> UrlType.Playlist | ||
| else -> UrlType.Unknown | ||
| } | ||
| } | ||
|
|
||
| private suspend fun loadVideo(url: String): LoadResponse { | ||
| val extractor = ServiceList.YouTube.getStreamExtractor(url) | ||
| extractor.fetchPage() | ||
|
|
||
| val info = StreamInfo.getInfo(extractor) | ||
|
|
||
| return newMovieLoadResponse( | ||
| info.name, | ||
| url, | ||
| if (info.streamType?.name?.contains("LIVE") == true) | ||
| TvType.Live else TvType.Others, | ||
| url | ||
| ) { | ||
| plot = info.description.content.toString() | ||
| posterUrl = info.thumbnails.lastOrNull()?.url | ||
| duration = info.duration.toInt() | ||
|
|
||
| info.uploaderName?.takeIf { it.isNotBlank() }?.let { uploader -> | ||
| actors = listOf( | ||
| ActorData( | ||
| Actor( | ||
| uploader, | ||
| info.uploaderAvatars.lastOrNull()?.url ?: "" | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| tags = info.tags?.take(5)?.toList() | ||
| } | ||
| } | ||
|
|
||
| private suspend fun loadChannel(url: String): LoadResponse { | ||
| val extractor = ServiceList.YouTube.getChannelExtractor(url) | ||
| extractor.fetchPage() | ||
|
|
||
| val channelName = extractor.name | ||
| val channelDescription = extractor.description | ||
| val channelAvatar = extractor.avatars.lastOrNull()?.url | ||
| val channelBanner = extractor.banners.lastOrNull()?.url | ||
|
|
||
| val tabs = extractor.tabs | ||
| val videosTab = tabs.firstOrNull { it.url.contains("/videos") } ?: tabs.firstOrNull() | ||
| ?: throw RuntimeException("No videos tab found") | ||
|
|
||
| val videosExtractor = ServiceList.YouTube.getChannelTabExtractor(videosTab) | ||
| 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 | ||
| } | ||
| }) | ||
|
|
||
| // 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 | ||
| } | ||
| }) | ||
| 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 | ||
| // } | ||
| // }) | ||
| // } | ||
|
|
||
| return newTvSeriesLoadResponse( | ||
| channelName, | ||
| url, | ||
| TvType.TvSeries, | ||
| episodes | ||
| ) { | ||
| plot = channelDescription | ||
| posterUrl = channelBanner | ||
| backgroundPosterUrl = channelBanner | ||
| tags = listOf("Channel") | ||
| actors = listOf( | ||
| ActorData( | ||
| Actor( | ||
| channelName, | ||
| channelAvatar ?: "" | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun loadPlaylist(url: String): LoadResponse { | ||
| val extractor = ServiceList.YouTube.getPlaylistExtractor(url) | ||
| extractor.fetchPage() | ||
|
|
||
| val playlistName = extractor.name | ||
| val playlistDescription = extractor.description.content.toString() | ||
| val playlistThumbnail = extractor.thumbnails.lastOrNull()?.url | ||
| val uploaderName = extractor.uploaderName | ||
|
|
||
| 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 | ||
| } | ||
| }) | ||
|
|
||
| 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 | ||
| } | ||
| }) | ||
| pagesLoaded++ | ||
| } | ||
|
|
||
| // while (page.hasNextPage()) { | ||
| // page = extractor.getPage(page.nextPage) | ||
| // episodes.addAll(page.items.map { item -> | ||
| // newEpisode(item.url) { | ||
| // name = item.name | ||
| // posterUrl = item.thumbnails.lastOrNull()?.url | ||
| // } | ||
| // }) | ||
| // } | ||
|
|
||
| return newTvSeriesLoadResponse( | ||
| playlistName, | ||
| url, | ||
| TvType.TvSeries, | ||
| episodes | ||
| ) { | ||
| plot = playlistDescription | ||
| posterUrl = playlistThumbnail | ||
| tags = if (uploaderName.isNotBlank()) listOf("Channel: $uploaderName") else listOf("Playlist") | ||
| if (uploaderName.isNotBlank()) { | ||
| actors = listOf( | ||
| ActorData( | ||
| Actor( | ||
| uploaderName, | ||
| extractor.uploaderAvatars.lastOrNull()?.url ?: "" | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun loadLinks( | ||
| data: String, | ||
| isCasting: Boolean, | ||
| subtitleCallback: (SubtitleFile) -> Unit, | ||
| callback: (ExtractorLink) -> Unit | ||
| ): Boolean { | ||
| return loadExtractor( | ||
| "https://youtube.com/watch?v=$data", | ||
| subtitleCallback, | ||
| callback | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.