Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 新增Bilibili直播间搜索功能 #27

Open
wants to merge 16 commits into
base: V2.1-Dev
Choose a base branch
from
1 change: 1 addition & 0 deletions Cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion plugins/bilibili_parse/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
Version: "1.0.0",
Description: "Bilibili视频解析插件",
Author: "WhitePaper233",
Usage: "发送任意形式的B站分享链接、番号及移动端分享卡片, 将自动解析出视频信息",
Usage: `-发送任意形式的B站分享链接、番号及移动端分享卡片, 将自动解析出视频信息`,
}
// 初始化插件
engine := control.Registe(&metadata, control.EchoAny)
Expand Down
126 changes: 126 additions & 0 deletions plugins/blive/handler.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
37 changes: 37 additions & 0 deletions plugins/blive/init.go
Original file line number Diff line number Diff line change
@@ -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),
)
}