cloudstream-extensions-hexated/TimefourTv/src/main/kotlin/com/hexated/TimefourTv.kt

182 lines
6.7 KiB
Kotlin
Raw Normal View History

2022-09-26 17:07:39 +00:00
package com.hexated
2022-10-17 01:35:49 +00:00
import com.hexated.TimefourTvExtractor.getLink
2022-09-26 17:07:39 +00:00
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.Qualities
import org.jsoup.Jsoup
2022-09-26 17:07:39 +00:00
import org.jsoup.nodes.Element
2022-10-05 21:48:01 +00:00
open class TimefourTv : MainAPI() {
final override var mainUrl = "https://time4tv.stream"
2022-09-26 17:07:39 +00:00
override var name = "Time4tv"
override val hasDownloadSupport = false
override val hasMainPage = true
2022-11-06 05:45:43 +00:00
private val time4tvPoster = "$mainUrl/images/logo.png"
2022-09-26 17:07:39 +00:00
override val supportedTypes = setOf(
TvType.Live
)
override val mainPage = mainPageOf(
"$mainUrl/tv-channels" to "All Channels",
"$mainUrl/usa-channels" to "USA Channels",
"$mainUrl/uk-channels" to "UK Channels",
"$mainUrl/sports-channels" to "Sport Channels",
"$mainUrl/live-sports-streams" to "Live Sport Channels",
"$mainUrl/news-channels" to "News Channels",
2022-11-06 05:45:43 +00:00
"$mainUrl/schedule.php" to "Schedule",
2022-09-26 17:07:39 +00:00
)
override suspend fun getMainPage(
page: Int,
request: MainPageRequest
): HomePageResponse {
2022-10-04 12:51:29 +00:00
val items = mutableListOf<HomePageList>()
val nonPaged = request.name != "All Channels" && page <= 1
2022-10-05 21:48:01 +00:00
if (nonPaged) {
2022-10-04 12:51:29 +00:00
val res = app.get("${request.data}.php").document
val home = res.select("div.tab-content ul li").mapNotNull {
it.toSearchResult()
}
2022-11-27 09:06:00 +00:00
if(home.isNotEmpty()) items.add(HomePageList(request.name, home, true))
2022-10-04 12:51:29 +00:00
}
if (request.name == "All Channels") {
val res = if (page == 1) {
2022-09-26 17:07:39 +00:00
app.get("${request.data}.php").document
} else {
app.get("${request.data}${page.minus(1)}.php").document
}
2022-10-04 12:51:29 +00:00
val home = res.select("div.tab-content ul li").mapNotNull {
it.toSearchResult()
}
2022-11-27 09:06:00 +00:00
if(home.isNotEmpty()) items.add(HomePageList(request.name, home, true))
2022-09-26 17:07:39 +00:00
}
2022-10-04 12:51:29 +00:00
2022-11-06 05:45:43 +00:00
if (nonPaged && request.name == "Schedule") {
val res = app.get(request.data).document
2022-11-27 09:06:00 +00:00
val schedule = res.select("div.search_p h1,div.search_p h2").mapNotNull {
2022-11-06 05:45:43 +00:00
it.toSearchSchedule()
}
items.add(HomePageList(request.name, schedule, true))
}
2022-10-04 12:51:29 +00:00
return newHomePageResponse(items)
2022-09-26 17:07:39 +00:00
}
2022-11-06 05:45:43 +00:00
private fun Element.toSearchSchedule(): LiveSearchResponse? {
return LiveSearchResponse(
this.text() ?: return null,
2022-11-06 09:12:19 +00:00
this.text(),
2022-11-06 05:45:43 +00:00
this@TimefourTv.name,
TvType.Live,
posterUrl = time4tvPoster
)
}
2022-09-26 17:07:39 +00:00
private fun Element.toSearchResult(): LiveSearchResponse? {
return LiveSearchResponse(
this.selectFirst("div.channelName")?.text() ?: return null,
fixUrl(this.selectFirst("a")!!.attr("href")),
this@TimefourTv.name,
TvType.Live,
fixUrlNull(this.selectFirst("img")?.attr("src")),
)
}
2022-11-06 05:45:43 +00:00
private suspend fun loadSchedule(url: String): LoadResponse {
2022-11-06 09:12:19 +00:00
val name = url.removePrefix("$mainUrl/")
2022-11-06 05:45:43 +00:00
val doc = app.get("$mainUrl/schedule.php").document
2022-11-25 15:04:23 +00:00
val episode = mutableListOf<Episode>()
2022-11-29 09:32:26 +00:00
doc.selectFirst("div.search_p h2:contains($name)")?.nextElementSiblings()?.toString()
?.substringBefore("<h2")?.split("<br>")?.map {
2022-11-25 15:04:23 +00:00
val desc = it.substringBefore("<span").replace(Regex("</?strong>"), "").replace("<p>", "")
Jsoup.parse(it).select("span").map { ele ->
2022-11-06 05:45:43 +00:00
val title = ele.select("a").text()
val href = ele.select("a").attr("href")
2022-11-25 15:04:23 +00:00
episode.add(
Episode(
href,
title,
description = desc,
posterUrl = time4tvPoster
)
2022-11-06 05:45:43 +00:00
)
2022-11-25 15:04:23 +00:00
}
}
2022-11-06 05:45:43 +00:00
2022-11-06 09:12:19 +00:00
return newTvSeriesLoadResponse(name, url, TvType.TvSeries, episode) {
2022-11-06 05:45:43 +00:00
}
}
2022-09-26 17:07:39 +00:00
override suspend fun load(url: String): LoadResponse? {
2022-11-06 05:45:43 +00:00
val res = app.get(url)
2022-11-06 09:12:19 +00:00
if (!res.isSuccessful) return loadSchedule(url)
2022-11-06 05:45:43 +00:00
val document = res.document
2022-09-26 17:07:39 +00:00
val title = document.selectFirst("div.channelHeading h1")?.text() ?: return null
val poster =
fixUrlNull(document.selectFirst("meta[property=\"og:image\"]")?.attr("content"))
val description = document.selectFirst("div.tvText")?.text() ?: return null
val episodes = document.selectFirst("div.playit")?.attr("onclick")?.substringAfter("open('")
?.substringBefore("',")?.let { link ->
val doc = app.get(link).document.selectFirst("div.tv_palyer iframe")?.attr("src")
?.let { iframe ->
2022-10-04 15:03:18 +00:00
app.get(fixUrl(iframe), referer = link).document
}
if (doc?.select("div.stream_button").isNullOrEmpty()) {
doc?.select("iframe")?.mapIndexed { eps, ele ->
Episode(
fixUrl(ele.attr("src")),
"Server ${eps.plus(1)}"
)
}
} else {
doc?.select("div.stream_button a")?.map {
Episode(
fixUrl(it.attr("href")),
it.text()
)
2022-09-26 17:07:39 +00:00
}
}
} ?: throw ErrorLoadingException("Refresh page")
return newTvSeriesLoadResponse(title, url, TvType.TvSeries, episodes) {
this.posterUrl = poster
this.plot = description
}
}
override suspend fun loadLinks(
data: String,
isCasting: Boolean,
subtitleCallback: (SubtitleFile) -> Unit,
callback: (ExtractorLink) -> Unit
): Boolean {
2022-11-06 05:45:43 +00:00
val link = when {
data.contains("/channel") -> app.get(data).document.selectFirst("div.tv_palyer iframe")?.attr("src")
data.startsWith(mainUrl) -> {
app.get(data, allowRedirects = false).document.selectFirst("iframe")?.attr("src")
}
else -> {
data
}
2022-10-04 15:03:18 +00:00
} ?: throw ErrorLoadingException()
2022-10-17 01:35:49 +00:00
getLink(fixUrl(link))?.let { m3uLink ->
2022-12-29 15:03:27 +00:00
val url = app.get(m3uLink, referer = "$mainServer/")
2022-09-26 17:07:39 +00:00
callback.invoke(
ExtractorLink(
this.name,
this.name,
2022-12-29 15:03:27 +00:00
url.url,
2022-09-26 17:07:39 +00:00
referer = "$mainServer/",
quality = Qualities.Unknown.value,
2022-10-05 21:48:01 +00:00
isM3u8 = true,
2022-09-26 17:07:39 +00:00
)
)
}
return true
}
}