From f96f7b7055f7ef58e0a4717d97a8cdcb79069bc9 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Mon, 14 Oct 2024 17:40:04 +0800 Subject: [PATCH] fix: fix bili video parse error and add pastebin command --- src/main/kotlin/cn/rtast/fancybot/Const.kt | 2 +- .../fancybot/commands/misc/PastebinCommand.kt | 35 +++++++ .../commands/parse/BilibiliParseCommand.kt | 93 ++++++++++--------- .../fancybot/entity/pastebin/Pastebin.kt | 12 +++ 4 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 src/main/kotlin/cn/rtast/fancybot/commands/misc/PastebinCommand.kt create mode 100644 src/main/kotlin/cn/rtast/fancybot/entity/pastebin/Pastebin.kt diff --git a/src/main/kotlin/cn/rtast/fancybot/Const.kt b/src/main/kotlin/cn/rtast/fancybot/Const.kt index 08aef47..f1f0cb4 100644 --- a/src/main/kotlin/cn/rtast/fancybot/Const.kt +++ b/src/main/kotlin/cn/rtast/fancybot/Const.kt @@ -127,5 +127,5 @@ val commands = listOf( SupportMeCommand(), TheCatCommand(), TheHistoryOfTodayCommand(), MCVersionCommand(), MinecraftWikiCommand(), GaoKaoDaysRemainCommand(), IdiomExplainCommand(), NiuziManagerCommand(), TodayEatCommand(), TodayDrinkCommand(), MCLoginCommand(), RCONCommand(), - GravatarCommand() + GravatarCommand(), PastebinCommand() ) \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/misc/PastebinCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/misc/PastebinCommand.kt new file mode 100644 index 0000000..d5a4900 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/commands/misc/PastebinCommand.kt @@ -0,0 +1,35 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/10/14 + */ + + +package cn.rtast.fancybot.commands.misc + +import cn.rtast.fancybot.API_RTAST_URL +import cn.rtast.fancybot.annotations.CommandDescription +import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink +import cn.rtast.fancybot.configManager +import cn.rtast.fancybot.entity.pastebin.PastebinPayload +import cn.rtast.fancybot.entity.pastebin.PastebinResponse +import cn.rtast.fancybot.util.Http +import cn.rtast.fancybot.util.str.toJson +import cn.rtast.rob.entity.GroupMessage +import cn.rtast.rob.util.BaseCommand +import cn.rtast.rob.util.ob.OneBotListener + +@CommandDescription("Pastebin!") +class PastebinCommand : BaseCommand() { + override val commandNames = listOf("/pastebin", "/pb") + + override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List) { + val content = args.joinToString(" ") + val pastebinPayload = PastebinPayload(content) + val response = Http.post( + "$API_RTAST_URL/api/pastebin", pastebinPayload.toJson(), + params = mapOf("key" to configManager.apiRtastKey) + ) + message.reply("$API_RTAST_URL/api/pp/${response.id}?raw=true".makeShortLink()) + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/BilibiliParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/BilibiliParseCommand.kt index 629e453..b9c7e8c 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/BilibiliParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/BilibiliParseCommand.kt @@ -185,54 +185,57 @@ object BiliVideoParseCommand { } suspend fun parse(listener: OneBotListener, message: GroupMessage) { - val bvid = if (bvRegex.containsMatchIn(message.rawMessage)) { - message.rawMessage.extractBv() - } else if (avRegex.containsMatchIn(message.rawMessage)) { - message.rawMessage.extractAv().avToBv() - } else if (shortUrlRegex.containsMatchIn(message.rawMessage)) { - val shortUrl = shortUrlRegex.find(message.rawMessage)!!.value - getShortUrlBVID(shortUrl) - } else if (bvRegex.containsMatchIn(message.rawMessage)) { - bvRegex.find(message.rawMessage)!!.value - } else if (message.message.find { it.type == ArrayMessageType.json } != null) { - val card = message.message.find { it.type == ArrayMessageType.json }!! - .data.data!!.toString().fromJson() - val shortUrl = if (card.meta.detail == null) { - if (!card.meta.news?.tag!!.contains("哔哩哔哩")) return - card.meta.news.jumpUrl + try { + val bvid = if (bvRegex.containsMatchIn(message.rawMessage)) { + message.rawMessage.extractBv() + } else if (avRegex.containsMatchIn(message.rawMessage)) { + message.rawMessage.extractAv().avToBv() + } else if (shortUrlRegex.containsMatchIn(message.rawMessage)) { + val shortUrl = shortUrlRegex.find(message.rawMessage)!!.value + getShortUrlBVID(shortUrl) + } else if (bvRegex.containsMatchIn(message.rawMessage)) { + bvRegex.find(message.rawMessage)!!.value + } else if (message.message.find { it.type == ArrayMessageType.json } != null) { + val card = message.message.find { it.type == ArrayMessageType.json }!! + .data.data!!.toString().fromJson() + 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 + card.meta.detail.qqDocUrl + } + getShortUrlBVID(shortUrl) } else { - if (!card.meta.detail.title.contains("哔哩哔哩")) return - card.meta.detail.qqDocUrl + return } - getShortUrlBVID(shortUrl) - } else { - return + val videoInfo = this.getVideoStat(bvid) + val viewCount = this.getViewCount(bvid, videoInfo.data.cid) + val shortUrl = this.generateShortUrl(bvid, videoInfo.data.aid) + val fans = Http.get(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 + 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, + share, like, favorite, fans, viewCount + ) + val msg = MessageChain.Builder() + .addReply(message.messageId) + .addImage(image, true) + .addText(shortUrl) + .build() + listener.sendGroupMessage(message.groupId, msg) + } catch (_: Exception) { } - val videoInfo = this.getVideoStat(bvid) - val viewCount = this.getViewCount(bvid, videoInfo.data.cid) - val shortUrl = this.generateShortUrl(bvid, videoInfo.data.aid) - val fans = Http.get(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 - 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, - share, like, favorite, fans, viewCount - ) - val msg = MessageChain.Builder() - .addReply(message.messageId) - .addImage(image, true) - .addText(shortUrl) - .build() - listener.sendGroupMessage(message.groupId, msg) } } diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/pastebin/Pastebin.kt b/src/main/kotlin/cn/rtast/fancybot/entity/pastebin/Pastebin.kt new file mode 100644 index 0000000..4d32478 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/entity/pastebin/Pastebin.kt @@ -0,0 +1,12 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/10/14 + */ + + +package cn.rtast.fancybot.entity.pastebin + +data class PastebinPayload(val content: String) + +data class PastebinResponse(val id: String) \ No newline at end of file