MytvExtractor.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/MytvExtractor.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 * Mytv embed extractor.12 *13 * Walks every `<script>` for `CreatePlayer("v=…&tp=video…")` calls — the14 * `v=` field is a URL-encoded video URL that we just decode in-place.15 *16 * If the decoded URL doesn't start with `https:` the source labels it as a17 * generic `Stream`; otherwise it's labelled `Mytv`.18 */19class MytvExtractor(20 private val http: HttpService,21) : AnimeExtractorService {2223 override val hostName: String get() = "mytv"2425 override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =26 extractVideos(headers) { videosFromUrl(url) }2728 private suspend fun videosFromUrl(url: String): List<DbVideo> {29 val response = http.request(HttpRequest(url))30 if (response.statusCode !in 200..299) return emptyList()31 val videos = mutableListOf<DbVideo>()32 ScriptTagRegex.findAll(response.body).forEach { match ->33 val text = match.groupValues[1]34 if (!text.contains("CreatePlayer(\"v")) return@forEach35 val rawUrl = text36 .substringAfter("\"v=", "")37 .substringBefore("\\u0026tp=video", "")38 if (rawUrl.isBlank()) return@forEach39 val decoded = rawUrl40 .replace("%26", "&")41 .replace("%3a", ":")42 .replace("%2f", "/")43 .replace("%3f", "?")44 .replace("%3d", "=")45 val label = if (decoded.contains("https:")) "Mytv" else "Stream"46 videos += DbVideo(url = decoded, quality = label, originalUrl = decoded)47 }48 return videos49 }50}51