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
Prev Previous commit
Next Next commit
Update handler.go
jiangnan777312 committed Aug 4, 2022

Verified

This commit was signed with the committer’s verified signature.
olsh Oleg Shevchenko
commit 50aadad256cc16237d88f9e3840576805b56dfa0
66 changes: 31 additions & 35 deletions plugins/bilibili_search/handler.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ package bilibilisearch
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
@@ -23,33 +22,41 @@ const (
BiliLiveURL = "https://live.bilibili.com/"
)

// BiliLiveUserSearchAPIResp Bilibili直播用户搜索返回结构体
type BiliLiveUserSearchAPIResp struct {
Code int `json:"code"`
Data struct {
Result []struct {
IsLive bool `json:"is_live"`
RoomID int `json:"roomid"`
} `json:"result"`
} `json:"data"`
}

// BiliLiveSearchAPIResp Bilibili直播搜索返回结构体
type BiliLiveSearchAPIResp struct {
Code int `json:"code"`
Data struct {
Result struct {
LiveRoom []struct {
Title string `json:"title"`
CoverURL string `json:"user_cover"`
Watched struct {
RoomCover string `json:"cover"`
LiveTime string `json:"live_time"`
Title string `json:"title"`
UserFace string `json:"uface"`
UserCover string `json:"user_cover"`
UserName string `json:"uname"`
Watched struct {
Num int `json:"num"`
} `json:"watched_show"`
} `json:"watchers_show"`
} `json:"live_room"`
LiveUser []struct {
IsLive bool `json:"is_live"`
LiveTime string `json:"live_time"`
RoomID int `json:"roomid"`
UserFace string `json:"uface"`
UserName string `json:"uname"`
} `json:"live_user"`
} `json:"result"`
} `json:"data"`
}

// LiveSearch 直播间搜索
func handleLiveSearch(ctx *zero.Ctx) {
// API参数->搜索直播用户
LiveSearchAPI := fmt.Sprintf(BiliSearchAPI, "1", "live_user")
LiveUserSearchAPI := fmt.Sprintf(BiliSearchAPI, "1", "live_user")
// 处理搜索参数
raw_cmd := compiledLiveSearchRegex.FindAllStringSubmatch(ctx.MessageString(), -1)
cmd_str := string([]byte(raw_cmd[0][0])[9:]) // 截去命令前缀"bililive "
@@ -59,13 +66,13 @@ func handleLiveSearch(ctx *zero.Ctx) {
for index, arg := range args {
var reqURLBuilder strings.Builder

reqURLBuilder.WriteString(LiveSearchAPI)
reqURLBuilder.WriteString(LiveUserSearchAPI)
reqURLBuilder.WriteString(arg)
reqURLs[index] = reqURLBuilder.String()
}

// 从API获取直播用户信息,并解析为结构体
var respInfos_USER = make([]BiliLiveSearchAPIResp, len(reqURLs))
var respInfos_USER = make([]BiliLiveUserSearchAPIResp, len(reqURLs))
var respInfos_ROOM = make([]BiliLiveSearchAPIResp, len(reqURLs))
for index, reqURL := range reqURLs {
// 调用API获取信息
@@ -83,13 +90,7 @@ func handleLiveSearch(ctx *zero.Ctx) {
defer response.Body.Close()

// 将请求结果JSON解析为BiliLiveAPIResp结构体
respdata, err := ioutil.ReadAll(response.Body)
if err != nil {
logger.Errorln(err)
}
logger.Infoln("API返回结果: ", string(respdata))

var respInfo_USER BiliLiveSearchAPIResp
var respInfo_USER BiliLiveUserSearchAPIResp
err = json.NewDecoder(response.Body).Decode(&respInfo_USER)
if err != nil {
logger.Errorln("JSON解析失败:", err)
@@ -104,14 +105,14 @@ func handleLiveSearch(ctx *zero.Ctx) {
respInfos_USER[index] = respInfo_USER

// API参数->搜索直播间
LiveSearchAPI = fmt.Sprintf(BiliSearchAPI, "1", "live_room")
LiveSearchAPI := fmt.Sprintf(BiliSearchAPI, "1", "live")

// 依照RoomID重新构建reqURLs
for index := range args {
var reqURLBuilder strings.Builder

reqURLBuilder.WriteString(LiveSearchAPI)
reqURLBuilder.WriteString(formatDigit(respInfo_USER.Data.Result.LiveUser[0].RoomID))
reqURLBuilder.WriteString(formatDigit(respInfo_USER.Data.Result[0].RoomID))
reqURLs[index] = reqURLBuilder.String()
}

@@ -132,12 +133,6 @@ func handleLiveSearch(ctx *zero.Ctx) {
defer response.Body.Close()

// 将请求结果JSON解析为BiliLiveAPIResp结构体
respdata, err := ioutil.ReadAll(response.Body)
if err != nil {
logger.Errorln(err)
}
logger.Infoln("API返回结果: ", string(respdata))

var respInfo_ROOM BiliLiveSearchAPIResp
err = json.NewDecoder(response.Body).Decode(&respInfo_ROOM)
if err != nil {
@@ -157,13 +152,14 @@ func handleLiveSearch(ctx *zero.Ctx) {
// 格式化回复字符串及封面图
var replyStr = make([]string, len(respInfos_USER))
var coverLst = make([]string, len(respInfos_ROOM))
for index, respInfo := range respInfos_USER {
for index, respInfo := range respInfos_ROOM {
userInfo := respInfos_USER[index]
// 格式化字符串
var replyBuilder strings.Builder

replyBuilder.WriteString("\n主播: ")
replyBuilder.WriteString(respInfo.Data.Result.LiveUser[0].UserName)
if respInfo.Data.Result.LiveUser[0].IsLive {
replyBuilder.WriteString(respInfo.Data.Result.LiveRoom[0].UserName)
if userInfo.Data.Result[0].IsLive {
replyBuilder.WriteString("【直播中】\n")
} else {
replyBuilder.WriteString("【未开播】\n")
@@ -176,11 +172,11 @@ func handleLiveSearch(ctx *zero.Ctx) {
replyBuilder.WriteString(strconv.FormatUint(uint64(respInfo.Data.Result.LiveRoom[0].Watched.Num), 10))
replyBuilder.WriteString("人观看过\n")
replyBuilder.WriteString(BiliLiveURL)
replyBuilder.WriteString(formatDigit(respInfo.Data.Result.LiveUser[0].RoomID))
replyBuilder.WriteString(formatDigit(userInfo.Data.Result[0].RoomID))

replyStr[index] = replyBuilder.String()
//格式化封面图URL
coverLst[index] = respInfo.Data.Result.LiveRoom[0].CoverURL
coverLst[index] = respInfo.Data.Result.LiveRoom[0].UserCover
}

// 发送信息