diff --git a/src/main/kotlin/xyz/cssxsh/bilibili/BiliClient.kt b/src/main/kotlin/xyz/cssxsh/bilibili/BiliClient.kt index 953f6501..01cab76d 100644 --- a/src/main/kotlin/xyz/cssxsh/bilibili/BiliClient.kt +++ b/src/main/kotlin/xyz/cssxsh/bilibili/BiliClient.kt @@ -17,7 +17,7 @@ import kotlinx.serialization.json.Json import xyz.cssxsh.bilibili.api.SPACE import java.io.IOException -class BiliClient(val ignore: suspend (exception: Throwable) -> Boolean = DefaultIgnore, interval: Long = 10 * 1000L) { +open class BiliClient { companion object { val Json = Json { prettyPrint = true @@ -29,6 +29,8 @@ class BiliClient(val ignore: suspend (exception: Throwable) -> Boolean = Default val DefaultIgnore: suspend (Throwable) -> Boolean = { it is IOException || it is HttpRequestTimeoutException } } + protected open val ignore: suspend (exception: Throwable) -> Boolean = DefaultIgnore + private val cookiesStorage = AcceptAllCookiesStorage() private fun client() = HttpClient(OkHttp) { @@ -51,20 +53,18 @@ class BiliClient(val ignore: suspend (exception: Throwable) -> Boolean = Default ContentEncoding() } - internal val mutex = BiliApiMutex(interval) + internal open val mutex = BiliApiMutex(10 * 1000L) suspend fun useHttpClient(block: suspend (HttpClient) -> T): T = supervisorScope { - client().use { - while (isActive) { - runCatching { - block(it) - }.onFailure { - if (ignore(it).not()) throw it - }.onSuccess { - return@use it - } + while (isActive) { + runCatching { + client().use { block(it) } + }.onFailure { + if (ignore(it).not()) throw it + }.onSuccess { + return@supervisorScope it } - throw CancellationException() } + throw CancellationException() } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliHelperPlugin.kt b/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliHelperPlugin.kt index 6f1b1e31..d651966c 100644 --- a/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliHelperPlugin.kt +++ b/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliHelperPlugin.kt @@ -19,8 +19,6 @@ object BiliHelperPlugin : KotlinPlugin( } ) { - val client by lazy { BiliClient(interval = BiliHelperSettings.api * 1000) } - lateinit var driver: RemoteWebDriver private set diff --git a/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliUtils.kt b/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliUtils.kt index 87c3bf4a..f3077dce 100644 --- a/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliUtils.kt +++ b/src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliUtils.kt @@ -26,7 +26,17 @@ import java.time.format.DateTimeFormatter internal val logger by BiliHelperPlugin::logger -internal val client by BiliHelperPlugin::client +internal val client by lazy { + object : BiliClient() { + override val ignore: suspend (exception: Throwable) -> Boolean = { throwable -> + super.ignore(throwable).also { + if (it) logger.warning { "Ignore $throwable" } + } + } + + override val mutex: BiliApiMutex = BiliApiMutex(interval = BiliHelperSettings.api * 1000) + } +} internal val ImageCache by lazy { File(BiliHelperSettings.cache) }