VoeExtractor.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/VoeExtractor.kt
1package eu.friedcutlet.yokan.services.media.extractors23import eu.friedcutlet.yokan.database.DbVideo4import eu.friedcutlet.yokan.services.http.HttpRequest5import eu.friedcutlet.yokan.services.http.HttpService6import eu.friedcutlet.yokan.services.media.AnimeExtractorService7import eu.friedcutlet.yokan.services.media.ExtractorResult8import eu.friedcutlet.yokan.services.media.extractVideos9import kotlin.io.encoding.Base6410import kotlin.io.encoding.ExperimentalEncodingApi1112/**13 * Voe embed extractor.14 *15 * Voe ships several script shapes that all end at an HLS master URL. We16 * mirror the Flutter implementation's branch logic:17 *18 * - If the page contains a localStorage-stamp redirect script, we follow19 * `window.location.href = '…'` once and re-parse.20 * - If the script contains `sources` / `'hls'`, the master URL is inlined21 * (sometimes raw, sometimes base64-encoded with no scheme).22 * - Otherwise the player uses the `wc0` flow: pull the only `'…'` literal23 * out of the script, base64-decode it, then read the JSON `file` field.24 * A handful of newer scripts ship the JSON reversed — alternativeScript25 * branch covers that.26 *27 * Then we fetch the master playlist and emit one [DbVideo] per variant.28 */29class VoeExtractor(30 private val http: HttpService,31) : AnimeExtractorService {3233 override val hostName: String get() = "voe"3435 override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =36 extractVideos(headers) { videosFromUrl(url, prefix = null) }3738 @OptIn(ExperimentalEncodingApi::class)39 private suspend fun videosFromUrl(url: String, prefix: String? = null): List<DbVideo> {40 val initial = http.request(HttpRequest(url))41 var body = initial.body42 // Handle the localStorage redirect Voe sometimes ships.43 val firstScript = ScriptTagRegex.find(body)?.groupValues?.get(1).orEmpty()44 if (firstScript.contains("if (typeof localStorage !== 'undefined')")) {45 val redirect = firstScript.substringAfter("window.location.href = '", "")46 .substringBefore("';", "")47 if (redirect.isNotBlank()) {48 body = http.request(HttpRequest(redirect)).body49 }50 }5152 val scripts = ScriptTagRegex.findAll(body).map { it.groupValues[1] }.toList()53 val primaryScript = scripts.firstOrNull { s ->54 s.contains("const sources") || s.contains("var sources") || s.contains("wc0")55 }56 // "Alternative" payload — a let/var assigned to a base64 string literal.57 val alternativeScripts = scripts.filter { BASE64_VAR_REGEX.containsMatchIn(it) }58 val script = primaryScript ?: alternativeScripts.firstOrNull() ?: return emptyList()5960 val playlistUrl = when {61 script.contains("sources") -> {62 val raw = script.substringAfter("hls': '", "").substringBefore("'", "")63 if (raw.startsWith("http", ignoreCase = true)) raw64 else runCatching { Base64.decode(raw).decodeToString() }.getOrNull().orEmpty()65 }66 script.contains("wc0") || primaryScript == null -> {67 val literal = SINGLE_QUOTED_REGEX.find(script)?.value68 ?.removePrefix("'")?.removeSuffix("'")69 ?: return emptyList()70 val decoded = runCatching { Base64.decode(literal).decodeToString() }.getOrNull()71 ?: return emptyList()72 // The alternative shape stores the JSON reversed.73 val maybeJson = if (primaryScript == null) decoded.reversed() else decoded74 FILE_REGEX.find(maybeJson)?.groupValues?.get(1).orEmpty()75 }76 else -> ""77 }78 if (playlistUrl.isBlank()) return emptyList()7980 val labelPrefix = listOfNotNull(prefix?.takeIf { it.isNotBlank() }, "Voe:").joinToString(" ")81 return http.m3u8Videos(masterUrl = playlistUrl, labelPrefix = labelPrefix)82 }8384 private companion object {85 val BASE64_VAR_REGEX = Regex(86 """(let|var)\s+\w+\s*=\s*'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)';""",87 )88 // Greedy enough to pick the long base64 literal Voe inlines.89 val SINGLE_QUOTED_REGEX = Regex("""'.*'""")90 val FILE_REGEX = Regex(""""file"\s*:\s*"([^"]+)"""")91 }92}93