AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/APIRepository.kt

95 lines
3.0 KiB
Kotlin
Raw Normal View History

2021-07-29 00:19:42 +00:00
package com.lagradost.cloudstream3.ui
import com.lagradost.cloudstream3.*
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
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"
}
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
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()
api.load(api.fixUrl(url)) ?: 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)
?: throw ErrorLoadingException())
// .filter { typesActive.contains(it.type) }
.toList()
2021-07-29 00:19:42 +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
}
}
suspend fun getMainPage(page: Int, nameIndex: Int? = null): Resource<List<HomePageResponse?>> {
2021-07-29 00:19:42 +00:00
return safeApiCall {
nameIndex?.let { api.mainPage.getOrNull(it) }?.let { data ->
2022-07-31 15:02:22 +00:00
listOf(api.getMainPage(page, MainPageRequest(data.name, data.data)))
} ?: api.mainPage.apmap { data ->
2022-07-31 15:02:22 +00:00
api.getMainPage(page, MainPageRequest(data.name, data.data))
}
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
}
}