fix: fix bili card share bug and improve bv parser
This commit is contained in:
6 files changed
+52
-18
No files matched your search
@@ -58,9 +58,7 @@ class FancyBot : OBMessage {
|
||||
if (message.rawMessage.startsWith("https://github.com/") ||
|
||||
message.rawMessage.startsWith("git@github.com:") ||
|
||||
(message.rawMessage.split("/").size == 2 && message.rawMessage.split("/").first().isNotEmpty())
|
||||
) {
|
||||
GitHubParseCommand.parse(this, message)
|
||||
}
|
||||
) GitHubParseCommand.parse(this, message)
|
||||
|
||||
if (message.rawMessage.startsWith("BV") ||
|
||||
message.rawMessage.startsWith("https://www.bilibili.com") ||
|
||||
@@ -70,14 +68,19 @@ class FancyBot : OBMessage {
|
||||
// parse bilibili video with a link, bvid or b23.tv link
|
||||
val bvid = if (message.rawMessage.startsWith("BV")) {
|
||||
message.rawMessage
|
||||
} else if (message.rawMessage.contains("https://b23.tv/")) {
|
||||
} else if (message.rawMessage.split(" ").last().startsWith("https://b23.tv/")) {
|
||||
val shortUrl = message.rawMessage.split(" ").last()
|
||||
BVParseCommand.getShortUrlBVID(shortUrl)
|
||||
} else if (message.message.find { it.type == ArrayMessageType.json } != null) {
|
||||
val card = message.message.find { it.type == ArrayMessageType.json }!!
|
||||
.data.data!!.toString().fromJson<CardShare>()
|
||||
val shortUrl = if (card.meta.detail == null) {
|
||||
if (!card.meta.news?.tag!!.contains("哔哩哔哩")) return
|
||||
card.meta.news.jumpUrl
|
||||
} else {
|
||||
if (!card.meta.detail.title.contains("哔哩哔哩")) return
|
||||
val shortUrl = card.meta.detail.qqDocUrl
|
||||
card.meta.detail.qqDocUrl
|
||||
}
|
||||
BVParseCommand.getShortUrlBVID(shortUrl)
|
||||
} else {
|
||||
message.rawMessage.split("/")[4]
|
||||
|
||||
@@ -8,10 +8,12 @@
|
||||
package cn.rtast.fancybot.commands.parse
|
||||
|
||||
import cn.rtast.fancybot.entity.bili.BVID
|
||||
import cn.rtast.fancybot.entity.bili.UserStat
|
||||
import cn.rtast.fancybot.util.Http
|
||||
import cn.rtast.fancybot.util.Resources
|
||||
import cn.rtast.fancybot.util.drawCustomImage
|
||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||
import cn.rtast.fancybot.util.str.formatNumber
|
||||
import cn.rtast.fancybot.util.str.setTruncat
|
||||
import cn.rtast.rob.entity.GroupMessage
|
||||
import cn.rtast.rob.util.ob.MessageChain
|
||||
@@ -29,6 +31,7 @@ import javax.imageio.ImageIO
|
||||
object BVParseCommand {
|
||||
|
||||
private const val CID_URL = "https://api.bilibili.com/x/web-interface/view"
|
||||
private const val USER_STAT_URL = "https://api.bilibili.com/x/relation/stat"
|
||||
private const val CANVAS_WIDTH = 1000
|
||||
private const val CANVAS_HEIGHT = 600
|
||||
private val titleFont = Font("Serif", Font.ITALIC, 40).deriveFont(Font.ITALIC)
|
||||
@@ -51,15 +54,6 @@ object BVParseCommand {
|
||||
}
|
||||
}
|
||||
|
||||
private fun Int.formatNumber(): String {
|
||||
return if (this >= 10000) {
|
||||
val result = this / 10000.0
|
||||
String.format("%.1f万", result)
|
||||
} else {
|
||||
this.toString()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createResponseImage(
|
||||
title: String,
|
||||
author: String,
|
||||
@@ -70,7 +64,8 @@ object BVParseCommand {
|
||||
coin: String,
|
||||
share: String,
|
||||
like: String,
|
||||
favorite: String
|
||||
favorite: String,
|
||||
fans: Int,
|
||||
): String {
|
||||
val coverImage = ImageIO.read(URI(picUrl).toURL())
|
||||
val faceImage = ImageIO.read(URI(authorFace).toURL())
|
||||
@@ -106,6 +101,9 @@ object BVParseCommand {
|
||||
g2d.drawCustomImage(likeIcon, 40, 160, 50.0, 50.0, false)
|
||||
// draw 22 logo
|
||||
g2d.drawCustomImage(twoTwoLogo, 800, 450, 90.0, 160.0, false)
|
||||
// draw fans count
|
||||
g2d.font = numberFont
|
||||
g2d.drawString(fans.formatNumber() + "粉丝", 120, 520)
|
||||
// draw author face
|
||||
g2d.drawCustomImage(faceImage, 40, 480, 60.0, 60.0, true)
|
||||
// draw cover image
|
||||
@@ -119,6 +117,7 @@ object BVParseCommand {
|
||||
|
||||
suspend fun parse(listener: OBMessage, bvid: String, message: GroupMessage) {
|
||||
val videoInfo = Http.get<BVID>(CID_URL, mapOf("bvid" to bvid))
|
||||
val fans = Http.get<UserStat>(USER_STAT_URL, mapOf("vmid" to videoInfo.data.owner.mid)).data.follower
|
||||
val authorFace = videoInfo.data.owner.face
|
||||
val title = videoInfo.data.title
|
||||
val author = videoInfo.data.owner.name
|
||||
@@ -132,7 +131,7 @@ object BVParseCommand {
|
||||
val image = this.createResponseImage(
|
||||
title, author, authorFace,
|
||||
coverPicUrl, reply, view, coin,
|
||||
share, like, favorite
|
||||
share, like, favorite, fans
|
||||
)
|
||||
val msg = MessageChain.Builder()
|
||||
.addReply(message.messageId)
|
||||
|
||||
@@ -19,6 +19,7 @@ data class BVID(
|
||||
)
|
||||
|
||||
data class Owner(
|
||||
val mid: Long,
|
||||
val name: String,
|
||||
val face: String,
|
||||
)
|
||||
|
||||
@@ -14,7 +14,8 @@ data class CardShare(
|
||||
) {
|
||||
data class Meta(
|
||||
@SerializedName("detail_1")
|
||||
val detail: Detail
|
||||
val detail: Detail?,
|
||||
val news: News?
|
||||
)
|
||||
|
||||
data class Detail(
|
||||
@@ -22,4 +23,9 @@ data class CardShare(
|
||||
@SerializedName("qqdocurl")
|
||||
val qqDocUrl: String,
|
||||
)
|
||||
|
||||
data class News(
|
||||
val tag: String,
|
||||
val jumpUrl: String
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/9/13
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity.bili
|
||||
|
||||
data class UserStat(
|
||||
val data: Data
|
||||
) {
|
||||
data class Data(
|
||||
val follower: Int
|
||||
)
|
||||
}
|
||||
@@ -22,3 +22,12 @@ fun setTruncat(origin: String, g2d: Graphics2D, maxWidth: Int = 500): String {
|
||||
origin.substring(0, endIndex) + "..."
|
||||
} else origin
|
||||
}
|
||||
|
||||
fun Int.formatNumber(): String {
|
||||
return if (this >= 10000) {
|
||||
val result = this / 10000.0
|
||||
String.format("%.1f万", result)
|
||||
} else {
|
||||
this.toString()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user