From e6e0d4736bc0173a8d7c49b47dac309ef4f3ec7b Mon Sep 17 00:00:00 2001 From: Colter23 <13864833022@163.com> Date: Thu, 25 Nov 2021 20:29:55 +0800 Subject: [PATCH] fix: bug --- build.gradle.kts | 25 ++----------- .../BiliBiliTasker.kt | 2 +- .../Dynamic.kt | 5 ++- .../PluginMain.kt | 3 +- .../utils/HttpUtils.kt | 37 ++++++++----------- .../utils/ImgUtils.kt | 4 +- 6 files changed, 28 insertions(+), 48 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 359ba62..0aa4861 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,35 +1,16 @@ plugins { - val kotlinVersion = "1.5.10" + val kotlinVersion = "1.5.31" kotlin("jvm") version kotlinVersion kotlin("plugin.serialization") version kotlinVersion - id("net.mamoe.mirai-console") version "2.8.1" + id("net.mamoe.mirai-console") version "2.8.2" } group = "top.colter" -version = "2.0.2" +version = "2.0.3" repositories { mavenLocal() maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库 mavenCentral() } - -kotlin { - sourceSets { - all { - languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn") - languageSettings.useExperimentalAnnotation("kotlin.ExperimentalStdlibApi") - languageSettings.useExperimentalAnnotation("io.ktor.util.KtorExperimentalAPI") - languageSettings.useExperimentalAnnotation("kotlinx.serialization.InternalSerializationApi") - languageSettings.useExperimentalAnnotation("kotlinx.serialization.ExperimentalSerializationApi") - languageSettings.useExperimentalAnnotation("net.mamoe.mirai.console.util.ConsoleExperimentalApi") - } - test { - languageSettings.useExperimentalAnnotation("net.mamoe.mirai.console.ConsoleFrontEndImplementation") - } - } -} -dependencies { - -} diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/BiliBiliTasker.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/BiliBiliTasker.kt index 4028122..70f7332 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/BiliBiliTasker.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/BiliBiliTasker.kt @@ -200,7 +200,7 @@ object DynamicTasker : CoroutineScope by PluginMain.childScope("DynamicTasker") seleniumMutex.withLock { list.sendMessage { di.build(it, color) } history.add(describe.dynamicId) - lastDynamic = di.timestamp +// lastDynamic = di.timestamp } } } diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt index 45d1da8..818aeb4 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt @@ -1,6 +1,6 @@ package top.colter.mirai.plugin.bilibili -//import top.colter.mirai.plugin.bilibili.utils.getScreenshot +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.decodeFromString import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.message.data.Message @@ -19,6 +19,7 @@ import java.time.LocalDateTime import java.time.OffsetDateTime import java.time.format.DateTimeFormatter +@OptIn(ExperimentalSerializationApi::class) inline fun String.decode(): T = json.decodeFromString(this) val DynamicInfo.did get() = describe.dynamicId @@ -165,6 +166,8 @@ fun String.buildContent(type: Int, dynamicInfo: DynamicInfo): List decode().bufferedImages(dynamicInfo) DynamicType.DELETE -> listOf(ImgUtils.infoContent("源动态已被作者删除")) DynamicType.SKETCH -> decode().bufferedImages(dynamicInfo) + DynamicType.LIVE, DynamicType.LIVE_ING -> listOf(ImgUtils.infoContent("直播")) + DynamicType.LIVE_END -> listOf(ImgUtils.infoContent("直播结束了")) else -> { if (dynamicInfo.link == "") { dynamicInfo.link = "https://t.bilibili.com/${dynamicInfo.did}" diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/PluginMain.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/PluginMain.kt index 4e74443..2ce249f 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/PluginMain.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/PluginMain.kt @@ -1,5 +1,6 @@ package top.colter.mirai.plugin.bilibili +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister @@ -14,7 +15,7 @@ object PluginMain : KotlinPlugin( JvmPluginDescription( id = "top.colter.bilibili-dynamic-mirai-plugin", name = "BilibiliDynamic", - version = "2.0.2" + version = "2.0.3" ) { author("Colter") info( diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/HttpUtils.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/HttpUtils.kt index ec3c346..48bd670 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/HttpUtils.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/HttpUtils.kt @@ -1,19 +1,19 @@ package top.colter.mirai.plugin.bilibili.utils import kotlinx.serialization.json.JsonElement +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import top.colter.mirai.plugin.bilibili.PluginMain import top.colter.mirai.plugin.bilibili.data.ResultData -import java.net.URI -import java.net.http.HttpClient -import java.net.http.HttpRequest -import java.net.http.HttpResponse import java.time.Duration class HttpUtils { private val cookie: String = PluginMain.sessData - private var client: HttpClient = HttpClient.newBuilder().connectTimeout(Duration.ofMillis(10000)).build() + private var client: OkHttpClient = OkHttpClient().newBuilder().connectTimeout(Duration.ofMillis(20000)).build() private val ua = listOf( "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);", @@ -25,32 +25,27 @@ class HttpUtils { "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25" ) - private fun sendRequest(request: HttpRequest): JsonElement { - val bodyHandler: HttpResponse.BodyHandler = HttpResponse.BodyHandlers.ofString() - val response: HttpResponse = client.send(request, bodyHandler) - val body: String = response.body() + private fun sendRequest(request: Request): JsonElement { + val body: String = client.newCall(request).execute().body!!.string() return json.parseToJsonElement(body) } fun get(url: String): JsonElement { - val request: HttpRequest = - HttpRequest.newBuilder(URI.create(url)) - .header("cookie", cookie) - .header("Content-Type", "application/json; charset=utf-8") - .header("user-agent", ua.random()) - .GET().build() - + val request = Request.Builder().url(url) + .header("cookie", cookie) + .header("Content-Type", "application/json; charset=utf-8") + .header("user-agent", ua.random()) + .get().build() return sendRequest(request) } fun post(url: String, postBody: String): JsonElement { - val request: HttpRequest = - HttpRequest.newBuilder(URI.create(url)) + val media = "application/x-www-form-urlencoded; charset=utf-8" + val request = Request.Builder().url(url) .header("cookie", cookie) - .header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") + .header("Content-Type", media) .header("user-agent", ua.random()) - .POST(HttpRequest.BodyPublishers.ofString(postBody)).build() - + .post(postBody.toRequestBody(media.toMediaTypeOrNull())).build() return sendRequest(request) } diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/ImgUtils.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/ImgUtils.kt index 211b3cd..1d706fb 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/ImgUtils.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/utils/ImgUtils.kt @@ -177,7 +177,7 @@ object ImgUtils { val margin = 10 val rw = imgWidth - margin * 2 val rh = height - margin * 2 - bgG2.color = Color.WHITE + bgG2.color = Color(255,255,255, 200) bgG2.fillRoundRect(margin, margin, rw, rh, 20, 20) bgG2.color = Color(238, 238, 238) bgG2.drawRoundRect(margin - 1, margin - 1, rw + 1, rh + 1, 20, 20) @@ -465,7 +465,7 @@ object ImgUtils { } fun infoContent(text: String): BufferedImage { - val infoBi = BufferedImage(imgWidth, 30, BufferedImage.TYPE_INT_ARGB) + val infoBi = BufferedImage(imgWidth, 50, BufferedImage.TYPE_INT_ARGB) val infoG2 = infoBi.createGraphics() infoG2.setRenderingHints(renderingHints) infoG2.font = font.deriveFont(25f)