-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,5 @@ | |
## 图解 | ||
|
||
![img.png](../img/admincall.png) | ||
![img.png](../../img/admincall.png) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters