StreamlareExtractor.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/StreamlareExtractor.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 * Streamlare / slwatch.co extractor.12 *13 * Posts the trailing id segment to `/api/video/stream/get` and reads the JSON14 * envelope. Two response shapes:15 * - `type == "hls"` → parse the master URL out of `"file":"…"`, fan out16 * variants via the m3u8 helper.17 * - Anything else → walk every `"label":"…"`/`"file":"…"` pair, follow the18 * API URL with a POST and emit each as a single video.19 */20class StreamlareExtractor(21 private val http: HttpService,22) : AnimeExtractorService {2324 override val hostName: String get() = "streamlare"2526 override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =27 extractVideos(headers) { videosFromUrl(url) }2829 private suspend fun videosFromUrl(30 url: String,31 prefix: String = "",32 suffix: String = "",33 ): List<DbVideo> {34 val id = url.substringAfterLast('/', missingDelimiterValue = "")35 if (id.isBlank()) return emptyList()36 val playlistResponse = http.request(37 HttpRequest(38 url = API_ENDPOINT,39 method = "POST",40 headers = mapOf("Content-Type" to "application/json"),41 body = "{\"id\":\"$id\"}",42 ),43 )44 if (playlistResponse.statusCode !in 200..299) return emptyList()45 val playlist = playlistResponse.body46 val type = playlist.substringAfter("\"type\":\"", "").substringBefore("\"", "")4748 return if (type == "hls") {49 extractHls(playlist, prefix, suffix)50 } else {51 extractLabelled(playlist, prefix, suffix)52 }53 }5455 private suspend fun extractHls(playlist: String, prefix: String, suffix: String): List<DbVideo> {56 val masterUrl = playlist57 .substringAfter("\"file\":\"", "")58 .substringBefore("\"", "")59 .replace("""\/""", "/")60 if (masterUrl.isBlank()) return emptyList()61 val master = http.request(HttpRequest(masterUrl))62 if (master.statusCode !in 200..299) return emptyList()63 // The master URL ends in `master.m3u8`; the source resolves relative64 // variant paths against everything before that filename.65 val resolveBase = masterUrl.substringBefore("master.m3u8", missingDelimiterValue = masterUrl.substringBeforeLast('/') + "/")66 return parseM3u8MasterToVideos(67 masterUrl = masterUrl,68 masterPlaylist = master.body,69 labelPrefix = labelPrefix(prefix),70 labelSuffix = suffix,71 resolveBase = resolveBase,72 )73 }7475 private suspend fun extractLabelled(playlist: String, prefix: String, suffix: String): List<DbVideo> {76 // Original Flutter source slices on `"label":"` repeatedly. We mirror77 // the pattern but use a tighter regex over the JSON envelope so we78 // don't double-slice between label/file pairs.79 val pairs = ENTRY_REGEX.findAll(playlist).toList()80 if (pairs.isEmpty()) return emptyList()81 val videos = mutableListOf<DbVideo>()82 for (match in pairs) {83 val label = match.groupValues[1]84 val apiUrl = match.groupValues[2].replace("\\", "")85 val mp4 = http.request(HttpRequest(apiUrl, method = "POST"))86 if (mp4.statusCode !in 200..299) continue87 // The Flutter implementation reads the post-redirect URL — we88 // don't expose that, so reuse the API URL as the playable handle.89 // Most Streamlare hosts return playable mp4 from the same URL.90 val quality = buildString {91 if (prefix.isNotBlank()) append(prefix).append(' ')92 append("Streamlare:$label")93 if (suffix.isNotBlank()) append(' ').append(suffix)94 }95 videos += DbVideo(url = apiUrl, quality = quality, originalUrl = apiUrl)96 }97 return videos98 }99100 private fun labelPrefix(prefix: String): String = buildString {101 if (prefix.isNotBlank()) append(prefix).append(' ')102 append("Streamlare:")103 }104105 private companion object {106 const val API_ENDPOINT = "https://slwatch.co/api/video/stream/get"107 // Captures `"label":"<lbl>",…,"file":"<url>"` pairs in either order.108 val ENTRY_REGEX = Regex(""""label"\s*:\s*"([^"]+)"[^}]*?"file"\s*:\s*"([^"]+)"""")109 }110}111