Yokan / rebuild
Course 1 8624561

OkruExtractor.kt

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

implementation49 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 * OK.ru video embed extractor.12 *13 * The player ships its config in a `<div data-options="…">` attribute. The14 * value is a giant JSON blob with all quote characters double-escaped — we15 * only need the `ondemandHls` URL, which is encoded as `&` between query16 * params.17 *18 * The HLS master is then parsed normally; relative variant URLs are resolved19 * against the master's directory.20 */21class OkruExtractor(22    private val http: HttpService,23) : AnimeExtractorService {2425    override val hostName: String get() = "okru"2627    override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =28        extractVideos(headers) { videosFromUrl(url) }2930    private suspend fun videosFromUrl(31        url: String,32        prefix: String = "",33    ): List<DbVideo> {34        val response = http.request(HttpRequest(url))35        if (response.statusCode !in 200..299) return emptyList()36        val attribute = DATA_OPTIONS_REGEX.find(response.body)?.groupValues?.get(1).orEmpty()37        if (attribute.isBlank() || !attribute.contains("ondemandHls")) return emptyList()38        val playlistUrl = attribute39            .substringAfter("ondemandHls\\\":\\\"", "")40            .substringBefore("\\\"", "")41            .replace("\\\\u0026", "&")42        if (playlistUrl.isBlank()) return emptyList()43        return http.m3u8Videos(masterUrl = playlistUrl, labelPrefix = prefix + "Okru:")44    }4546    private companion object {47        val DATA_OPTIONS_REGEX = Regex("""data-options="([^"]+)"""", RegexOption.IGNORE_CASE)48    }49}50

Search chapters, reference and source filenames.