Yokan / rebuild
Course 1 8624561

StreamWishExtractor.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/extractors/StreamWishExtractor.kt

implementation70 lines
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 * StreamWish embed extractor — also covers other Wish-family hosts (sfastwish,12 * doodstream-mirror, etc.) that ship the same packed-JS player.13 *14 * Strategy:15 *   1. Fetch the embed and find the `<script>` whose body contains "m3u8".16 *   2. If it's wrapped in `function(p,a,c,k,e,d)…` run the unpacker.17 *   3. Pull `source…file:"…m3u8…"` from the unpacked source.18 *   4. Fetch the master playlist with sensible Origin/Referer headers and emit19 *      one variant per `#EXT-X-STREAM-INF`.20 */21class StreamWishExtractor(22    private val http: HttpService,23) : AnimeExtractorService {2425    override val hostName: String get() = "streamwish"2627    override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =28        extractVideos(headers) { videosFromUrl(url, prefix = "StreamWish", headers = headers) }2930    private suspend fun videosFromUrl(31        url: String,32        prefix: String,33        headers: Map<String, String> = emptyMap(),34    ): List<DbVideo> {35        val response = http.request(HttpRequest(url, headers = headers))36        if (response.statusCode !in 200..299) return emptyList()37        val body = response.body38        val scripts = ScriptTagRegex.findAll(body)39            .map { it.groupValues[1] }40            .filter { it.contains("m3u8") }41            .toList()42        if (scripts.isEmpty()) return emptyList()4344        val script = scripts.first().let { raw ->45            if (raw.contains("function(p,a,c")) JsPacker.unpack(raw) ?: raw46            else raw47        }48        val masterUrl = script49            .substringAfter("source", "")50            .substringAfter("file:\"", "")51            .substringBefore("\"", "")52        if (masterUrl.isBlank()) return emptyList()5354        val embedHost = hostOf(url) ?: return emptyList()55        val masterHost = hostOf(masterUrl) ?: return emptyList()56        val playlistHeaders = buildMap {57            putAll(headers)58            put("Accept", "*/*")59            put("Host", masterHost)60            put("Origin", "https://$embedHost")61            put("Referer", "https://$embedHost/")62        }63        return http.m3u8Videos(64            masterUrl = masterUrl,65            labelPrefix = "$prefix - ",66            headers = playlistHeaders,67            requestHeaders = playlistHeaders,68        )69    }70}71

Search chapters, reference and source filenames.