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

104 lines
4 KiB
Kotlin
Raw Normal View History

2022-03-06 14:44:00 +00:00
package com.lagradost.cloudstream3.metaproviders
import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
import com.lagradost.cloudstream3.utils.ExtractorLink
class CrossTmdbProvider : TmdbProvider() {
override val name = "MultiMovie"
override val apiName = "MultiMovie"
override val lang = "en"
override val useMetaLoadResponse = true
override val usesWebView = true
private fun filterName(name: String): String {
return Regex("""[^a-zA-Z0-9-]""").replace(name, "")
}
private val validApis by lazy {
apis.filter { it.lang == this.lang && it::class.java != this::class.java }
//.distinctBy { it.uniqueId }
}
data class CrossMetaData(
@JsonProperty("isSuccess") val isSuccess: Boolean,
@JsonProperty("movies") val movies: List<Pair<String, String>>? = null,
)
override suspend fun loadLinks(
data: String,
isCasting: Boolean,
subtitleCallback: (SubtitleFile) -> Unit,
callback: (ExtractorLink) -> Unit
): Boolean {
tryParseJson<CrossMetaData>(data)?.let { metaData ->
if (!metaData.isSuccess) return false
metaData.movies?.apmap { (apiName, data) ->
getApiFromNameNull(apiName)?.let {
try {
it.loadLinks(data, isCasting, subtitleCallback, callback)
} catch (e: Exception) {
logError(e)
}
}
}
return true
}
return false
}
override suspend fun search(query: String): List<SearchResponse>? {
return super.search(query)?.filterIsInstance<MovieSearchResponse>()
}
override suspend fun load(url: String): LoadResponse? {
val base = super.load(url)?.apply {
val matchName = filterName(this.name)
when (this) {
is MovieLoadResponse -> {
val data = validApis.apmap { api ->
try {
if (api.supportedTypes.contains(TvType.Movie)) { //|| api.supportedTypes.contains(TvType.AnimeMovie)
return@apmap api.search(this.name)?.first {
if (filterName(it.name).equals(
matchName,
ignoreCase = true
)
) {
if (it is MovieSearchResponse)
if (it.year != null && this.year != null && it.year != this.year) // if year exist then do a check
return@first false
return@first true
}
false
}?.let { search ->
val response = api.load(search.url)
if (response is MovieLoadResponse) {
response
} else {
null
}
}
}
null
} catch (e: Exception) {
logError(e)
null
}
}.filterNotNull()
this.dataUrl =
CrossMetaData(true, data.map { it.apiName to it.dataUrl }).toJson()
}
}
}
return base
}
}