From 769ec7fe1ba875d2eb878ec85d0b52e7ecbecaeb Mon Sep 17 00:00:00 2001 From: Colter23 <13864833022@163.com> Date: Tue, 21 Dec 2021 20:55:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8A=A8=E6=80=81=E6=96=87?= =?UTF-8?q?=E5=AD=97=E8=BF=87=E9=95=BF=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 4 ++-- .../BiliBiliTasker.kt | 7 ++---- .../Dynamic.kt | 3 +++ .../Listener.kt | 24 +++++++++++++++++++ .../top.colter.mirai.plugin.bilibili/Live.kt | 4 +++- .../PluginMain.kt | 2 +- .../data/Api.kt | 2 ++ .../data/BiliPluginConfig.kt | 2 ++ .../data/DynamicData.kt | 6 +++++ .../utils/ImgUtils.kt | 18 +++++++++++++- 10 files changed, 62 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c305767..754c074 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - val kotlinVersion = "1.5.31" + val kotlinVersion = "1.6.0" kotlin("jvm") version kotlinVersion kotlin("plugin.serialization") version kotlinVersion @@ -7,7 +7,7 @@ plugins { } group = "top.colter" -version = "2.0.5" +version = "2.0.6" repositories { mavenLocal() 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 c3188da..b338a1c 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/BiliBiliTasker.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/BiliBiliTasker.kt @@ -27,7 +27,7 @@ object DynamicTasker : CoroutineScope by PluginMain.childScope("DynamicTasker") private val seleniumMutex = Mutex() // private val httpMutex = Mutex() - private var httpUtils = HttpUtils() + val httpUtils = HttpUtils() val mutex = Mutex() val dynamic: MutableMap by BiliSubscribeData::dynamic @@ -207,7 +207,7 @@ object DynamicTasker : CoroutineScope by PluginMain.childScope("DynamicTasker") } } }.onSuccess { - delay((interval..interval + 5000L).random()) + delay((interval..(interval + 5000L)).random()) }.onFailure { logger.error("ERROR $it") findContact(BiliPluginConfig.admin)?.sendMessage("动态检测失败\n" + it.message) @@ -364,9 +364,6 @@ fun findContact(del: String): Contact? = synchronized(contactMap) { for (stranger in bot.strangers) { if (stranger.id == delegate) return@compute stranger } - for (friend in bot.friends) { - if (friend.id == delegate) return@compute friend - } for (group in bot.groups) { for (member in group.members) { if (member.id == delegate) return@compute member 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 35f4d12..6007289 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt @@ -27,6 +27,8 @@ val DynamicInfo.uid get() = describe.uid val DynamicInfo.profile get() = describe.profile?.decode() val DynamicInfo.uname get() = profile?.user?.uname val DynamicInfo.timestamp get() = describe.timestamp +val DynamicInfo.type get() = describe.type +val DynamicInfo.originType get() = describe.originType val DynamicInfo.time: String get() = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss") .format(LocalDateTime.ofEpochSecond(describe.timestamp, 0, OffsetDateTime.now().offset)) @@ -155,6 +157,7 @@ fun DynamicLive.getContent(dynamicInfo: DynamicInfo): String { } + fun String.buildContent(type: Int, dynamicInfo: DynamicInfo): List { return when (type) { DynamicType.REPLY -> decode().bufferedImages(dynamicInfo) diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Listener.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Listener.kt index 0ff34cb..a5bd8cd 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Listener.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Listener.kt @@ -3,15 +3,39 @@ package top.colter.mirai.plugin.bilibili import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.sync.withLock +import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.event.globalEventChannel import net.mamoe.mirai.event.selectMessagesUnit import net.mamoe.mirai.utils.MiraiExperimentalApi +@OptIn(ConsoleExperimentalApi::class) internal object Listener : CoroutineScope by PluginMain.childScope("Listener") { @OptIn(MiraiExperimentalApi::class) fun subscribe() { +// globalEventChannel().subscribeAlways { +// message.filterIsInstance().map { it.target }.first { it == bot.id }.let { _ -> +// println("At") +// if (message.content.contains("下载图片")){ +// println("下载图片") +// +// message[QuoteReply.Key]?.source?.originalMessage?.content.let { quote -> +// val dynamicRegex = """(?<=t\.bilibili\.com/)(\d+)""".toRegex() +// val videoRegex = """((av|AV)\d+|BV[0-9A-z]{8,12})""".toRegex() +// dynamicRegex.find(quote!!)?.value.let { +// println("动态:$it") +// val dynamic = DynamicTasker.httpUtils.getAndDecode(DYNAMIC_DETAIL).dynamic +// if (dynamic?.type == DynamicType.DELETE) +// } +// videoRegex.find(quote)?.value.let{ +// println("视频:$it") +// } +// } +// } +// } +// } + globalEventChannel().subscribeAlways { val c = DynamicTasker.dynamic[uid]?.contacts?.get(subject) if (c == null) { diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Live.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Live.kt index 3cdc1b0..1319c9e 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/Live.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/Live.kt @@ -41,5 +41,7 @@ suspend fun LiveInfo.buildTextLive(contact: Contact): Message { suspend fun LiveInfo.buildImageLive(contact: Contact): Message { val link = "https://live.bilibili.com/${roomId}" val file = buildLiveImageMessage(title, cover, uname, face, "#d3edfa", "live/${uid}/${liveTime}.png") - return (file.uploadAsImage(contact) + "$uname@$uid@直播\n$time\n$link") + val msg = BiliPluginConfig.pushTemplate.replace("{name}", uname).replace("{uid}",uid.toString()) + .replace("{type}","直播").replace("{time}",time).replace("{link}",link) + return (file.uploadAsImage(contact) + msg) } 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 c1bb7d1..674d802 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/PluginMain.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/PluginMain.kt @@ -13,7 +13,7 @@ object PluginMain : KotlinPlugin( JvmPluginDescription( id = "top.colter.bilibili-dynamic-mirai-plugin", name = "BilibiliDynamic", - version = "2.0.5" + version = "2.0.6" ) { author("Colter") info( diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/Api.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/Api.kt index c1c7e94..a13bf86 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/Api.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/Api.kt @@ -7,6 +7,8 @@ const val NEW_DYNAMIC = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/ const val DYNAMIC_LIST = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history?visitor_uid=0&offset_dynamic_id=0&need_top=0&platform=web&host_uid=" +const val DYNAMIC_DETAIL = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/get_dynamic_detail?dynamic_id=" + const val LIVE_LIST = "https://api.live.bilibili.com/xlive/web-ucenter/v1/xfetter/GetWebList?page=1&page_size=20" // 参数 uid diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/BiliPluginConfig.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/BiliPluginConfig.kt index a42b9ab..8b0509c 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/BiliPluginConfig.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/BiliPluginConfig.kt @@ -1,5 +1,6 @@ package top.colter.mirai.plugin.bilibili.data +import kotlinx.serialization.Serializer import net.mamoe.mirai.console.data.ReadOnlyPluginConfig import net.mamoe.mirai.console.data.ValueDescription import net.mamoe.mirai.console.data.value @@ -30,6 +31,7 @@ object BiliPluginConfig : ReadOnlyPluginConfig("BiliPluginConfig") { @ValueDescription("推送文字模板, 参数请看readme") val pushTemplate: String by value("{name}@{type}\n{link}") + //@Suppress(stringSerialization = DOUBLE_QUOTATION) @ValueDescription("cookie, 请使用双引号") val cookie: String by value("") diff --git a/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/DynamicData.kt b/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/DynamicData.kt index 59d78eb..0363119 100644 --- a/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/DynamicData.kt +++ b/src/main/kotlin/top.colter.mirai.plugin.bilibili/data/DynamicData.kt @@ -20,6 +20,12 @@ data class DynamicList( val newNum: Int = 0 ) +@Serializable +data class DynamicDetail( + @SerialName("card") + val dynamic: DynamicInfo? = null, +) + @Serializable data class DynamicInfo( @SerialName("card") 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 9b5d965..62df2ad 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 @@ -60,6 +60,16 @@ object ImgUtils { } } + fun testBuildImageMessage(msg: String){ + buildImageMessage( + listOf(textContent(msg)!!), + UserProfile(UserInfo(1, + "Test", + "https://i0.hdslb.com/bfs/face/904bef1b4067335068faba12062f735dda07c1fe.jpg@240w_240h_1c_1s.png")), + "2021年12月21日","#9fc7f3","D:/Code/test.png" + ) + } + fun buildImageMessage( biList: List, profile: UserProfile?, @@ -86,7 +96,7 @@ object ImgUtils { } g2.dispose() - +// val file = File(fileStr) val file = PluginMain.resolveDataFile(fileStr) if (!file.parentFile.exists()) file.parentFile.mkdirs() ImageIO.write(bi, "png", file) @@ -272,6 +282,12 @@ object ImgUtils { } else textG2.drawString(cs, textX, textY) textX += textG2.getStrWidth(cs) } + if (textY > 1900){ + textG2.color = Color.red + textX += 200 + textG2.drawString("!!文字过长, 后续已省略!!", textX, textY) + break + } } textG2.dispose()