diff --git a/Cocoa.go b/Cocoa.go index e7df2c6..e9f55ec 100644 --- a/Cocoa.go +++ b/Cocoa.go @@ -20,6 +20,7 @@ import ( // normal priority _ "github.com/DaydreamCafe/Cocoa/V2/plugins/bilibili_parse" + _ "github.com/DaydreamCafe/Cocoa/V2/plugins/blive" _ "github.com/DaydreamCafe/Cocoa/V2/plugins/char_reverser" _ "github.com/DaydreamCafe/Cocoa/V2/plugins/coser" _ "github.com/DaydreamCafe/Cocoa/V2/plugins/github_parse" diff --git a/plugins/bilibili_parse/init.go b/plugins/bilibili_parse/init.go index c4c10e0..30e4023 100644 --- a/plugins/bilibili_parse/init.go +++ b/plugins/bilibili_parse/init.go @@ -30,7 +30,7 @@ func init() { Version: "1.0.0", Description: "Bilibili视频解析插件", Author: "WhitePaper233", - Usage: "发送任意形式的B站分享链接、番号及移动端分享卡片, 将自动解析出视频信息", + Usage: `-发送任意形式的B站分享链接、番号及移动端分享卡片, 将自动解析出视频信息`, } // 初始化插件 engine := control.Registe(&metadata, control.EchoAny) diff --git a/plugins/blive/handler.go b/plugins/blive/handler.go new file mode 100644 index 0000000..c637b69 --- /dev/null +++ b/plugins/blive/handler.go @@ -0,0 +1,126 @@ +// Package blive B站直播搜索 +package blive + +import ( + "encoding/json" + "fmt" + "net/http" + "strconv" + "strings" + + logger "github.com/sirupsen/logrus" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" +) + +const ( + // BiliSearchAPI API地址 + BiliSearchAPI = "https://api.bilibili.com/x/web-interface/search/type?page=1&search_type=live&keyword=%s" + // BiliLiveURL 直播间地址 + BiliLiveURL = "https://live.bilibili.com/" +) + +type searchResp struct { + Code int64 `json:"code"` + Data struct { + Result struct { + LiveRoom []struct { + RoomCover string `json:"cover"` + LiveTime string `json:"live_time"` + Title string `json:"title"` + UserCover string `json:"user_cover"` + Watched struct { + Num int64 `json:"num"` + } `json:"watched_show"` + } `json:"live_room"` + LiveUser []struct { + IsLive bool `json:"is_live"` + UserName string `json:"uname"` + LiveTime string `json:"live_time"` + RoomID int64 `json:"roomid"` + } `json:"live_user"` + } `json:"result"` + } `json:"data"` +} + +func handleBlive(ctx *zero.Ctx) { + // 解析命令参数 + rawCmd := compiledCommandRegex.FindAllStringSubmatch(ctx.MessageString(), -1) + targetKeyword := string([]byte(rawCmd[0][0])[6:]) // 截去命令前缀"bililive " + // 请求API + response, err := http.Get(fmt.Sprintf(BiliSearchAPI, targetKeyword)) + if err != nil { + logger.Errorln("查询播主错误:", err) + ctx.SendChain(message.Text("查询播主错误")) + return + } + + // 解析json + var userResp searchResp + err = json.NewDecoder(response.Body).Decode(&userResp) + if err != nil { + logger.Errorln("解析json错误:", err) + ctx.SendChain(message.Text("查询播主错误")) + return + } + + defer response.Body.Close() + + if len(userResp.Data.Result.LiveUser) == 0 { + logger.Debugln("未查询到播主:", targetKeyword) + ctx.SendChain(message.Text("未查询到播主:", targetKeyword)) + return + } + + // 请求API + response, err = http.Get(fmt.Sprintf(BiliSearchAPI, strconv.FormatUint(uint64(userResp.Data.Result.LiveUser[0].RoomID), 10))) + if err != nil { + logger.Errorln("查询直播间错误:", err) + ctx.SendChain(message.Text("查询直播间错误")) + return + } + + // 解析json + var roomResp searchResp + err = json.NewDecoder(response.Body).Decode(&roomResp) + if err != nil { + logger.Errorln("解析json错误:", err) + ctx.SendChain(message.Text("查询直播间错误")) + return + } + + defer response.Body.Close() + + if len(roomResp.Data.Result.LiveRoom) == 0 { + logger.Debugln("未查询到播主直播间:", strconv.FormatUint(uint64(userResp.Data.Result.LiveUser[0].RoomID), 10)) + } + + // 格式化回复信息 + var replyBuilder strings.Builder + replyBuilder.WriteString("主播: ") + replyBuilder.WriteString(roomResp.Data.Result.LiveUser[0].UserName) + if userResp.Data.Result.LiveUser[0].IsLive { + replyBuilder.WriteString("【直播中】\n") + replyBuilder.WriteString(roomResp.Data.Result.LiveRoom[0].Title) + replyBuilder.WriteString("\n--------------------\n") + replyBuilder.WriteString("开播时间: ") + replyBuilder.WriteString(roomResp.Data.Result.LiveRoom[0].LiveTime) + replyBuilder.WriteRune('\n') + replyBuilder.WriteString(strconv.FormatUint(uint64(roomResp.Data.Result.LiveRoom[0].Watched.Num), 10)) + replyBuilder.WriteString("人观看过\n") + } else { + replyBuilder.WriteString("【未开播】\n") + } + replyBuilder.WriteString(BiliLiveURL) + replyBuilder.WriteString(strconv.FormatUint(uint64(userResp.Data.Result.LiveUser[0].RoomID), 10)) + reply := replyBuilder.String() + // 发送信息 + if userResp.Data.Result.LiveUser[0].IsLive { + ctx.SendChain( + message.Image("https:"+roomResp.Data.Result.LiveRoom[0].UserCover), + message.Text("\n"+reply), + ) + } else { + ctx.SendChain(message.Text(reply)) + } +} diff --git a/plugins/blive/init.go b/plugins/blive/init.go new file mode 100644 index 0000000..3cdb345 --- /dev/null +++ b/plugins/blive/init.go @@ -0,0 +1,37 @@ +// Package blive B站直播搜索 +package blive + +import ( + "regexp" + + "github.com/DaydreamCafe/Cocoa/V2/utils/control" + zero "github.com/wdvxdr1123/ZeroBot" +) + +const ( + // CommandRegex 直播间搜索正则表达式 + CommandRegex = `^blive \S+` +) + +var ( + // compiledCommandRegex 编译后的直播间搜索正则表达式 + compiledCommandRegex *regexp.Regexp = regexp.MustCompile(CommandRegex) +) + +func init() { + // 设置插件信息 + metadata := control.Metadata{ + Name: "blive", + Version: "1.0.0", + Description: "Bilibili直播搜索", + Author: "jiangnan777312 / WhitePaper233", + Usage: `-提供B站直播搜索`, + } + // 初始化插件 + engine := control.Registe(&metadata, control.EchoAny) + + // 处理直播间搜索 + engine.OnRegex(CommandRegex, zero.OnlyGroup).Handle( + control.CheckPremissionHandler(handleBlive, 5, control.OnlyEchoError), + ) +}