Skip to content

Commit

Permalink
[Fix]: 蜜柑计划映射修复
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Apr 10, 2024
1 parent ae9244c commit 3cdcabc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 68 deletions.
49 changes: 48 additions & 1 deletion lib-common/src/main/assets/config/mikan/mikan.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
12 changes: 9 additions & 3 deletions lib-common/src/main/java/com/xiaoyv/common/api/api/BgmWebApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>

/**
* 获取蜜柑计划ID映射表
*/
@GET("https://raw.githubusercontent.com/xiaoyvyv/bangumi-data/main/data/mikan/bangumi-mikan.json")
suspend fun queryMikanIdMapByGithub(): Map<String, String>

/**
* 获取蜜柑计划媒体详情
Expand Down
9 changes: 8 additions & 1 deletion lib-common/src/main/java/com/xiaoyv/common/api/dns/BgmDns.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
76 changes: 13 additions & 63 deletions lib-common/src/main/java/com/xiaoyv/common/helper/MiKanHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -40,71 +38,23 @@ object MiKanHelper {
val string = mikanMapFile.readText()
idMap.putAll(string.fromJson<Map<String, String>>().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<Long>()
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())
}
}

Expand Down

0 comments on commit 3cdcabc

Please sign in to comment.