mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
* 🎨 修改bilibili,bilibiliparse插件结构,添加bilibili动态直播专栏解析 * 💩 修改大小写 * 🎨 正则全局,错误处理 * 🎨 使用json.NewDecoder() * 💚 空使用 * 💚 修lint
106 lines
3.4 KiB
Go
106 lines
3.4 KiB
Go
package bilibili
|
|
|
|
import (
|
|
"regexp"
|
|
"time"
|
|
|
|
ctrl "github.com/FloatTech/zbpctrl"
|
|
"github.com/FloatTech/zbputils/control"
|
|
"github.com/FloatTech/zbputils/ctxext"
|
|
zero "github.com/wdvxdr1123/ZeroBot"
|
|
"github.com/wdvxdr1123/ZeroBot/message"
|
|
)
|
|
|
|
var (
|
|
limit = ctxext.NewLimiterManager(time.Second*10, 1)
|
|
searchVideo = `bilibili.com/video/(?:av(\d+)|([bB][vV][0-9a-zA-Z]+))`
|
|
searchDynamic = `(t.bilibili.com|m.bilibili.com/dynamic)/(\d+)`
|
|
searchArticle = `bilibili.com/read/(?:cv|mobile/)(\d+)`
|
|
searchLiveRoom = `live.bilibili.com/(\d+)`
|
|
searchVideoRe = regexp.MustCompile(searchVideo)
|
|
searchDynamicRe = regexp.MustCompile(searchDynamic)
|
|
searchArticleRe = regexp.MustCompile(searchArticle)
|
|
searchLiveRoomRe = regexp.MustCompile(searchLiveRoom)
|
|
)
|
|
|
|
// 插件主体
|
|
func init() {
|
|
en := control.Register("bilibiliparse", &ctrl.Options[*zero.Ctx]{
|
|
DisableOnDefault: false,
|
|
Help: "b站动态、专栏、视频、直播解析\n" +
|
|
"- t.bilibili.com/642277677329285174 | bilibili.com/read/cv17134450 | bilibili.com/video/BV13B4y1x7pS | live.bilibili.com/22603245 ",
|
|
})
|
|
en.OnRegex(`((b23|acg).tv|bili2233.cn)/[0-9a-zA-Z]+`).SetBlock(true).Limit(limit.LimitByGroup).
|
|
Handle(func(ctx *zero.Ctx) {
|
|
url := ctx.State["regex_matched"].([]string)[0]
|
|
realurl, err := getrealurl("https://" + url)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
switch {
|
|
case searchVideoRe.MatchString(realurl):
|
|
ctx.State["regex_matched"] = searchVideoRe.FindStringSubmatch(realurl)
|
|
handleVideo(ctx)
|
|
case searchDynamicRe.MatchString(realurl):
|
|
ctx.State["regex_matched"] = searchDynamicRe.FindStringSubmatch(realurl)
|
|
handleDynamic(ctx)
|
|
case searchArticleRe.MatchString(realurl):
|
|
ctx.State["regex_matched"] = searchArticleRe.FindStringSubmatch(realurl)
|
|
handleArticle(ctx)
|
|
case searchLiveRoomRe.MatchString(realurl):
|
|
ctx.State["regex_matched"] = searchLiveRoomRe.FindStringSubmatch(realurl)
|
|
handleLive(ctx)
|
|
}
|
|
})
|
|
en.OnRegex(searchVideo).SetBlock(true).Limit(limit.LimitByGroup).Handle(handleVideo)
|
|
en.OnRegex(searchDynamic).SetBlock(true).Limit(limit.LimitByGroup).Handle(handleDynamic)
|
|
en.OnRegex(searchArticle).SetBlock(true).Limit(limit.LimitByGroup).Handle(handleArticle)
|
|
en.OnRegex(searchLiveRoom).SetBlock(true).Limit(limit.LimitByGroup).Handle(handleLive)
|
|
}
|
|
|
|
func handleVideo(ctx *zero.Ctx) {
|
|
id := ctx.State["regex_matched"].([]string)[1]
|
|
if id == "" {
|
|
id = ctx.State["regex_matched"].([]string)[2]
|
|
}
|
|
card, err := getVideoInfo(id)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR:", err))
|
|
return
|
|
}
|
|
msg, err := videoCard2msg(card)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR:", err))
|
|
return
|
|
}
|
|
ctx.SendChain(msg...)
|
|
}
|
|
|
|
func handleDynamic(ctx *zero.Ctx) {
|
|
msg, err := dynamicDetail(ctx.State["regex_matched"].([]string)[2])
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR:", err))
|
|
return
|
|
}
|
|
ctx.SendChain(msg...)
|
|
}
|
|
|
|
func handleArticle(ctx *zero.Ctx) {
|
|
card, err := getArticleInfo(ctx.State["regex_matched"].([]string)[1])
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR:", err))
|
|
return
|
|
}
|
|
ctx.SendChain(articleCard2msg(card, ctx.State["regex_matched"].([]string)[1])...)
|
|
}
|
|
|
|
func handleLive(ctx *zero.Ctx) {
|
|
card, err := getLiveRoomInfo(ctx.State["regex_matched"].([]string)[1])
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR:", err))
|
|
return
|
|
}
|
|
ctx.SendChain(liveCard2msg(card)...)
|
|
}
|