Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Better Way To Fetch YHDMGO / YHDMZ Video Source #12

Closed
cubevlmu opened this issue Dec 31, 2023 · 1 comment
Closed

A Better Way To Fetch YHDMGO / YHDMZ Video Source #12

cubevlmu opened this issue Dec 31, 2023 · 1 comment

Comments

@cubevlmu
Copy link

cubevlmu commented Dec 31, 2023

作者您好,对于yhdmgo和yhdmz类似网站,可以使用这套算法实现,而不需要使用webview
这里使用的是计算cookie仿造源站点的请求方法,测试了对于yhdmgo和我这里使用的yhdmz(iyhdmm)都是没问题的
比起webview,我认为有更优的性能。希望作者大大采纳。

`

fun calculateT2(key: String): String {
    var timestampString: String
    var lastDigitKey = key

    while (true) {
        val now = Instant.now()
        val millis = now.toEpochMilli()
        timestampString = millis.toString()
        lastDigitKey = timestampString.substring(timestampString.length - 1)

        if (timestampString.contains(lastDigitKey)) {
            break
        }
    }

    return timestampString
}

private fun decodeData(encodedData: String): String {
    if ('{' !in encodedData) {
        var decodedString = ""
        val magicNumber = 1561
        val length = encodedData.length

        for (i in 0 until length step 2) {
            var hexValue = encodedData.substring(i, i + 2).toInt(16)
            hexValue = (hexValue + 1048576 - magicNumber - (length / 2 - 1 - i / 2).toInt()) % 256
            decodedString = hexValue.toChar() + decodedString
        }

        return decodedString
    }

    return encodedData
}


private fun generateKey(): String {
    val timestamp = (getCurrentTimestamp() / 1000 shr 0x11 + 1).toLong()
    val key = (timestamp * 21 + 154) *
            (timestamp % 64 + 13) *
            (timestamp % 32 + 34) *
            (timestamp % 16 + 87) *
            (timestamp % 8 + 65) + 751

    return key.toString()
}

private fun getCurrentTimestamp(): Long {
    return System.currentTimeMillis()
}

private fun calculateK2(a: Long): String {
    val cookieValue = a / 1000
    val timestamp = (cookieValue.toInt() ushr 0x5).toLong()
    val randomValue = 35236
    val key = (timestamp * (timestamp % 256 + 1) + randomValue) *
            (timestamp % 128 + 1) *
            (timestamp % 16 + 1) + timestamp

    return key.toString()
}

companion object {
    const val BASE_URL = "https://www.iyhdmm.com"
}

private val urlFlow = MutableSharedFlow<String>()

suspend fun requestVideoUrl(animeId : Int, index : Int, episode: Int) : String? {
    val respondContent = AsyncHttpRespond()
    val cookies = withContext(Dispatchers.IO) {
        AsyncHttpTask("${BASE_URL}/vp/${animeId}-${index}-$episode.html")
            .submitRequest()
            .getHeaderContent(respondContent, RequestFetchId.GET_COOKIES)
            .endRequest()

        respondContent.headerContent ?: ""
    }

    val cookieMap = HashMap<String, String>()
    for (cookiePair in cookies.split(";")) {
        if (!cookiePair.contains("=")) continue
        val pair = cookiePair.split("=")
        cookieMap[pair[0]] = pair[1]
    }

    val m2t = generateKey();
    val k2 = calculateK2(cookieMap["t1"]!!.toLong());
    val t2 = calculateT2(k2);
    Log.d("AniCoreNet", "[Sakura] M2t Below : $m2t");
    Log.d("AniCoreNet", "[Sakura] K2 Below : $k2");
    Log.d("AniCoreNet", "[Sakura] T2 Below : $t2");

    val respond = AsyncHttpRespond()
    val videoUrl = withContext(Dispatchers.IO) {
        val url =
            "https://www.iyhdmm.com/playurl?aid=" + animeId + "&playindex=" + index + "&epindex=" + episode + "&r=" + Random.nextFloat()
        Log.d("AniFlyNet", "Request Url : $url")

        val cookieBuilder = StringBuilder()
        val cookieHeader = cookieBuilder
            .append("t1=").append(cookieMap["t1"]!!).append(";")
            .append("k1=").append(cookieMap["k1"]!!).append(";")
            .append("k2=").append(k2).append(";")
            .append("t2=").append(t2).append(";")
            .append("m2t=").append(m2t).append(";")
            .toString()
        Log.d("AniCoreNet", "Cookie Built Result $cookieHeader")

        AsyncHttpTask(url)
            .addHeader("Referer", "https://www.iyhdmm.com/vp/22203-1-0.html")
            .addHeader("Cookie", cookieHeader)
            .submitHeaders()
            .submitRequest()
            .getRespondContent(respond)
            .endRequest()

        respond.respondContent ?: ""
    }

    val element = Json.parseToJsonElement(decodeData(videoUrl))
    val vul = withContext(Dispatchers.IO) {
        URLDecoder.decode(element.jsonObject["vurl"].toString(), "UTF-8").replace("\"", "")
    }

      return vul
}

`

@xioneko
Copy link
Owner

xioneko commented Jan 1, 2024

感谢,这个方案看起来不错!我抽空研究一下

xioneko added a commit that referenced this issue Jan 13, 2024
@xioneko xioneko closed this as completed May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants