FilemoonExtractor.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/FilemoonExtractor.kt
1package eu.friedcutlet.yokan.services.media.extractors23import eu.friedcutlet.yokan.database.DbVideo4import eu.friedcutlet.yokan.database.DbVideoTrack5import eu.friedcutlet.yokan.services.catchingNonCancellation6import eu.friedcutlet.yokan.services.http.HttpRequest7import eu.friedcutlet.yokan.services.http.HttpService8import eu.friedcutlet.yokan.services.media.AnimeExtractorService9import eu.friedcutlet.yokan.services.media.ExtractorResult10import eu.friedcutlet.yokan.services.media.extractVideos11import kotlinx.serialization.json.Json12import kotlinx.serialization.json.JsonArray13import kotlinx.serialization.json.JsonObject14import kotlinx.serialization.json.jsonPrimitive1516/**17 * Filemoon embed extractor.18 *19 * Filemoon ships the m3u8 URL inside a packed `eval(function(p,a,c,k,e,d)…)`20 * script. After unpacking we look for `{file:"…m3u8…"}` and, optionally,21 * subtitle tracks via a `fetch('…');` call (or a `sub.info` query param).22 */23class FilemoonExtractor(24 private val http: HttpService,25) : AnimeExtractorService {2627 override val hostName: String get() = "filemoon"2829 override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =30 extractVideos(headers) { videosFromUrl(url, prefix = "Filemoon", suffix = "") }3132 private suspend fun videosFromUrl(33 url: String,34 prefix: String,35 suffix: String,36 ): List<DbVideo> {37 val host = hostOf(url) ?: return emptyList()38 val videoHeaders = mapOf(39 "Referer" to url,40 "Origin" to "https://$host",41 )42 val response = http.request(HttpRequest(url))43 if (response.statusCode !in 200..299) return emptyList()44 // Filemoon ships exactly one packed script that contains "eval(".45 val packed = ScriptTagRegex.findAll(response.body)46 .map { it.groupValues[1] }47 .firstOrNull { it.contains("eval(") }48 ?: return emptyList()49 val unpacked = JsPacker.unpack(packed) ?: return emptyList()5051 val masterUrl = unpacked52 .substringAfter("{file:\"", "")53 .substringBefore("\"}", "")54 if (masterUrl.isBlank()) return emptyList()5556 val subtitleTracks = subtitleTracksFor(url, unpacked, videoHeaders)5758 return http.m3u8Videos(59 masterUrl = masterUrl,60 labelPrefix = (prefix.ifBlank { "Filemoon" }) + " -",61 labelSuffix = suffix,62 headers = videoHeaders,63 subtitles = subtitleTracks,64 )65 }6667 private suspend fun subtitleTracksFor(68 embedUrl: String,69 unpacked: String,70 videoHeaders: Map<String, String>,71 ): List<DbVideoTrack> {72 // Prefer the explicit `sub.info` query param if the source provided one;73 // fall back to the inline `fetch('…')` URL the unpacked script ships.74 val explicit = SUB_INFO_REGEX.find(embedUrl)?.groupValues?.get(1).orEmpty()75 val inline = unpacked.substringAfter("fetch('", "").substringBefore("').", "")76 val subUrl = listOf(explicit, inline).firstOrNull { it.isNotBlank() } ?: return emptyList()77 return catchingNonCancellation {78 val res = http.request(HttpRequest(subUrl, headers = videoHeaders))79 if (res.statusCode !in 200..299) return@catchingNonCancellation emptyList<DbVideoTrack>()80 val arr = Json.parseToJsonElement(res.body) as? JsonArray ?: return@catchingNonCancellation emptyList()81 arr.mapNotNull { entry ->82 val obj = entry as? JsonObject ?: return@mapNotNull null83 val file = obj["file"]?.jsonPrimitive?.content84 val label = obj["label"]?.jsonPrimitive?.content85 if (file.isNullOrBlank()) null else DbVideoTrack(file = file, label = label)86 }87 }.getOrDefault(emptyList())88 }8990 private companion object {91 val SUB_INFO_REGEX = Regex("""[?&]sub\.info=([^&#]+)""")92 }93}94