cloudstream/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt

199 lines
4.7 KiB
Kotlin
Raw Normal View History

2021-04-30 17:20:15 +00:00
package com.lagradost.cloudstream3
2021-05-15 23:37:42 +00:00
import android.app.Activity
import androidx.preference.PreferenceManager
2021-04-30 17:20:15 +00:00
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
2021-05-12 21:51:02 +00:00
import com.lagradost.cloudstream3.animeproviders.ShiroProvider
2021-04-30 17:20:15 +00:00
import java.util.*
import kotlin.collections.ArrayList
const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Firefox/68.0"
val baseHeader = mapOf("User-Agent" to USER_AGENT)
val mapper = JsonMapper.builder().addModule(KotlinModule())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build()!!
2021-05-12 21:51:02 +00:00
object APIHolder {
2021-05-15 23:37:42 +00:00
val allApi = AllProvider()
private const val defProvider = 0
2021-05-12 21:51:02 +00:00
val apis = arrayListOf<MainAPI>(
ShiroProvider()
)
2021-05-15 23:37:42 +00:00
2021-05-18 13:43:32 +00:00
fun getApiFromName(apiName: String): MainAPI {
for (api in apis) {
if (apiName == api.name)
return api
}
return apis[defProvider]
}
2021-05-15 23:37:42 +00:00
fun Activity.getApiSettings(): HashSet<String> {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
return settingsManager.getStringSet(this.getString(R.string.search_providers_list_key),
setOf(apis[defProvider].name))?.toHashSet() ?: hashSetOf(apis[defProvider].name)
}
2021-05-12 21:51:02 +00:00
}
abstract class MainAPI {
2021-04-30 17:20:15 +00:00
open val name = "NONE"
open val mainUrl = "NONE"
open fun search(query: String): ArrayList<Any>? { // SearchResponse
return null
}
2021-05-18 13:43:32 +00:00
open fun load(slug: String): Any? { //LoadResponse
2021-04-30 17:20:15 +00:00
return null
}
2021-05-18 13:43:32 +00:00
open fun loadLinks(data: Any, id: Int): Boolean {
2021-04-30 17:20:15 +00:00
return false
}
}
2021-05-15 23:37:42 +00:00
fun MainAPI.fixUrl(url: String): String {
if (url.startsWith('/')) {
return mainUrl + url
2021-05-18 13:43:32 +00:00
} else if (!url.startsWith("http") && !url.startsWith("//")) {
2021-05-15 23:37:42 +00:00
return "$mainUrl/$url"
}
return url
}
2021-04-30 17:20:15 +00:00
data class Link(
val name: String,
2021-05-12 21:51:02 +00:00
val url: String,
2021-04-30 17:20:15 +00:00
val quality: Int?,
2021-05-12 21:51:02 +00:00
val referer: String?,
2021-04-30 17:20:15 +00:00
)
enum class ShowStatus {
Completed,
Ongoing,
}
enum class DubStatus {
HasDub,
HasSub,
}
enum class TvType {
Movie,
TvSeries,
Anime,
ONA,
}
interface SearchResponse {
val name: String
2021-05-18 13:43:32 +00:00
val url: String // PUBLIC URL FOR OPEN IN APP
val slug: String // USED FOR INTERNAL DATA
2021-04-30 17:20:15 +00:00
val apiName: String
val type: TvType
val posterUrl: String?
val year: Int?
}
data class AnimeSearchResponse(
override val name: String,
override val url: String,
2021-05-18 13:43:32 +00:00
override val slug: String,
2021-04-30 17:20:15 +00:00
override val apiName: String,
override val type: TvType,
override val posterUrl: String?,
override val year: Int?,
val otherName: String?,
val dubStatus: EnumSet<DubStatus>?,
val dubEpisodes: Int?,
val subEpisodes: Int?,
) : SearchResponse
data class MovieSearchResponse(
override val name: String,
override val url: String,
2021-05-18 13:43:32 +00:00
override val slug: String,
2021-04-30 17:20:15 +00:00
override val apiName: String,
override val type: TvType,
override val posterUrl: String?,
override val year: Int?,
) : SearchResponse
data class TvSeriesSearchResponse(
override val name: String,
override val url: String,
2021-05-18 13:43:32 +00:00
override val slug: String,
2021-04-30 17:20:15 +00:00
override val apiName: String,
override val type: TvType,
override val posterUrl: String?,
override val year: Int?,
val episodes: Int?,
) : SearchResponse
interface LoadResponse {
val name: String
val url: String
val apiName: String
val type: TvType
val posterUrl: String?
val year: Int?
}
data class AnimeLoadResponse(
2021-05-16 18:28:00 +00:00
val engName: String?,
val japName: String?,
2021-04-30 17:20:15 +00:00
override val name: String,
override val url: String,
override val apiName: String,
override val type: TvType,
override val posterUrl: String?,
override val year: Int?,
2021-05-16 18:28:00 +00:00
val dubEpisodes: ArrayList<Any>?,
val subEpisodes: ArrayList<Any>?,
2021-04-30 17:20:15 +00:00
val showStatus: ShowStatus?,
val tags: ArrayList<String>?,
val plot: String?,
val synonyms: ArrayList<String>?,
val malId: Int?,
val anilistId: Int?,
) : LoadResponse
data class MovieLoadResponse(
override val name: String,
override val url: String,
override val apiName: String,
override val type: TvType,
val movieUrl: String,
override val posterUrl: String?,
override val year: Int?,
val imdbId: Int?,
) : LoadResponse
data class TvSeriesLoadResponse(
override val name: String,
override val url: String,
override val apiName: String,
override val type: TvType,
val episodes: ArrayList<String>,
override val posterUrl: String?,
override val year: Int?,
val showStatus: ShowStatus?,
val imdbId: Int?,
) : LoadResponse