Skip to content

Commit

Permalink
修复动态文字过长报错
Browse files Browse the repository at this point in the history
  • Loading branch information
Colter23 committed Dec 21, 2021
1 parent 744e0f4 commit 769ec7f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 10 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
plugins {
val kotlinVersion = "1.5.31"
val kotlinVersion = "1.6.0"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

id("net.mamoe.mirai-console") version "2.9.0-M1"
}

group = "top.colter"
version = "2.0.5"
version = "2.0.6"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, SubData> by BiliSubscribeData::dynamic
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/top.colter.mirai.plugin.bilibili/Dynamic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ val DynamicInfo.uid get() = describe.uid
val DynamicInfo.profile get() = describe.profile?.decode<UserProfile>()
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))
Expand Down Expand Up @@ -155,6 +157,7 @@ fun DynamicLive.getContent(dynamicInfo: DynamicInfo): String {
}



fun String.buildContent(type: Int, dynamicInfo: DynamicInfo): List<BufferedImage> {
return when (type) {
DynamicType.REPLY -> decode<DynamicReply>().bufferedImages(dynamicInfo)
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/top.colter.mirai.plugin.bilibili/Listener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageEvent> {
// message.filterIsInstance<At>().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<DynamicDetail>(DYNAMIC_DETAIL).dynamic
// if (dynamic?.type == DynamicType.DELETE)
// }
// videoRegex.find(quote)?.value.let{
// println("视频:$it")
// }
// }
// }
// }
// }

globalEventChannel().subscribeAlways<MyEvent> {
val c = DynamicTasker.dynamic[uid]?.contacts?.get(subject)
if (c == null) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/top.colter.mirai.plugin.bilibili/Live.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/top.colter.mirai.plugin.bilibili/data/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BufferedImage>,
profile: UserProfile?,
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 769ec7f

Please sign in to comment.