package com.hexated import com.fasterxml.jackson.annotation.JsonProperty import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.LoadResponse.Companion.addActors import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer import com.lagradost.cloudstream3.mvvm.safeApiCall import com.lagradost.cloudstream3.network.CloudflareKiller import com.lagradost.cloudstream3.utils.* import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson import okhttp3.Interceptor import okhttp3.Response import org.jsoup.Jsoup import org.jsoup.nodes.Element import java.net.URI class IdlixProvider : MainAPI() { override var mainUrl = "https://tv.idlixplus.net" private var directUrl = mainUrl override var name = "Idlix" override val hasMainPage = true override var lang = "id" override val hasDownloadSupport = true private val cloudflareKiller by lazy { CloudflareKiller() } private val interceptor by lazy { CloudflareInterceptor(cloudflareKiller) } override val supportedTypes = setOf( TvType.Movie, TvType.TvSeries, TvType.Anime, TvType.AsianDrama ) private val key = "\\x5a\\x6d\\x5a\\x6c\\x4e\\x7a\\x55\\x79\\x4d\\x54\\x56\\x6a\\x5a\\x47\\x52\\x69\\x5a\\x44\\x55\\x30\\x5a\\x6d\\x59\\x35\\x4f\\x57\\x45\\x33\\x4d\\x44\\x4a\\x69\\x4e\\x32\\x4a\\x6c\\x4f\\x54\\x42\\x6c\\x4e\\x7a\\x49\\x3d" override val mainPage = mainPageOf( "$mainUrl/" to "Featured", "$mainUrl/trending/page/?get=movies" to "Trending Movies", "$mainUrl/trending/page/?get=tv" to "Trending TV Series", "$mainUrl/movie/page/" to "Movie Terbaru", "$mainUrl/tvseries/page/" to "TV Series Terbaru", "$mainUrl/network/netflix/page/" to "Netflix", "$mainUrl/genre/anime/page/" to "Anime", "$mainUrl/genre/drama-korea/page/" to "Drama Korea", ) private fun getBaseUrl(url: String): String { return URI(url).let { "${it.scheme}://${it.host}" } } override suspend fun getMainPage( page: Int, request: MainPageRequest ): HomePageResponse { val url = request.data.split("?") val nonPaged = request.name == "Featured" && page <= 1 val req = if (nonPaged) { app.get(request.data, interceptor = interceptor) } else { app.get("${url.first()}$page/?${url.lastOrNull()}", interceptor = interceptor) } mainUrl = getBaseUrl(req.url) val document = req.document val home = (if (nonPaged) { document.select("div.items.featured article") } else { document.select("div.items.full article, div#archive-content article") }).mapNotNull { it.toSearchResult() } return newHomePageResponse(request.name, home) } private fun getProperLink(uri: String): String { return when { uri.contains("/episode/") -> { var title = uri.substringAfter("$mainUrl/episode/") title = Regex("(.+?)-season").find(title)?.groupValues?.get(1).toString() "$mainUrl/tvseries/$title" } uri.contains("/season/") -> { var title = uri.substringAfter("$mainUrl/season/") title = Regex("(.+?)-season").find(title)?.groupValues?.get(1).toString() "$mainUrl/tvseries/$title" } else -> { uri } } } private fun Element.toSearchResult(): SearchResponse { val title = this.selectFirst("h3 > a")!!.text().replace(Regex("\\(\\d{4}\\)"), "").trim() val href = getProperLink(this.selectFirst("h3 > a")!!.attr("href")) val posterUrl = this.select("div.poster > img").attr("src").toString() val quality = getQualityFromString(this.select("span.quality").text()) return newMovieSearchResponse(title, href, TvType.Movie) { this.posterUrl = posterUrl this.quality = quality posterHeaders = cloudflareKiller.getCookieHeaders(mainUrl).toMap() } } override suspend fun search(query: String): List { val req = app.get("$mainUrl/search/$query", interceptor = interceptor) mainUrl = getBaseUrl(req.url) val document = req.document return document.select("div.result-item").map { val title = it.selectFirst("div.title > a")!!.text().replace(Regex("\\(\\d{4}\\)"), "").trim() val href = getProperLink(it.selectFirst("div.title > a")!!.attr("href")) val posterUrl = it.selectFirst("img")!!.attr("src").toString() newMovieSearchResponse(title, href, TvType.TvSeries) { this.posterUrl = posterUrl posterHeaders = cloudflareKiller.getCookieHeaders(mainUrl).toMap() } } } override suspend fun load(url: String): LoadResponse { val request = app.get(url, interceptor = interceptor, referer = "$directUrl/") directUrl = getBaseUrl(request.url) val document = request.document val title = document.selectFirst("div.data > h1")?.text()?.replace(Regex("\\(\\d{4}\\)"), "") ?.trim().toString() val poster = document.select("div.poster > img").attr("src").toString() val tags = document.select("div.sgeneros > a").map { it.text() } val year = Regex(",\\s?(\\d+)").find( document.select("span.date").text().trim() )?.groupValues?.get(1).toString().toIntOrNull() val tvType = if (document.select("ul#section > li:nth-child(1)").text().contains("Episodes") ) TvType.TvSeries else TvType.Movie val description = document.select("div.wp-content > p").text().trim() val trailer = document.selectFirst("div.embed iframe")?.attr("src") val rating = document.selectFirst("span.dt_rating_vgs")?.text()?.toRatingInt() val actors = document.select("div.persons > div[itemprop=actor]").map { Actor(it.select("meta[itemprop=name]").attr("content"), it.select("img").attr("src")) } val recommendations = document.select("div.owl-item").map { val recName = it.selectFirst("a")!!.attr("href").toString().removeSuffix("/").split("/").last() val recHref = it.selectFirst("a")!!.attr("href") val recPosterUrl = it.selectFirst("img")?.attr("src").toString() newTvSeriesSearchResponse(recName, recHref, TvType.TvSeries) { this.posterUrl = recPosterUrl posterHeaders = cloudflareKiller.getCookieHeaders(mainUrl).toMap() } } return if (tvType == TvType.TvSeries) { val episodes = document.select("ul.episodios > li").map { val href = it.select("a").attr("href") val name = fixTitle(it.select("div.episodiotitle > a").text().trim()) val image = it.select("div.imagen > img").attr("src") val episode = it.select("div.numerando").text().replace(" ", "").split("-").last() .toIntOrNull() val season = it.select("div.numerando").text().replace(" ", "").split("-").first() .toIntOrNull() Episode( href, name, season, episode, image ) } newTvSeriesLoadResponse(title, url, TvType.TvSeries, episodes) { this.posterUrl = poster this.year = year this.plot = description this.tags = tags this.rating = rating addActors(actors) this.recommendations = recommendations addTrailer(trailer) posterHeaders = cloudflareKiller.getCookieHeaders(mainUrl).toMap() } } else { newMovieLoadResponse(title, url, TvType.Movie, url) { this.posterUrl = poster this.year = year this.plot = description this.tags = tags this.rating = rating addActors(actors) this.recommendations = recommendations addTrailer(trailer) posterHeaders = cloudflareKiller.getCookieHeaders(mainUrl).toMap() } } } override suspend fun loadLinks( data: String, isCasting: Boolean, subtitleCallback: (SubtitleFile) -> Unit, callback: (ExtractorLink) -> Unit ): Boolean { val document = app.get(data, interceptor = interceptor, referer = "$directUrl/").document val id = document.select("meta#dooplay-ajax-counter").attr("data-postid") val type = if (data.contains("/movie/")) "movie" else "tv" document.select("ul#playeroptionsul > li").map { it.attr("data-nume") }.apmap { nume -> safeApiCall { val source = app.post( url = "$directUrl/wp-admin/admin-ajax.php", data = mapOf( "action" to "doo_player_ajax", "post" to id, "nume" to nume, "type" to type ), headers = mapOf("X-Requested-With" to "XMLHttpRequest"), referer = data, interceptor = interceptor ).let { tryParseJson(it.text) } ?: return@safeApiCall val password = if(source.key?.startsWith("\\x") == true) source.key else key var decrypted = AesHelper.cryptoAESHandler(source.embed_url, password.toByteArray(), false)?.fixBloat() ?: return@safeApiCall if (decrypted.startsWith("https://uservideo.xyz")) { decrypted = app.get(decrypted).document.select("iframe").attr("src") } getUrl(decrypted, "$directUrl/", subtitleCallback, callback) } } return true } class CloudflareInterceptor(private val cloudflareKiller: CloudflareKiller): Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) val doc = Jsoup.parse(response.peekBody(1024 * 1024).string()) if (doc.select("title").text() == "Just a moment...") { return cloudflareKiller.intercept(chain) } return response } } private fun String.fixBloat() : String { return this.replace("\"", "").replace("\\", "") } private suspend fun getUrl( url: String, referer: String?, subtitleCallback: (SubtitleFile) -> Unit, callback: (ExtractorLink) -> Unit ) { val document = app.get(url, referer = "$mainUrl/").document val hash = url.split("/").last().substringAfter("data=") val m3uLink = app.post( url = "$mainUrl/player/index.php?data=$hash&do=getVideo", data = mapOf("hash" to hash, "r" to "$referer"), referer = referer, headers = mapOf("X-Requested-With" to "XMLHttpRequest") ).parsed().videoSource M3u8Helper.generateM3u8( this.name, m3uLink, "$referer", ).forEach(callback) document.select("script").map { script -> if (script.data().contains("eval(function(p,a,c,k,e,d)")) { val subData = getAndUnpack(script.data()).substringAfter("\"tracks\":[").substringBefore("],") tryParseJson>("[$subData]")?.map { subtitle -> subtitleCallback.invoke( SubtitleFile( getLanguage(subtitle.label ?: ""), subtitle.file ) ) } } } } private fun getLanguage(str: String): String { return when { str.contains("indonesia", true) || str .contains("bahasa", true) -> "Indonesian" else -> str } } data class ResponseSource( @JsonProperty("hls") val hls: Boolean, @JsonProperty("videoSource") val videoSource: String, @JsonProperty("securedLink") val securedLink: String?, ) data class Tracks( @JsonProperty("kind") val kind: String?, @JsonProperty("file") val file: String, @JsonProperty("label") val label: String?, ) data class ResponseHash( @JsonProperty("embed_url") val embed_url: String, @JsonProperty("key") val key: String?, @JsonProperty("type") val type: String?, ) }