Skip to content

Commit

Permalink
feat: 新增bilibili链接解析模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Clov614 committed Sep 2, 2024
1 parent c259e68 commit 9f10577
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ start cmd /K rikka-bot-wechat.exe

## 功能模块

1. - [x] [管理权限](docs/plugin/README.md)
2. - [ ] 链接解析
1. - [x] [管理权限](docs/plugin/admin/README.md)
2. - [x] [链接解析](docs/plugin/bilibili/README.md)
3. - [ ] 热更新

## 如何开发插件
Expand Down
Binary file added docs/img/bilibili/biliurlparse01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/bilibili/biliurlparse02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/bilibili/biliurlparse03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/plugin/README.md → docs/plugin/admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
## 图解

![img.png](../img/admincall.png)
![img.png](../../img/admincall.png)

26 changes: 26 additions & 0 deletions docs/plugin/bilibili/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# bilibili链接解析模块

> [!TIP]
> 插件代码位置: [/rikkabot/plugins/admin/adminplugin.go](https://github.com/Clov614/rikka-bot-wechat/tree/main/rikkabot/plugins/biliUrlDecode/biliUrlDecode.go)

## 功能说明

以下三种消息可以触发链接解析:

- bvid: BV1yi42127nN

- 原始链接/分享短链: https://www.bilibili.com/video/BV1yi42127nN

- 分享的app消息

## 立即使用

被动技能无需主动命令发起

## 图解

![biliurlparse01.png](../../img/bilibili/biliurlparse01.png)
![biliurlparse02.png](../../img/bilibili/biliurlparse02.png)
![biliurlparse03.png](../../img/bilibili/biliurlparse03.png)

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
replace github.com/eatmoreapple/openwechat => github.com/Clov614/openwechat v1.4.8

require (
github.com/Clov614/bilibili v0.1.1 // indirect
github.com/bytedance/sonic v1.11.9 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Clov614/bilibili v0.1.1 h1:YsWcm8vYSwwksvUTZAjBPd37VTAcU2+BfArHQxJqKIY=
github.com/Clov614/bilibili v0.1.1/go.mod h1:th+qNf6jfSDH03oCM4s347a+i7IpCgZyNRIHY8AAXe8=
github.com/Clov614/go-ai-sdk v0.3.3 h1:aNL3vQvh6TRX+NZ8HcW/SJsfSdP1cpX+bcWXkNMSLGw=
github.com/Clov614/go-ai-sdk v0.3.3/go.mod h1:S/GDuJSLlnNU47uEpY2FrN1GdP+A4mDXBQZQ2i6sCzk=
github.com/Clov614/openwechat v1.4.8 h1:8O8zeVAGw4W2PWy/gVGthPuM7Ua25ZSfaerwAs6JqIU=
Expand Down
118 changes: 118 additions & 0 deletions rikkabot/plugins/biliUrlDecode/biliUrlDecode.go
Original file line number Diff line number Diff line change
@@ -1 +1,119 @@
package biliUrlDecode

import (
"bytes"
"encoding/xml"
"github.com/Clov614/bilibili"
"github.com/rs/zerolog/log"
"regexp"
"strconv"
"wechat-demo/rikkabot/message"
"wechat-demo/rikkabot/processor/control"
"wechat-demo/rikkabot/processor/control/dialog"
"wechat-demo/rikkabot/processor/register"
"wechat-demo/rikkabot/utils/imgutil"
)

func init() {
biliDecodePlugin := biliPlugin{
OnceDialog: &dialog.OnceDialog{},
}
biliDecodePlugin.PluginName = "bilibili链接解析"
// 允许群组 白名单允许
biliDecodePlugin.ProcessRules = &control.ProcessRules{EnableGroup: true, CostomTrigger: func(rikkaMsg message.Message) bool {
if rikkaMsg.Msgtype == message.MsgTypeApp {
var xmlMsg message.XMLMsg
err := xml.Unmarshal([]byte(rikkaMsg.Content), &xmlMsg)
if err != nil {
log.Err(err).Msg("xml.Unmarshal fail at biliPlugin")
return false
}
// 解析链接
if xmlMsg.AppInfo.AppName == "哔哩哔哩" {
return true
}
} else if rikkaMsg.Msgtype == message.MsgTypeText {
regexBV := regexp.MustCompile(`(BV[\w\d]+)`)
regexBilibili := regexp.MustCompile(`https:\/\/www\.bilibili\.com\/video\/(BV[\w\d]+)\/?`)
regexShort := regexp.MustCompile(`https:\/\/b23\.tv\/([\w\d]+)`)
return regexBV.MatchString(rikkaMsg.Content) || regexBilibili.MatchString(rikkaMsg.Content) || regexShort.MatchString(rikkaMsg.Content)
}
return false
}}
biliDecodePlugin.Once = func(recvmsg message.Message, sendMsg chan<- *message.Message) {
switch recvmsg.Msgtype {
case message.MsgTypeApp:
var xmlMsg message.XMLMsg
err := xml.Unmarshal([]byte(recvmsg.Content), &xmlMsg)
if err != nil {
log.Err(err).Msg("xml.Unmarshal fail at biliPlugin")
return
}
// 解析链接
if xmlMsg.AppInfo.AppName == "哔哩哔哩" {
videoInfo, err := bilibili.NewUrlDecoder().Parse(xmlMsg.AppMsg.URL)
if err != nil {
log.Err(err).Msg("bilibili.NewUrlDecoder fail at biliPlugin")
}
output := buildOutput(videoInfo)
biliDecodePlugin.SendText(recvmsg.MetaData, output)
return
}
case message.MsgTypeText:
regexBV := regexp.MustCompile(`(BV[\w\d]+)`)
regexBilibili := regexp.MustCompile(`https:\/\/www\.bilibili\.com\/video\/(BV[\w\d]+)\/?`)
regexShort := regexp.MustCompile(`https:\/\/b23\.tv\/([\w\d]+)`)
var videoInfo *bilibili.VideoInfo
var err error
urlParser := bilibili.NewUrlDecoder()
if match := regexBV.FindStringSubmatch(recvmsg.Content); len(match) > 0 {
videoInfo, err = urlParser.ParseByBvid(match[1])
if err != nil {
log.Err(err).Msg("bilibili.urlParser.ParseByBvid fail at biliPlugin")
return
}
} else if match = regexBilibili.FindStringSubmatch(recvmsg.Content); len(match) > 0 {
videoInfo, err = urlParser.Parse(match[1])
if err != nil {
log.Err(err).Msg("bilibili.urlParser.ParseByBvid fail at biliPlugin")
return
}
} else if match = regexShort.FindStringSubmatch(recvmsg.Content); len(match) > 0 {
videoInfo, err = urlParser.Parse(match[1])
if err != nil {
log.Err(err).Msg("bilibili.urlParser.ParseByBvid fail at biliPlugin")
return
}
}
output := buildOutput(videoInfo)
imgFetch, err := imgutil.ImgFetch(videoInfo.Pic)
if err != nil {
log.Err(err).Msg("bilibili.videoInfo.pic.fetchimg fail at biliPlugin")
} else {
biliDecodePlugin.SendImage(recvmsg.MetaData, imgFetch) // 发送图片封面
}
biliDecodePlugin.SendText(recvmsg.MetaData, output)
}
}
register.RegistPlugin("bili-url-parse", biliDecodePlugin.OnceDialog, 1)
}

func buildOutput(videoInfo *bilibili.VideoInfo) string {
// 构建输出视频信息
videoUrl := "https://www.bilibili.com/video/" + videoInfo.Bvid + "\n"
var buf bytes.Buffer
buf.WriteString(videoUrl)
buf.WriteString("标题: " + videoInfo.Title + "\n")
buf.WriteString("分区: " + videoInfo.Tname + "\n")
buf.WriteString("播放量: " + strconv.Itoa(videoInfo.View) + "\n")
buf.WriteString("点赞: " + strconv.Itoa(videoInfo.Like) + "\n")
buf.WriteString("投币: " + strconv.Itoa(videoInfo.Coin) + "\n")
buf.WriteString("收藏: " + strconv.Itoa(videoInfo.Favorite) + "\n")
buf.WriteString("分享: " + strconv.Itoa(videoInfo.Share) + "\n")
buf.WriteString("Bvid: \n\n " + videoInfo.Bvid + "\n")
return buf.String()
}

type biliPlugin struct {
*dialog.OnceDialog
}
7 changes: 4 additions & 3 deletions rikkabot/plugins/defaultPlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"wechat-demo/rikkabot/processor/control/dialog"
"wechat-demo/rikkabot/processor/register"

_ "wechat-demo/rikkabot/plugins/admin" // 需要副作用 init注册方法
_ "wechat-demo/rikkabot/plugins/ai" // 需要副作用 init注册方法
_ "wechat-demo/rikkabot/plugins/game" // 需要副作用 init注册方法
_ "wechat-demo/rikkabot/plugins/admin" // 需要副作用 init注册方法
_ "wechat-demo/rikkabot/plugins/ai" // 需要副作用 init注册方法
_ "wechat-demo/rikkabot/plugins/biliUrlDecode" // 需要副作用 init注册方法
_ "wechat-demo/rikkabot/plugins/game" // 需要副作用 init注册方法
)

func init() {
Expand Down

0 comments on commit 9f10577

Please sign in to comment.