StreamTapeExtractor.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/StreamTapeExtractor.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 * StreamTape embed extractor.12 *13 * Strategy (matches the Flutter implementation):14 * 1. Normalise the embed URL to `https://streamtape.com/e/<id>`.15 * 2. Fetch the embed page and find the script that builds `robotlink`.16 * 3. The script ends with `document.getElementById('robotlink').innerHTML = '<link>' + ('xcd<suffix>')`17 * where the runtime concatenates the two halves into a full media URL.18 * 4. Recombine and emit a single direct video URL.19 */20class StreamTapeExtractor(21 private val http: HttpService,22) : AnimeExtractorService {2324 override val hostName: String get() = "streamtape"2526 override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =27 extractVideos(headers) { videosFromUrl(url, headers) }2829 private suspend fun videosFromUrl(30 url: String,31 headers: Map<String, String> = emptyMap(),32 quality: String = "StreamTape",33 ): List<DbVideo> {34 val embedUrl = normaliseEmbed(url) ?: return emptyList()35 val response = http.request(HttpRequest(embedUrl, headers = headers))36 if (response.statusCode !in 200..299) return emptyList()37 val body = response.body38 val target = "document.getElementById('robotlink')"39 val scripts = ScriptTagRegex.findAll(body)40 .map { it.groupValues[1] }41 .filter { it.contains(target) }42 .toList()43 if (scripts.isEmpty()) return emptyList()44 val script = scripts.first()45 val afterAssign = script.substringAfter("$target.innerHTML = '", missingDelimiterValue = "")46 if (afterAssign.isBlank()) return emptyList()47 // The first chunk is everything up to the closing single-quote.48 val firstHalf = afterAssign.substringBefore("'", missingDelimiterValue = "")49 // The second half ships as `+ ('xcd<suffix>'` — the page strips the leading `xcd` literal50 // before appending. Mirror the Flutter behaviour: take what comes after `+ ('xcd`.51 val secondHalfRaw = afterAssign.substringAfter("+ ('xcd", missingDelimiterValue = "")52 val secondHalf = secondHalfRaw.substringBefore("'", missingDelimiterValue = "")53 val videoUrl = "https:$firstHalf$secondHalf"54 if (firstHalf.isBlank() || secondHalf.isBlank()) return emptyList()55 return listOf(DbVideo(url = videoUrl, quality = quality, originalUrl = videoUrl))56 }5758 private fun normaliseEmbed(url: String): String? {59 if (url.startsWith(EMBED_BASE)) return url60 // streamtape.com/<path>/<id> → grab the id segment (index 4 in the split).61 val parts = url.split("/")62 val id = parts.getOrNull(4) ?: return null63 return EMBED_BASE + id64 }6566 private companion object {67 const val EMBED_BASE = "https://streamtape.com/e/"68 }69}70