cloudstream/app/src/main/java/com/lagradost/cloudstream3/metaproviders/TmdbProvider.kt

365 lines
14 KiB
Kotlin
Raw Normal View History

2021-11-28 16:10:19 +00:00
package com.lagradost.cloudstream3.metaproviders
import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.*
2022-02-05 23:58:56 +00:00
import com.lagradost.cloudstream3.LoadResponse.Companion.addActors
2022-04-03 21:41:28 +00:00
import com.lagradost.cloudstream3.LoadResponse.Companion.addImdbId
2022-06-16 01:04:24 +00:00
import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer
2021-11-28 16:10:19 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.uwetrottmann.tmdb2.Tmdb
import com.uwetrottmann.tmdb2.entities.*
import com.uwetrottmann.tmdb2.enumerations.AppendToResponseItem
2022-06-16 01:04:24 +00:00
import com.uwetrottmann.tmdb2.enumerations.VideoType
2022-01-31 19:39:24 +00:00
import retrofit2.awaitResponse
2021-11-28 16:10:19 +00:00
import java.util.*
/**
* episode and season starting from 1
* they are null if movie
* */
data class TmdbLink(
@JsonProperty("imdbID") val imdbID: String?,
@JsonProperty("tmdbID") val tmdbID: Int?,
@JsonProperty("episode") val episode: Int?,
2022-01-31 19:39:24 +00:00
@JsonProperty("season") val season: Int?,
@JsonProperty("movieName") val movieName: String? = null,
)
2021-11-28 16:10:19 +00:00
open class TmdbProvider : MainAPI() {
2022-06-15 17:47:07 +00:00
// This should always be false, but might as well make it easier for forks
open val includeAdult = false
2021-11-28 16:10:19 +00:00
// Use the LoadResponse from the metadata provider
2021-11-28 16:10:19 +00:00
open val useMetaLoadResponse = false
open val apiName = "TMDB"
// As some sites doesn't support s0
open val disableSeasonZero = true
2021-11-28 16:10:19 +00:00
override val hasMainPage = true
override val providerType = ProviderType.MetaProvider
// Fuck it, public private api key because github actions won't co-operate.
// Please no stealy.
2021-12-13 18:41:33 +00:00
private val tmdb = Tmdb("e6333b32409e02a4a6eba6fb7ff866bb")
2021-11-28 16:10:19 +00:00
private fun getImageUrl(link: String?): String? {
if (link == null) return null
return if (link.startsWith("/")) "https://image.tmdb.org/t/p/w500/$link" else link
}
private fun getUrl(id: Int?, tvShow: Boolean): String {
return if (tvShow) "https://www.themoviedb.org/tv/${id ?: -1}"
else "https://www.themoviedb.org/movie/${id ?: -1}"
}
private fun BaseTvShow.toSearchResponse(): TvSeriesSearchResponse {
return TvSeriesSearchResponse(
this.name ?: this.original_name,
getUrl(id, true),
apiName,
TvType.TvSeries,
getImageUrl(this.poster_path),
this.first_air_date?.let {
Calendar.getInstance().apply {
time = it
}.get(Calendar.YEAR)
},
null,
this.id
)
}
private fun BaseMovie.toSearchResponse(): MovieSearchResponse {
return MovieSearchResponse(
this.title ?: this.original_title,
getUrl(id, false),
apiName,
TvType.TvSeries,
getImageUrl(this.poster_path),
this.release_date?.let {
Calendar.getInstance().apply {
time = it
}.get(Calendar.YEAR)
},
this.id,
)
}
private fun List<CastMember?>?.toActors(): List<Pair<Actor, String?>>? {
return this?.mapNotNull {
Pair(
Actor(it?.name ?: return@mapNotNull null, getImageUrl(it.profile_path)),
it.character
)
}
}
2022-06-17 17:25:41 +00:00
private suspend fun TvShow.toLoadResponse(): TvSeriesLoadResponse {
val episodes = this.seasons?.filter { !disableSeasonZero || (it.season_number ?: 0) != 0 }
2021-12-13 18:41:33 +00:00
?.mapNotNull { season ->
season.episodes?.map { episode ->
Episode(
TmdbLink(
2021-12-13 18:41:33 +00:00
episode.external_ids?.imdb_id ?: this.external_ids?.imdb_id,
this.id,
2021-12-13 18:41:33 +00:00
episode.episode_number,
episode.season_number,
).toJson(),
episode.name,
episode.season_number,
episode.episode_number,
2021-12-13 18:41:33 +00:00
getImageUrl(episode.still_path),
episode.rating,
episode.overview,
episode.air_date?.time,
)
2021-12-13 18:41:33 +00:00
} ?: (1..(season.episode_count ?: 1)).map { episodeNum ->
Episode(
episode = episodeNum,
data = TmdbLink(
this.external_ids?.imdb_id,
this.id,
episodeNum,
2021-12-13 18:41:33 +00:00
season.season_number,
).toJson(),
2021-12-13 18:41:33 +00:00
season = season.season_number
)
}
}?.flatten() ?: listOf()
return newTvSeriesLoadResponse(
2021-11-28 16:10:19 +00:00
this.name ?: this.original_name,
getUrl(id, true),
TvType.TvSeries,
episodes
) {
posterUrl = getImageUrl(poster_path)
year = first_air_date?.let {
2021-11-28 16:10:19 +00:00
Calendar.getInstance().apply {
time = it
}.get(Calendar.YEAR)
}
plot = overview
2022-04-03 21:41:28 +00:00
addImdbId(external_ids?.imdb_id)
tags = genres?.mapNotNull { it.name }
duration = episode_run_time?.average()?.toInt()
rating = this@toLoadResponse.rating
2022-06-16 01:04:24 +00:00
addTrailer(videos.toTrailers())
recommendations = (this@toLoadResponse.recommendations
?: this@toLoadResponse.similar)?.results?.map { it.toSearchResponse() }
2022-02-05 23:58:56 +00:00
addActors(credits?.cast?.toList().toActors())
}
2021-11-28 16:10:19 +00:00
}
2022-06-16 01:04:24 +00:00
private fun Videos?.toTrailers(): List<String>? {
return this?.results?.filter { it.type != VideoType.OPENING_CREDITS && it.type != VideoType.FEATURETTE }
?.sortedBy { it.type?.ordinal ?: 10000 }
?.mapNotNull {
when (it.site?.trim()?.lowercase()) {
"youtube" -> { // TODO FILL SITES
"https://www.youtube.com/watch?v=${it.key}"
}
else -> null
}
}
}
2022-06-17 17:25:41 +00:00
private suspend fun Movie.toLoadResponse(): MovieLoadResponse {
return newMovieLoadResponse(
this.title ?: this.original_title, getUrl(id, false), TvType.Movie, TmdbLink(
2021-11-28 16:10:19 +00:00
this.imdb_id,
this.id,
null,
null,
2022-01-31 19:39:24 +00:00
this.title ?: this.original_title,
).toJson()
) {
posterUrl = getImageUrl(poster_path)
year = release_date?.let {
2021-11-28 16:10:19 +00:00
Calendar.getInstance().apply {
time = it
}.get(Calendar.YEAR)
}
plot = overview
2022-04-03 21:41:28 +00:00
addImdbId(external_ids?.imdb_id)
tags = genres?.mapNotNull { it.name }
duration = runtime
rating = this@toLoadResponse.rating
2022-06-16 01:04:24 +00:00
addTrailer(videos.toTrailers())
recommendations = (this@toLoadResponse.recommendations
?: this@toLoadResponse.similar)?.results?.map { it.toSearchResponse() }
2022-02-05 23:58:56 +00:00
addActors(credits?.cast?.toList().toActors())
}
2021-11-28 16:10:19 +00:00
}
2022-01-16 22:31:42 +00:00
override suspend fun getMainPage(): HomePageResponse {
2021-11-28 16:10:19 +00:00
// SAME AS DISCOVER IT SEEMS
// val popularSeries = tmdb.tvService().popular(1, "en-US").execute().body()?.results?.map {
// it.toSearchResponse()
// } ?: listOf()
//
// val popularMovies =
// tmdb.moviesService().popular(1, "en-US", "840").execute().body()?.results?.map {
// it.toSearchResponse()
// } ?: listOf()
2022-01-31 19:39:24 +00:00
var discoverMovies: List<MovieSearchResponse> = listOf()
var discoverSeries: List<TvSeriesSearchResponse> = listOf()
var topMovies: List<MovieSearchResponse> = listOf()
var topSeries: List<TvSeriesSearchResponse> = listOf()
argamap(
{
discoverMovies = tmdb.discoverMovie().build().awaitResponse().body()?.results?.map {
it.toSearchResponse()
} ?: listOf()
}, {
discoverSeries = tmdb.discoverTv().build().awaitResponse().body()?.results?.map {
it.toSearchResponse()
} ?: listOf()
}, {
// https://en.wikipedia.org/wiki/ISO_3166-1
topMovies =
tmdb.moviesService().topRated(1, "en-US", "US").awaitResponse()
.body()?.results?.map {
it.toSearchResponse()
} ?: listOf()
}, {
topSeries =
tmdb.tvService().topRated(1, "en-US").awaitResponse().body()?.results?.map {
it.toSearchResponse()
} ?: listOf()
}
)
2021-11-28 16:10:19 +00:00
return HomePageResponse(
listOf(
// HomePageList("Popular Series", popularSeries),
// HomePageList("Popular Movies", popularMovies),
HomePageList("Popular Movies", discoverMovies),
HomePageList("Popular Series", discoverSeries),
HomePageList("Top Movies", topMovies),
HomePageList("Top Series", topSeries),
)
)
}
open fun loadFromImdb(imdb: String, seasons: List<TvSeason>): LoadResponse? {
return null
}
open fun loadFromTmdb(tmdb: Int, seasons: List<TvSeason>): LoadResponse? {
return null
}
open fun loadFromImdb(imdb: String): LoadResponse? {
return null
}
open fun loadFromTmdb(tmdb: Int): LoadResponse? {
return null
}
// Possible to add recommendations and such here.
2022-01-16 22:31:42 +00:00
override suspend fun load(url: String): LoadResponse? {
2021-11-28 16:10:19 +00:00
// https://www.themoviedb.org/movie/7445-brothers
// https://www.themoviedb.org/tv/71914-the-wheel-of-time
val idRegex = Regex("""themoviedb\.org/(.*)/(\d+)""")
val found = idRegex.find(url)
val isTvSeries = found?.groupValues?.getOrNull(1).equals("tv", ignoreCase = true)
2022-04-03 21:41:28 +00:00
val id = found?.groupValues?.getOrNull(2)?.toIntOrNull()
?: throw ErrorLoadingException("No id found")
2021-11-28 16:10:19 +00:00
return if (useMetaLoadResponse) {
return if (isTvSeries) {
2022-06-16 01:04:24 +00:00
val body = tmdb.tvService()
.tv(
id,
"en-US",
AppendToResponse(
AppendToResponseItem.EXTERNAL_IDS,
AppendToResponseItem.VIDEOS
)
)
.awaitResponse().body()
2022-02-04 21:25:09 +00:00
val response = body?.toLoadResponse()
if (response != null) {
if (response.recommendations.isNullOrEmpty())
tmdb.tvService().recommendations(id, 1, "en-US").awaitResponse().body()
?.let {
it.results?.map { res -> res.toSearchResponse() }
}?.let { list ->
2022-04-03 21:41:28 +00:00
response.recommendations = list
}
if (response.actors.isNullOrEmpty())
tmdb.tvService().credits(id, "en-US").awaitResponse().body()?.let {
2022-02-05 23:58:56 +00:00
response.addActors(it.cast?.toActors())
}
2022-02-04 21:25:09 +00:00
}
2022-02-04 21:25:09 +00:00
response
2021-11-28 16:10:19 +00:00
} else {
2022-06-16 01:04:24 +00:00
val body = tmdb.moviesService()
.summary(
id,
"en-US",
AppendToResponse(
AppendToResponseItem.EXTERNAL_IDS,
AppendToResponseItem.VIDEOS
)
)
.awaitResponse().body()
2022-02-04 21:25:09 +00:00
val response = body?.toLoadResponse()
if (response != null) {
if (response.recommendations.isNullOrEmpty())
tmdb.moviesService().recommendations(id, 1, "en-US").awaitResponse().body()
?.let {
it.results?.map { res -> res.toSearchResponse() }
}?.let { list ->
response.recommendations = list
}
if (response.actors.isNullOrEmpty())
tmdb.moviesService().credits(id).awaitResponse().body()?.let {
2022-02-05 23:58:56 +00:00
response.addActors(it.cast?.toActors())
}
2022-02-04 21:25:09 +00:00
}
response
2021-11-28 16:10:19 +00:00
}
} else {
loadFromTmdb(id)?.let { return it }
if (isTvSeries) {
2022-01-31 19:39:24 +00:00
tmdb.tvService().externalIds(id, "en-US").awaitResponse().body()?.imdb_id?.let {
2021-11-28 16:10:19 +00:00
val fromImdb = loadFromImdb(it)
val result = if (fromImdb == null) {
2022-01-31 19:39:24 +00:00
val details = tmdb.tvService().tv(id, "en-US").awaitResponse().body()
2021-11-28 16:10:19 +00:00
loadFromImdb(it, details?.seasons ?: listOf())
?: loadFromTmdb(id, details?.seasons ?: listOf())
} else {
fromImdb
}
result
}
} else {
2022-01-31 19:39:24 +00:00
tmdb.moviesService().externalIds(id, "en-US").awaitResponse()
2021-11-28 16:10:19 +00:00
.body()?.imdb_id?.let { loadFromImdb(it) }
}
}
}
2022-01-16 22:31:42 +00:00
override suspend fun search(query: String): List<SearchResponse>? {
2022-06-15 17:47:07 +00:00
return tmdb.searchService().multi(query, 1, "en-Us", "US", includeAdult).awaitResponse()
2021-11-28 16:10:19 +00:00
.body()?.results?.mapNotNull {
it.movie?.toSearchResponse() ?: it.tvShow?.toSearchResponse()
}
}
}