2024-09-06 10:58:55 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2024 RTAkland
|
|
|
|
|
* Author: RTAkland
|
|
|
|
|
* Date: 2024/9/6
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2024-09-13 12:44:10 +08:00
|
|
|
package cn.rtast.fancybot.commands.parse
|
2024-09-06 10:58:55 +08:00
|
|
|
|
|
|
|
|
import cn.rtast.fancybot.entity.bili.BVID
|
2024-09-13 16:19:54 +08:00
|
|
|
import cn.rtast.fancybot.entity.bili.UserStat
|
2024-09-06 10:58:55 +08:00
|
|
|
import cn.rtast.fancybot.util.Http
|
2024-09-10 17:44:07 +08:00
|
|
|
import cn.rtast.fancybot.util.Resources
|
2024-09-12 15:43:44 +08:00
|
|
|
import cn.rtast.fancybot.util.drawCustomImage
|
2024-09-10 17:44:07 +08:00
|
|
|
import cn.rtast.fancybot.util.str.encodeToBase64
|
2024-09-13 16:19:54 +08:00
|
|
|
import cn.rtast.fancybot.util.str.formatNumber
|
2024-09-12 15:43:44 +08:00
|
|
|
import cn.rtast.fancybot.util.str.setTruncat
|
2024-09-06 10:58:55 +08:00
|
|
|
import cn.rtast.rob.entity.GroupMessage
|
|
|
|
|
import cn.rtast.rob.util.ob.MessageChain
|
|
|
|
|
import cn.rtast.rob.util.ob.OBMessage
|
2024-09-12 13:06:13 +08:00
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
|
import okhttp3.Request
|
2024-09-10 17:44:07 +08:00
|
|
|
import java.awt.Color
|
|
|
|
|
import java.awt.Font
|
2024-09-12 15:49:48 +08:00
|
|
|
import java.awt.RenderingHints
|
2024-09-10 17:44:07 +08:00
|
|
|
import java.awt.image.BufferedImage
|
|
|
|
|
import java.io.ByteArrayOutputStream
|
|
|
|
|
import java.net.URI
|
|
|
|
|
import javax.imageio.ImageIO
|
2024-09-06 10:58:55 +08:00
|
|
|
|
|
|
|
|
object BVParseCommand {
|
|
|
|
|
|
|
|
|
|
private const val CID_URL = "https://api.bilibili.com/x/web-interface/view"
|
2024-09-13 16:19:54 +08:00
|
|
|
private const val USER_STAT_URL = "https://api.bilibili.com/x/relation/stat"
|
2024-09-10 17:44:07 +08:00
|
|
|
private const val CANVAS_WIDTH = 1000
|
|
|
|
|
private const val CANVAS_HEIGHT = 600
|
2024-09-14 14:52:37 +08:00
|
|
|
private val titleFont = Font("Serif", Font.ITALIC, 40)
|
|
|
|
|
private val numberFont = Font("Serif", Font.ITALIC, 25)
|
2024-09-10 17:44:07 +08:00
|
|
|
private val twoTwoLogo = ImageIO.read(Resources.loadFromResources("bili/22-coin.png"))
|
|
|
|
|
private val likeIcon = ImageIO.read(Resources.loadFromResources("bili/like.png"))
|
|
|
|
|
private val coinIcon = ImageIO.read(Resources.loadFromResources("bili/coin.png"))
|
|
|
|
|
private val viewIcon = ImageIO.read(Resources.loadFromResources("bili/view.png"))
|
|
|
|
|
private val shareIcon = ImageIO.read(Resources.loadFromResources("bili/share.png"))
|
|
|
|
|
private val favoriteIcon = ImageIO.read(Resources.loadFromResources("bili/favorite.png"))
|
|
|
|
|
private val replyIcon = ImageIO.read(Resources.loadFromResources("bili/reply.png"))
|
2024-09-06 10:58:55 +08:00
|
|
|
|
2024-09-12 13:06:13 +08:00
|
|
|
private val tempOkHttpClient = OkHttpClient()
|
|
|
|
|
|
|
|
|
|
fun getShortUrlBVID(shortUrl: String): String {
|
|
|
|
|
val request = Request.Builder().url(shortUrl.split(" ").last()).build()
|
|
|
|
|
val redirectedUrl = tempOkHttpClient.newCall(request).execute()
|
|
|
|
|
redirectedUrl.use {
|
|
|
|
|
return it.request.url.toString().split("/")[4].split("?").first()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 17:44:07 +08:00
|
|
|
private fun createResponseImage(
|
|
|
|
|
title: String,
|
|
|
|
|
author: String,
|
|
|
|
|
authorFace: String,
|
|
|
|
|
picUrl: String,
|
|
|
|
|
reply: String,
|
|
|
|
|
view: String,
|
|
|
|
|
coin: String,
|
|
|
|
|
share: String,
|
|
|
|
|
like: String,
|
2024-09-13 16:19:54 +08:00
|
|
|
favorite: String,
|
|
|
|
|
fans: Int,
|
2024-09-10 17:44:07 +08:00
|
|
|
): String {
|
|
|
|
|
val coverImage = ImageIO.read(URI(picUrl).toURL())
|
|
|
|
|
val faceImage = ImageIO.read(URI(authorFace).toURL())
|
|
|
|
|
val backgroundImage = BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_INT_RGB)
|
|
|
|
|
val g2d = backgroundImage.createGraphics()
|
2024-09-12 15:49:48 +08:00
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
|
2024-09-10 17:44:07 +08:00
|
|
|
g2d.color = Color(43, 43, 43)
|
|
|
|
|
g2d.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)
|
|
|
|
|
// draw rect
|
|
|
|
|
g2d.color = Color(102, 102, 102)
|
|
|
|
|
g2d.fillRect(0, 0, CANVAS_WIDTH, 120)
|
|
|
|
|
g2d.color = Color.WHITE
|
|
|
|
|
// draw numbers
|
|
|
|
|
g2d.font = numberFont
|
|
|
|
|
g2d.drawString(like, 110, 200)
|
|
|
|
|
g2d.drawString(coin, 110, 290)
|
|
|
|
|
g2d.drawString(view, 110, 380)
|
|
|
|
|
g2d.drawString(favorite, 300, 380)
|
|
|
|
|
g2d.drawString(reply, 300, 290)
|
|
|
|
|
g2d.drawString(share, 300, 200)
|
|
|
|
|
// draw video title
|
2024-09-12 15:43:44 +08:00
|
|
|
val truncatedText = setTruncat(title, g2d)
|
2024-09-10 17:44:07 +08:00
|
|
|
g2d.font = titleFont
|
|
|
|
|
g2d.drawString(truncatedText, 20, 70)
|
|
|
|
|
// draw author name
|
|
|
|
|
g2d.drawString(author, 40, 580)
|
|
|
|
|
// draw icons
|
2024-09-12 15:43:44 +08:00
|
|
|
g2d.drawCustomImage(favoriteIcon, 230, 340, 50.0, 50.0, false)
|
|
|
|
|
g2d.drawCustomImage(replyIcon, 230, 250, 50.0, 50.0, false)
|
|
|
|
|
g2d.drawCustomImage(shareIcon, 230, 160, 50.0, 50.0, false)
|
|
|
|
|
g2d.drawCustomImage(viewIcon, 40, 340, 50.0, 50.0, false)
|
|
|
|
|
g2d.drawCustomImage(coinIcon, 40, 250, 50.0, 50.0, false)
|
|
|
|
|
g2d.drawCustomImage(likeIcon, 40, 160, 50.0, 50.0, false)
|
2024-09-10 17:44:07 +08:00
|
|
|
// draw 22 logo
|
2024-09-12 15:43:44 +08:00
|
|
|
g2d.drawCustomImage(twoTwoLogo, 800, 450, 90.0, 160.0, false)
|
2024-09-13 16:19:54 +08:00
|
|
|
// draw fans count
|
|
|
|
|
g2d.font = numberFont
|
|
|
|
|
g2d.drawString(fans.formatNumber() + "粉丝", 120, 520)
|
2024-09-10 17:44:07 +08:00
|
|
|
// draw author face
|
2024-09-12 15:43:44 +08:00
|
|
|
g2d.drawCustomImage(faceImage, 40, 480, 60.0, 60.0, true)
|
2024-09-10 17:44:07 +08:00
|
|
|
// draw cover image
|
2024-09-12 15:43:44 +08:00
|
|
|
g2d.drawCustomImage(coverImage, 430, 140, 600.0, 300.0, true)
|
2024-09-10 17:44:07 +08:00
|
|
|
g2d.dispose()
|
|
|
|
|
val byteArrayOutputStream = ByteArrayOutputStream()
|
|
|
|
|
ImageIO.write(backgroundImage, "png", byteArrayOutputStream)
|
|
|
|
|
val imageBytes = byteArrayOutputStream.toByteArray()
|
|
|
|
|
return imageBytes.encodeToBase64()
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 11:18:57 +08:00
|
|
|
suspend fun parse(listener: OBMessage, bvid: String, message: GroupMessage) {
|
2024-09-10 17:44:07 +08:00
|
|
|
val videoInfo = Http.get<BVID>(CID_URL, mapOf("bvid" to bvid))
|
2024-09-13 16:19:54 +08:00
|
|
|
val fans = Http.get<UserStat>(USER_STAT_URL, mapOf("vmid" to videoInfo.data.owner.mid)).data.follower
|
2024-09-10 17:44:07 +08:00
|
|
|
val authorFace = videoInfo.data.owner.face
|
|
|
|
|
val title = videoInfo.data.title
|
|
|
|
|
val author = videoInfo.data.owner.name
|
|
|
|
|
val coverPicUrl = videoInfo.data.pic
|
|
|
|
|
val view = videoInfo.data.stat.view.formatNumber()
|
|
|
|
|
val share = videoInfo.data.stat.share.formatNumber()
|
|
|
|
|
val like = videoInfo.data.stat.like.formatNumber()
|
|
|
|
|
val favorite = videoInfo.data.stat.favorite.formatNumber()
|
|
|
|
|
val coin = videoInfo.data.stat.coin.formatNumber()
|
|
|
|
|
val reply = videoInfo.data.stat.reply.formatNumber()
|
|
|
|
|
val image = this.createResponseImage(
|
|
|
|
|
title, author, authorFace,
|
|
|
|
|
coverPicUrl, reply, view, coin,
|
2024-09-13 16:19:54 +08:00
|
|
|
share, like, favorite, fans
|
2024-09-06 10:58:55 +08:00
|
|
|
)
|
|
|
|
|
val msg = MessageChain.Builder()
|
2024-09-06 17:10:10 +08:00
|
|
|
.addReply(message.messageId)
|
2024-09-10 17:44:07 +08:00
|
|
|
.addImage(image, true)
|
2024-09-06 10:58:55 +08:00
|
|
|
.build()
|
|
|
|
|
listener.sendGroupMessage(message.groupId, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|