Yokan / rebuild
Course 1 8624561

DoodExtractor.kt

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

implementation83 lines
1package eu.friedcutlet.yokan.services.media.extractors23import eu.friedcutlet.yokan.database.DbVideo4import eu.friedcutlet.yokan.services.extensions.js.md55import eu.friedcutlet.yokan.services.http.HttpRequest6import eu.friedcutlet.yokan.services.http.HttpResponse7import eu.friedcutlet.yokan.services.http.HttpService8import eu.friedcutlet.yokan.services.media.AnimeExtractorService9import eu.friedcutlet.yokan.services.media.ExtractorResult10import eu.friedcutlet.yokan.services.media.extractVideos11import eu.friedcutlet.yokan.util.nowMillis12import kotlin.random.Random1314/**15 * DoodStream / mirror embed extractor.16 *17 * Strategy:18 *   1. GET the embed; remember the post-redirect URL (Dood serves the actual19 *      player from one of dozens of mirror domains).20 *   2. Locate the `'/pass_md5/<key>'` literal in the page, derive the trailing21 *      token from the last path segment.22 *   3. GET `https://<host>/pass_md5/<key>` with the embed as `Referer` — the23 *      response body is the URL prefix Dood expects us to glue together with24 *      a random 10-char salt, the token and an epoch timestamp.25 *   4. Emit one direct video with `User-Agent` + `Referer` headers.26 */27class DoodExtractor(28    private val http: HttpService,29) : AnimeExtractorService {3031    override val hostName: String get() = "dood"3233    override suspend fun extract(url: String, headers: Map<String, String>): ExtractorResult =34        extractVideos(headers) { videosFromUrl(url, redirect = true) }3536    private suspend fun videosFromUrl(37        url: String,38        quality: String? = null,39        redirect: Boolean = true,40    ): List<DbVideo> {41        val resolvedQuality = quality ?: ("Doodstream" + if (redirect) " mirror" else "")42        val response = http.request(HttpRequest(url))43        if (response.statusCode !in 200..299) return emptyList()44        // We don't currently expose the final post-redirect URL through45        // HttpResponse, so use the input URL as the referer/page URL for the46        // header set. Mirror hosts still work because the regex below picks47        // their hostname out of the embedded `/pass_md5/` literal.48        val pageUrl = url49        val doodHost = hostOf(pageUrl) ?: return emptyList()50        val body = response.body51        if (!body.contains("'/pass_md5/")) return emptyList()52        val md5 = body.substringAfter("'/pass_md5/", "").substringBefore("',", "")53        if (md5.isBlank()) return emptyList()54        val token = md5.substringAfterLast('/', missingDelimiterValue = md5)5556        val randomSalt = randomString(10)57        val expiry = nowMillis()58        val passResponse = http.request(59            HttpRequest(60                url = "https://$doodHost/pass_md5/$md5",61                headers = mapOf("Referer" to pageUrl),62            ),63        )64        if (passResponse.statusCode !in 200..299) return emptyList()65        val videoUrl = "${passResponse.body}$randomSalt?token=$token&expiry=$expiry"66        return listOf(67            DbVideo(68                url = videoUrl,69                quality = resolvedQuality,70                originalUrl = pageUrl,71                headers = mapOf(72                    "User-Agent" to "Mangayomi",73                    "Referer" to "https://$doodHost/",74                ),75            ),76        )77    }7879    private fun randomString(length: Int): String {80        val allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"81        return buildString(length) { repeat(length) { append(allowed[Random.nextInt(allowed.length)]) } }82    }83}84

Search chapters, reference and source filenames.