[skip ci] refactor(lib): Refactor some libraries (#2454)
This commit is contained in:
@ -17,5 +17,6 @@ dependencies {
|
||||
implementation("dev.datlag.jsunpacker:jsunpacker:1.0.1") {
|
||||
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
|
||||
}
|
||||
implementation(project(":lib-playlist-utils"))
|
||||
compileOnly(libs.bundles.common)
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.network.GET
|
||||
import eu.kanade.tachiyomi.util.asJsoup
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import eu.kanade.tachiyomi.lib.playlistutils.PlaylistUtils
|
||||
import okhttp3.Headers
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
@ -15,9 +16,15 @@ import uy.kohesive.injekt.injectLazy
|
||||
class FilemoonExtractor(private val client: OkHttpClient) {
|
||||
private val json: Json by injectLazy()
|
||||
|
||||
fun videosFromUrl(url: String, prefix: String = "Filemoon - ", headers: Headers? = null, useHeadersForHtml: Boolean = false): List<Video> {
|
||||
fun videosFromUrl(url: String, prefix: String = "Filemoon - ", headers: Headers? = null): List<Video> {
|
||||
return runCatching {
|
||||
val doc = if (useHeadersForHtml) client.newCall(GET(url, headers = (headers?.newBuilder() ?: Headers.Builder()).build())).execute().asJsoup() else client.newCall(GET(url)).execute().asJsoup()
|
||||
val httpUrl = url.toHttpUrl()
|
||||
val videoHeaders = (headers?.newBuilder() ?: Headers.Builder())
|
||||
.set("Referer", url)
|
||||
.set("Origin", "https://${httpUrl.host}")
|
||||
.build()
|
||||
|
||||
val doc = client.newCall(GET(url, videoHeaders)).execute().use { it.asJsoup() }
|
||||
val jsEval = doc.selectFirst("script:containsData(eval):containsData(m3u8)")!!.data()
|
||||
val unpacked = JsUnpacker.unpackAndCombine(jsEval).orEmpty()
|
||||
val masterUrl = unpacked.takeIf(String::isNotBlank)
|
||||
@ -26,14 +33,6 @@ class FilemoonExtractor(private val client: OkHttpClient) {
|
||||
?.takeIf(String::isNotBlank)
|
||||
?: return emptyList()
|
||||
|
||||
val masterPlaylist = client.newCall(GET(masterUrl)).execute().body.string()
|
||||
|
||||
val httpUrl = url.toHttpUrl()
|
||||
val videoHeaders = (headers?.newBuilder() ?: Headers.Builder())
|
||||
.set("Referer", url)
|
||||
.set("Origin", "https://${httpUrl.host}")
|
||||
.build()
|
||||
|
||||
val subtitleTracks = buildList {
|
||||
// Subtitles from a external URL
|
||||
val subUrl = httpUrl.queryParameter("sub.info")
|
||||
@ -48,48 +47,16 @@ class FilemoonExtractor(private val client: OkHttpClient) {
|
||||
.forEach { add(Track(it.file, it.label)) }
|
||||
}
|
||||
}
|
||||
|
||||
SUBTITLES_REGEX // Subtitles from the playlist
|
||||
.findAll(masterPlaylist)
|
||||
.forEach { add(Track(it.groupValues[2], it.groupValues[1])) }
|
||||
}
|
||||
val audioTracks = AUDIO_REGEX
|
||||
.findAll(masterPlaylist)
|
||||
.map { Track(it.groupValues[2], it.groupValues[1]) }
|
||||
.toList()
|
||||
|
||||
val separator = "#EXT-X-STREAM-INF:"
|
||||
masterPlaylist.substringAfter(separator).split(separator).map {
|
||||
val resolution = it.substringAfter("RESOLUTION=")
|
||||
.substringAfter("x")
|
||||
.substringBefore(",") + "p"
|
||||
val videoUrl = it.substringAfter("\n").substringBefore("\n")
|
||||
|
||||
Video(
|
||||
videoUrl,
|
||||
prefix + resolution,
|
||||
videoUrl,
|
||||
headers = videoHeaders,
|
||||
subtitleTracks = subtitleTracks,
|
||||
audioTracks = audioTracks,
|
||||
)
|
||||
}
|
||||
PlaylistUtils(client, videoHeaders).extractFromHls(
|
||||
masterUrl,
|
||||
subtitleList = subtitleTracks,
|
||||
videoNameGen = { "$prefix$it" },
|
||||
)
|
||||
}.getOrElse { emptyList() }
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class SubtitleDto(
|
||||
val file: String,
|
||||
val label: String,
|
||||
)
|
||||
|
||||
companion object {
|
||||
private val SUBTITLES_REGEX by lazy {
|
||||
Regex("""#EXT-X-MEDIA:TYPE=SUBTITLES.*?NAME="(.*?)".*?URI="(.*?)"""")
|
||||
}
|
||||
|
||||
private val AUDIO_REGEX by lazy {
|
||||
Regex("""#EXT-X-MEDIA:TYPE=AUDIO.*?NAME="(.*?)".*?URI="(.*?)"""")
|
||||
}
|
||||
}
|
||||
data class SubtitleDto(val file: String, val label: String)
|
||||
}
|
||||
|
Reference in New Issue
Block a user