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

186 lines
6.4 KiB
Kotlin
Raw Permalink Normal View History

2022-09-26 17:07:39 +00:00
package com.hexated
import com.lagradost.cloudstream3.*
2024-01-06 06:00:38 +00:00
import com.lagradost.cloudstream3.utils.AppUtils
import com.lagradost.cloudstream3.utils.AppUtils.toJson
2022-09-26 17:07:39 +00:00
import com.lagradost.cloudstream3.utils.ExtractorLink
2024-01-25 16:09:52 +00:00
import com.lagradost.cloudstream3.utils.Qualities
2022-09-26 17:07:39 +00:00
import org.jsoup.nodes.Element
2024-01-06 06:00:38 +00:00
import java.net.URI
2022-09-26 17:07:39 +00:00
2024-01-06 06:00:38 +00:00
class TimefourTv : MainAPI() {
override var mainUrl = "https://dlhd.sx"
2022-09-26 17:07:39 +00:00
override var name = "Time4tv"
override val hasDownloadSupport = false
override val hasMainPage = true
override val supportedTypes = setOf(
2024-01-25 16:09:52 +00:00
TvType.Live
2022-09-26 17:07:39 +00:00
)
2024-02-29 02:19:00 +00:00
private val homePoster = "https://cdn.discordapp.com/attachments/1109266606292488297/1193060449193840681/Screenshot_2024-01-06_at_12-14-16_Logo_Maker_Used_By_2.3_Million_Startups.png?ex=65ebf0a1&is=65d97ba1&hm=aa4018534090d5dc69cf16a15ab4663d6f84a742d3f3b5ccad4be779c26517d7&"
2024-01-06 06:00:38 +00:00
private val detailPoster =
2024-02-29 02:19:00 +00:00
"https://cdn.discordapp.com/attachments/1109266606292488297/1193060448929595454/Screenshot_2024-01-06_at_12-13-02_Logo_Maker_Used_By_2.3_Million_Startups.png?ex=65ebf0a1&is=65d97ba1&hm=2dc35d2fcc09530f6d9fc963ecf6b9a28eeec1a7c76a083711379c7280dd34dc&"
2024-01-06 06:00:38 +00:00
2022-09-26 17:07:39 +00:00
override val mainPage = mainPageOf(
2024-01-25 16:09:52 +00:00
"$mainUrl/24-7-channels.php" to "24/7 Channels",
"$mainUrl/schedule/schedule-generated.json" to "Schedule Channels"
2022-09-26 17:07:39 +00:00
)
override suspend fun getMainPage(
2024-01-25 16:09:52 +00:00
page: Int,
request: MainPageRequest
2022-09-26 17:07:39 +00:00
): HomePageResponse {
2022-10-04 12:51:29 +00:00
val items = mutableListOf<HomePageList>()
2024-01-06 06:00:38 +00:00
if (request.name == "24/7 Channels") {
val req = app.get(request.data)
mainUrl = getBaseUrl(req.url)
val res = req.document
val channels = res.select("div.grid-container div.grid-item").mapNotNull {
it.toSearchResponse()
2022-10-04 12:51:29 +00:00
}
2024-01-06 06:00:38 +00:00
if (channels.isNotEmpty()) items.add(HomePageList(request.name, channels, true))
} else {
val res = app.get(request.data).parsedSafe<Map<String, Map<String, ArrayList<Items>>>>()
res?.forEach { tag ->
val header = tag.key
val channels = tag.value.mapNotNull {
LiveSearchResponse(
2024-01-25 16:09:52 +00:00
it.key,
Item(it.key, items = it.value.toJson()).toJson(),
this@TimefourTv.name,
TvType.Live,
posterUrl = homePoster,
2024-01-06 06:00:38 +00:00
)
}
if (channels.isNotEmpty()) items.add(HomePageList(header, channels, true))
2022-11-06 05:45:43 +00:00
}
}
2024-01-06 06:00:38 +00:00
return newHomePageResponse(items, false)
2022-09-26 17:07:39 +00:00
}
2024-01-06 06:00:38 +00:00
private fun Element.toSearchResponse(): LiveSearchResponse {
val title = this.select("strong").text()
val href = fixUrl(this.select("a").attr("href"))
2023-01-01 14:03:35 +00:00
return LiveSearchResponse(
2024-01-25 16:09:52 +00:00
title,
Item(title, href).toJson(),
this@TimefourTv.name,
TvType.Live,
posterUrl = homePoster,
2023-01-01 14:03:35 +00:00
)
}
2023-09-10 12:56:48 +00:00
override suspend fun search(query: String): List<SearchResponse> {
2024-01-06 06:00:38 +00:00
val document = app.get("$mainUrl/24-7-channels.php").document
2023-09-10 12:56:48 +00:00
return document.select("div.grid-container div.grid-item:contains($query)").mapNotNull {
2024-01-06 06:00:38 +00:00
it.toSearchResponse()
2023-09-10 12:56:48 +00:00
}
}
2022-09-26 17:07:39 +00:00
2022-11-06 05:45:43 +00:00
2024-01-06 06:00:38 +00:00
override suspend fun load(url: String): LoadResponse {
val data = AppUtils.parseJson<Item>(url)
val episodes = if (data.items.isNullOrEmpty()) {
listOf(Episode(arrayListOf(Channels(data.title, data.url)).toJson()))
} else {
val items = AppUtils.parseJson<ArrayList<Items>>(data.items)
2024-01-07 11:09:14 +00:00
items.mapNotNull { eps ->
2024-01-06 06:00:38 +00:00
Episode(
2024-01-25 16:09:52 +00:00
data = eps.channels?.toJson() ?: return@mapNotNull null,
name = "${eps.event}${eps.time}",
description = eps.channels.map { it.channel_name }.joinToString(""),
posterUrl = detailPoster,
2024-01-06 06:00:38 +00:00
)
2023-07-29 19:37:51 +00:00
}
2022-09-26 17:07:39 +00:00
}
2024-01-06 06:00:38 +00:00
return newTvSeriesLoadResponse(
2024-01-25 16:09:52 +00:00
data.title ?: "",
url,
TvType.TvSeries,
episodes = episodes
2024-01-06 06:00:38 +00:00
) {
posterUrl = homePoster
}
2022-09-26 17:07:39 +00:00
}
override suspend fun loadLinks(
2024-01-25 16:09:52 +00:00
data: String,
isCasting: Boolean,
subtitleCallback: (SubtitleFile) -> Unit,
callback: (ExtractorLink) -> Unit
2022-09-26 17:07:39 +00:00
): Boolean {
2024-01-06 06:00:38 +00:00
val json = AppUtils.parseJson<ArrayList<Channels>>(data)
json.apmap {
val iframe = app.get(
2024-01-25 16:09:52 +00:00
fixChannelUrl(
it.channel_id ?: return@apmap
)
2024-01-06 06:00:38 +00:00
).document.selectFirst("iframe#thatframe")?.attr("src")
2024-01-25 16:09:52 +00:00
?: throw ErrorLoadingException("No Iframe Found")
2024-01-06 06:00:38 +00:00
val host = getBaseUrl(iframe)
val video = extractVideo(iframe)
2024-01-25 16:09:52 +00:00
callback.invoke(
ExtractorLink(
this.name,
2024-01-06 06:00:38 +00:00
it.channel_name ?: return@apmap,
video ?: return@apmap,
"$host/",
2024-01-25 16:09:52 +00:00
Qualities.Unknown.value,
isM3u8 = true,
)
)
2022-09-26 17:07:39 +00:00
}
2024-01-06 06:00:38 +00:00
2022-09-26 17:07:39 +00:00
return true
}
2024-01-06 06:00:38 +00:00
private suspend fun extractVideo(url: String): String? {
val res = app.get(url, referer = mainUrl)
return Regex("""source:['"](\S+.m3u8)['"],""").find(res.text)?.groupValues?.getOrNull(
2024-01-25 16:09:52 +00:00
1
2024-01-06 06:00:38 +00:00
) ?: run {
val scriptData =
2024-01-25 16:09:52 +00:00
res.document.selectFirst("div#player")?.nextElementSibling()?.data()
?.substringAfterLast("return(")?.substringBefore(".join")
2024-01-06 06:00:38 +00:00
scriptData?.removeSurrounding("[", "]")?.replace("\"", "")?.split(",")
2024-01-25 16:09:52 +00:00
?.joinToString("")
2024-01-06 06:00:38 +00:00
}
}
private fun fixChannelUrl(url: String): String {
return if (url.startsWith(mainUrl)) {
url
} else {
"$mainUrl/stream/stream-$url.php"
}
}
private fun getBaseUrl(url: String): String {
return URI(url).let {
"${it.scheme}://${it.host}"
}
}
data class Item(
2024-01-25 16:09:52 +00:00
val title: String? = null,
val url: String? = null,
val items: String? = null,
2024-01-06 06:00:38 +00:00
)
data class Items(
2024-01-25 16:09:52 +00:00
val time: String? = null,
val event: String? = null,
val channels: ArrayList<Channels>? = arrayListOf(),
2024-01-06 06:00:38 +00:00
)
data class Channels(
2024-01-25 16:09:52 +00:00
val channel_name: String? = null,
val channel_id: String? = null,
2024-01-06 06:00:38 +00:00
)
2022-09-26 17:07:39 +00:00
}