2021-07-29 00:19:42 +00:00
|
|
|
package com.lagradost.cloudstream3.ui
|
|
|
|
|
|
|
|
import com.lagradost.cloudstream3.*
|
2022-10-08 23:36:06 +00:00
|
|
|
import com.lagradost.cloudstream3.APIHolder.unixTimeMS
|
2021-07-29 00:19:42 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.Resource
|
2022-01-16 22:31:42 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.logError
|
2021-07-29 00:19:42 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.safeApiCall
|
|
|
|
import com.lagradost.cloudstream3.utils.ExtractorLink
|
2022-10-08 23:36:06 +00:00
|
|
|
import kotlinx.coroutines.delay
|
2021-07-29 00:19:42 +00:00
|
|
|
|
|
|
|
class APIRepository(val api: MainAPI) {
|
2021-08-12 00:04:58 +00:00
|
|
|
companion object {
|
2021-09-12 14:10:22 +00:00
|
|
|
var dubStatusActive = HashSet<DubStatus>()
|
2021-08-12 00:04:58 +00:00
|
|
|
|
2021-09-05 11:56:25 +00:00
|
|
|
val noneApi = object : MainAPI() {
|
2022-03-16 15:29:11 +00:00
|
|
|
override var name = "None"
|
2021-12-26 00:05:10 +00:00
|
|
|
override val supportedTypes = emptySet<TvType>()
|
2022-06-16 22:07:07 +00:00
|
|
|
override var lang = ""
|
2021-09-05 11:56:25 +00:00
|
|
|
}
|
|
|
|
val randomApi = object : MainAPI() {
|
2022-03-16 15:29:11 +00:00
|
|
|
override var name = "Random"
|
2021-12-26 00:05:10 +00:00
|
|
|
override val supportedTypes = emptySet<TvType>()
|
2022-06-16 22:07:07 +00:00
|
|
|
override var lang = ""
|
2021-09-05 11:56:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 17:50:16 +00:00
|
|
|
fun isInvalidData(data: String): Boolean {
|
2022-01-14 18:14:24 +00:00
|
|
|
return data.isEmpty() || data == "[]" || data == "about:blank"
|
|
|
|
}
|
2022-10-28 01:51:27 +00:00
|
|
|
|
|
|
|
private val cacheHash: HashMap<Pair<String,String>, LoadResponse> = hashMapOf()
|
2021-09-05 11:56:25 +00:00
|
|
|
}
|
2021-12-26 00:05:10 +00:00
|
|
|
|
2022-01-14 18:14:24 +00:00
|
|
|
val hasMainPage = api.hasMainPage
|
|
|
|
val name = api.name
|
|
|
|
val mainUrl = api.mainUrl
|
2022-07-30 03:43:57 +00:00
|
|
|
val mainPage = api.mainPage
|
2022-01-14 18:14:24 +00:00
|
|
|
val hasQuickSearch = api.hasQuickSearch
|
2022-08-01 01:00:48 +00:00
|
|
|
val vpnStatus = api.vpnStatus
|
|
|
|
val providerType = api.providerType
|
2021-07-29 00:19:42 +00:00
|
|
|
|
|
|
|
suspend fun load(url: String): Resource<LoadResponse> {
|
|
|
|
return safeApiCall {
|
2022-07-20 23:48:40 +00:00
|
|
|
if (isInvalidData(url)) throw ErrorLoadingException()
|
2022-10-28 01:51:27 +00:00
|
|
|
val fixedUrl = api.fixUrl(url)
|
|
|
|
val key = Pair(api.name,url)
|
|
|
|
cacheHash[key] ?: api.load(fixedUrl)?.also {
|
|
|
|
// we cache 20 responses because ppl often go back to the same shit + 20 because I dont want to cause too much memory leak
|
|
|
|
if (cacheHash.size > 20) cacheHash.remove(cacheHash.keys.random())
|
|
|
|
cacheHash[key] = it
|
|
|
|
} ?: throw ErrorLoadingException()
|
2021-07-29 00:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 19:35:26 +00:00
|
|
|
suspend fun search(query: String): Resource<List<SearchResponse>> {
|
2022-01-14 18:14:24 +00:00
|
|
|
if (query.isEmpty())
|
|
|
|
return Resource.Success(emptyList())
|
|
|
|
|
2021-07-29 00:19:42 +00:00
|
|
|
return safeApiCall {
|
2021-08-14 19:35:26 +00:00
|
|
|
return@safeApiCall (api.search(query)
|
2021-10-09 12:19:26 +00:00
|
|
|
?: throw ErrorLoadingException())
|
|
|
|
// .filter { typesActive.contains(it.type) }
|
|
|
|
.toList()
|
2021-07-29 00:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 17:38:41 +00:00
|
|
|
suspend fun quickSearch(query: String): Resource<List<SearchResponse>> {
|
2022-01-14 18:14:24 +00:00
|
|
|
if (query.isEmpty())
|
|
|
|
return Resource.Success(emptyList())
|
|
|
|
|
2021-07-29 00:19:42 +00:00
|
|
|
return safeApiCall {
|
2021-08-04 01:50:24 +00:00
|
|
|
api.quickSearch(query) ?: throw ErrorLoadingException()
|
2021-07-29 00:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 23:36:06 +00:00
|
|
|
suspend fun waitForHomeDelay() {
|
|
|
|
val delta = api.sequentialMainPageScrollDelay + api.lastHomepageRequest - unixTimeMS
|
2022-10-11 13:24:16 +00:00
|
|
|
if (delta < 0) return
|
2022-10-08 23:36:06 +00:00
|
|
|
delay(delta)
|
|
|
|
}
|
|
|
|
|
2022-07-30 03:43:57 +00:00
|
|
|
suspend fun getMainPage(page: Int, nameIndex: Int? = null): Resource<List<HomePageResponse?>> {
|
2021-07-29 00:19:42 +00:00
|
|
|
return safeApiCall {
|
2022-10-08 23:36:06 +00:00
|
|
|
api.lastHomepageRequest = unixTimeMS
|
|
|
|
|
2022-07-30 03:43:57 +00:00
|
|
|
nameIndex?.let { api.mainPage.getOrNull(it) }?.let { data ->
|
2022-10-28 01:51:27 +00:00
|
|
|
listOf(
|
|
|
|
api.getMainPage(
|
|
|
|
page,
|
|
|
|
MainPageRequest(data.name, data.data, data.horizontalImages)
|
|
|
|
)
|
|
|
|
)
|
2022-10-08 23:36:06 +00:00
|
|
|
} ?: run {
|
|
|
|
if (api.sequentialMainPage) {
|
|
|
|
var first = true
|
|
|
|
api.mainPage.map { data ->
|
|
|
|
if (!first) // dont want to sleep on first request
|
|
|
|
delay(api.sequentialMainPageDelay)
|
|
|
|
first = false
|
|
|
|
|
2022-10-11 13:24:16 +00:00
|
|
|
api.getMainPage(
|
|
|
|
page,
|
|
|
|
MainPageRequest(data.name, data.data, data.horizontalImages)
|
|
|
|
)
|
2022-10-08 23:36:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
api.mainPage.apmap { data ->
|
2022-10-11 13:24:16 +00:00
|
|
|
api.getMainPage(
|
|
|
|
page,
|
|
|
|
MainPageRequest(data.name, data.data, data.horizontalImages)
|
|
|
|
)
|
2022-10-08 23:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-30 03:43:57 +00:00
|
|
|
}
|
2021-07-29 00:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 17:50:16 +00:00
|
|
|
suspend fun extractorVerifierJob(extractorData: String?) {
|
|
|
|
safeApiCall {
|
|
|
|
api.extractorVerifierJob(extractorData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 22:31:42 +00:00
|
|
|
suspend fun loadLinks(
|
2021-07-29 00:19:42 +00:00
|
|
|
data: String,
|
|
|
|
isCasting: Boolean,
|
|
|
|
subtitleCallback: (SubtitleFile) -> Unit,
|
|
|
|
callback: (ExtractorLink) -> Unit
|
|
|
|
): Boolean {
|
2022-01-14 18:14:24 +00:00
|
|
|
if (isInvalidData(data)) return false // this makes providers cleaner
|
2022-01-16 22:31:42 +00:00
|
|
|
return try {
|
|
|
|
api.loadLinks(data, isCasting, subtitleCallback, callback)
|
|
|
|
} catch (throwable: Throwable) {
|
|
|
|
logError(throwable)
|
|
|
|
return false
|
|
|
|
}
|
2021-07-29 00:19:42 +00:00
|
|
|
}
|
|
|
|
}
|