From 3cdcabc04ed3e49a9b7c85e7e0bf7e93a10654ce Mon Sep 17 00:00:00 2001 From: why Date: Wed, 10 Apr 2024 18:53:37 +0800 Subject: [PATCH] =?UTF-8?q?[Fix]:=20=E8=9C=9C=E6=9F=91=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/assets/config/mikan/mikan.json | 49 +++++++++++- .../com/xiaoyv/common/api/api/BgmWebApi.kt | 12 ++- .../java/com/xiaoyv/common/api/dns/BgmDns.kt | 9 ++- .../com/xiaoyv/common/helper/MiKanHelper.kt | 76 ++++--------------- 4 files changed, 78 insertions(+), 68 deletions(-) diff --git a/lib-common/src/main/assets/config/mikan/mikan.json b/lib-common/src/main/assets/config/mikan/mikan.json index caebd8d8..3e630a21 100644 --- a/lib-common/src/main/assets/config/mikan/mikan.json +++ b/lib-common/src/main/assets/config/mikan/mikan.json @@ -2458,5 +2458,52 @@ "3275": "340167", "3276": "432583", "3277": "465773", - "3278": "383251" + "3278": "383251", + "3280": "414987", + "3285": "448655", + "3289": "457945", + "3290": "283643", + "3291": "425909", + "3292": "434144", + "3293": "407234", + "3294": "468758", + "3295": "426947", + "3296": "405785", + "3297": "471926", + "3298": "431767", + "3299": "342667", + "3300": "449562", + "3302": "459725", + "3304": "411276", + "3305": "404809", + "3306": "365952", + "3307": "444403", + "3308": "442176", + "3309": "410939", + "3311": "461338", + "3312": "472386", + "3313": "407133", + "3315": "439850", + "3316": "391866", + "3318": "462608", + "3319": "456619", + "3322": "405660", + "3323": "455345", + "3324": "467919", + "3325": "364522", + "3326": "433072", + "3327": "444339", + "3328": "357960", + "3329": "416777", + "3330": "372010", + "3331": "455835", + "3332": "425978", + "3336": "448478", + "3338": "406251", + "3339": "441586", + "3340": "460503", + "3341": "407573", + "3342": "425222", + "3343": "425587", + "3344": "444557" } \ No newline at end of file diff --git a/lib-common/src/main/java/com/xiaoyv/common/api/api/BgmWebApi.kt b/lib-common/src/main/java/com/xiaoyv/common/api/api/BgmWebApi.kt index c2d314e6..d33868b8 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/api/api/BgmWebApi.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/api/api/BgmWebApi.kt @@ -160,10 +160,16 @@ interface BgmWebApi { suspend fun createToken(@Body param: CreateTokenParam = CreateTokenParam()): String /** - * 获取蜜柑计划首页信息 + * 获取蜜柑计划ID映射表 */ - @GET("https://mikanime.tv") - suspend fun queryMikanHome(): Document + @GET("https://cdn.jsdelivr.net/gh/xiaoyvyv/bangumi-data@latest/data/mikan/bangumi-mikan.json") + suspend fun queryMikanIdMapByJsdelivr(): Map + + /** + * 获取蜜柑计划ID映射表 + */ + @GET("https://raw.githubusercontent.com/xiaoyvyv/bangumi-data/main/data/mikan/bangumi-mikan.json") + suspend fun queryMikanIdMapByGithub(): Map /** * 获取蜜柑计划媒体详情 diff --git a/lib-common/src/main/java/com/xiaoyv/common/api/dns/BgmDns.kt b/lib-common/src/main/java/com/xiaoyv/common/api/dns/BgmDns.kt index 7af89ce0..07f16856 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/api/dns/BgmDns.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/api/dns/BgmDns.kt @@ -50,6 +50,13 @@ class BgmDns : Dns { } companion object { - const val DEFAULT_HOSTS = "172.67.98.15 share.dmhy.org\n172.67.152.186 bangumi.moe\n" + /** + * 104.16.86.20 cdn.jsdelivr.net + */ + val DEFAULT_HOSTS = """ + 172.67.98.15 share.dmhy.org + 172.67.152.186 bangumi.moe + 185.199.110.133 raw.githubusercontent.com + """.trimIndent() } } \ No newline at end of file diff --git a/lib-common/src/main/java/com/xiaoyv/common/helper/MiKanHelper.kt b/lib-common/src/main/java/com/xiaoyv/common/helper/MiKanHelper.kt index 58aa6d81..d2523de8 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/helper/MiKanHelper.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/helper/MiKanHelper.kt @@ -2,14 +2,12 @@ package com.xiaoyv.common.helper import com.blankj.utilcode.util.FileIOUtils import com.blankj.utilcode.util.FileUtils -import com.blankj.utilcode.util.GsonUtils import com.blankj.utilcode.util.ResourceUtils import com.xiaoyv.blueprint.kts.launchProcess import com.xiaoyv.blueprint.kts.toJson import com.xiaoyv.common.api.BgmApiManager import com.xiaoyv.common.kts.debugLog import com.xiaoyv.common.kts.fromJson -import com.xiaoyv.widget.kts.orEmpty import com.xiaoyv.widget.kts.workDirOf import kotlinx.coroutines.Dispatchers @@ -40,71 +38,23 @@ object MiKanHelper { val string = mikanMapFile.readText() idMap.putAll(string.fromJson>().orEmpty()) - // 存在的最大 ID - val localMaxId = idMap.maxOf { it.key.toLongOrNull().orEmpty() } - - debugLog { "Mikan, local max id = $localMaxId" } - - // 同步 - checkOrSyncMikanId(localMaxId) - } - } - - private suspend fun checkOrSyncMikanId(localMaxId: Long) { - val mikanId = BgmApiManager.bgmWebApi.queryMikanHome() - .select(".m-week-square a") - .maxOf { - it.attr("href") - .substringAfterLast("/") - .toLongOrNull() - .orEmpty() - } - - debugLog { "Mikan, remote max id = $mikanId" } - - if (localMaxId >= mikanId) { - debugLog { "Mikan, 无需同步!" } - return - } - - // 待同步的集合 - val needSyncIds = arrayListOf() - for (id in localMaxId..mikanId) { - needSyncIds.add(id) - } - - debugLog { "Mikan, 待更新数据: ${needSyncIds.toJson()}" } - - // 开始同步 - needSyncIds.chunked(100).forEach { - launchProcess(Dispatchers.IO) { - it.forEach { id -> - syncMiKan(id) + // 更新远程数据 + runCatching { + idMap.putAll(BgmApiManager.bgmWebApi.queryMikanIdMapByJsdelivr()) + }.onFailure { + debugLog { "Mikan, js-cdn sync error: " + it.printStackTrace() } + + // cdn 失败用 github + runCatching { + idMap.putAll(BgmApiManager.bgmWebApi.queryMikanIdMapByGithub()) + }.onFailure { + debugLog { "Mikan, github sync error: " + it.printStackTrace() } } } - } - } - - /** - * 同步 ID 映射,并保存本地 - */ - private suspend fun syncMiKan(id: Long) { - runCatching { - val document = BgmApiManager.bgmWebApi.queryMikanDetail(id.toString()) - val links = document.select("a.w-other-c") - val element = links.find { it.text().contains("bgm.tv/subject") } - requireNotNull(element) { "MiKan: $id, Bgm id is empty!" } - val bgmId = element.text().substringAfterLast("/").trim() - idMap[id.toString()] = bgmId - val sortedMap = idMap.toSortedMap { p0, p1 -> - p0.toInt() - p1.toInt() - } - debugLog { "Mikan, 同步成功: $id" } + debugLog { "Mikan, Maps: " + idMap.size } - FileIOUtils.writeFileFromString(mikanMapFile, GsonUtils.toJson(sortedMap)) - }.onFailure { - debugLog { "Mikan, 同步错误: $id, error -> $it" } + FileIOUtils.writeFileFromString(mikanMapFile, idMap.toJson()) } }