Mp4UploadExtractor.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/Mp4UploadExtractor.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.extractVideos910/**11 * Mp4Upload embed extractor.12 *13 * The player serves the source URL via one of two script shapes:14 * - **Packed**: `eval(function(p,a,c,k,e,d){…})('payload', …)` — unpack first.15 * - **Plain**: a script that contains `player.src({ src: "<url>" })`.16 *17 * Quality is the `HEIGHT=` value inside the same script's m3u8 metadata.18 */19class Mp4UploadExtractor(20 private val http: HttpService,21) : AnimeExtractorService {2223 override val hostName: String get() = "mp4upload"2425 override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =26 extractVideos(headers) { videosFromUrl(url, headers) }2728 private suspend fun videosFromUrl(29 url: String,30 headers: Map<String, String>,31 prefix: String = "",32 suffix: String = "",33 ): List<DbVideo> {34 val mergedHeaders = headers.toMutableMap().apply { put("referer", REFERER) }35 val response = http.request(HttpRequest(url, headers = mergedHeaders))36 if (response.statusCode !in 200..299) return emptyList()37 val body = response.body3839 val packed = ScriptTagRegex.findAll(body)40 .map { it.groupValues[1] }41 .firstOrNull { it.contains("eval") && it.contains("p,a,c,k,e,d") }42 val plain = ScriptTagRegex.findAll(body)43 .map { it.groupValues[1] }44 .firstOrNull { it.contains("player.src") }45 val script = when {46 packed != null -> JsPacker.unpack(packed) ?: return emptyList()47 plain != null -> plain48 else -> return emptyList()49 }5051 val videoUrl = script52 .substringAfter(".src(", "")53 .substringBefore(")", "")54 .substringAfter("src:", "")55 .substringAfter("\"", "")56 .substringBefore("\"", "")57 if (videoUrl.isBlank()) return emptyList()58 val resolution = HEIGHT_REGEX.find(script)?.groupValues?.get(1) ?: "Unknown resolution"59 val quality = buildString {60 if (prefix.isNotBlank()) append(prefix).append(' ')61 append("Mp4Upload - ${resolution}p")62 if (suffix.isNotBlank()) append(' ').append(suffix)63 }64 return listOf(DbVideo(url = videoUrl, quality = quality, originalUrl = videoUrl, headers = mergedHeaders))65 }6667 private companion object {68 const val REFERER = "https://mp4upload.com/"69 val HEIGHT_REGEX = Regex("""\WHEIGHT=(\d+)""")70 }71}72