Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
add BiliLive 直播推送 默认关闭 易被冻结谨慎使用
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoo committed Jun 17, 2021
1 parent 139ac61 commit 31cff1a
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
110 changes: 110 additions & 0 deletions Bili/live.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package bili

import (
"errors"
"fmt"
"github.com/gaoyanpao/biliLiveHelper"
"sync"
)

type LiveManager struct {
biliClient map[int]*Client
lock sync.RWMutex
}

type Client struct {
C *biliLiveHelper.Client
OnDanmu func(ctx *biliLiveHelper.Context)
OnGift func(ctx *biliLiveHelper.Context)
OnWelcome func(ctx *biliLiveHelper.Context)
}

func NewLiveManager() *LiveManager {
return &LiveManager{biliClient: make(map[int]*Client), lock: sync.RWMutex{}}
}

func (l *LiveManager) AddClient(RoomId int) (c *Client, e error) {
l.lock.Lock()
defer l.lock.Unlock()
client := biliLiveHelper.NewClient(RoomId)
if client == nil {
e = errors.New("Client Err ")
return
}
client.RegHandleFunc(biliLiveHelper.CmdDanmuMsg, func(ctx *biliLiveHelper.Context) bool {
if c.OnDanmu != nil {
c.OnDanmu(ctx)
}
return false
})
client.RegHandleFunc(biliLiveHelper.CmdSendGift, func(ctx *biliLiveHelper.Context) bool {
if c.OnGift != nil {
c.OnGift(ctx)
}
return false
})
client.RegHandleFunc(biliLiveHelper.CmdWelcome, func(ctx *biliLiveHelper.Context) bool {
if c.OnWelcome != nil {
c.OnWelcome(ctx)
}
return false
})
c = &Client{C: client}
if _, ok := l.biliClient[RoomId]; ok {
e = errors.New("已加入了Room")
return
}
l.biliClient[RoomId] = c
return
}

func (l *LiveManager) RemoveClient(RoomId int) error {
l.lock.Lock()
defer l.lock.Unlock()
if v, ok := l.biliClient[RoomId]; ok {
if v.C.IsConnected() {
v.ExitRoom()
}
delete(l.biliClient, RoomId)
return nil
} else {
return errors.New("Client不存在! ")
}
}

func (c *Client) RegisterDanmuFunc(f func(ctx *biliLiveHelper.Context)) {
c.OnDanmu = f
}

func (c *Client) RegisterGiftFunc(f func(ctx *biliLiveHelper.Context)) {
c.OnGift = f
}
func (c *Client) RegisterWelcomeFunc(f func(ctx *biliLiveHelper.Context)) {
c.OnWelcome = f
}

func (c *Client) GetRoomInfo() biliLiveHelper.SimpleRoomInfo {
return c.C.SimpleRoomInfo
}

func (c *Client) ExitRoom() error {
return c.C.Conn.Close()
}

// Start 阻塞
func (c *Client) Start() error {

return c.C.StartListen()
}
func GetLiveStatusString(status int) string {
switch status {
case 0:
return "未开播"
case 1:
return "直播中"
case 2:
return "轮播中"
default:
return fmt.Sprintf("未知状态%d", status)
}
}
32 changes: 32 additions & 0 deletions Bili/live_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bili

import (
"github.com/gaoyanpao/biliLiveHelper"
"log"
"os"
"testing"
)

func TestLive(t *testing.T) {
client := biliLiveHelper.NewClient(650)
if client == nil {
os.Exit(1)
}
client.PrintRoomInfo()
client.RegHandleFunc(biliLiveHelper.CmdDanmuMsg, func(ctx *biliLiveHelper.Context) bool {
data := ctx.Msg
log.Printf("[弹幕]<%v>%s", data.Get("info").GetIndex(2).GetIndex(1).MustString(), data.Get("info").GetIndex(1).MustString())
return false
})
client.RegHandleFunc(biliLiveHelper.CmdWelcome, func(ctx *biliLiveHelper.Context) bool {
data := ctx.Msg
log.Printf("[欢迎]%v", data.Get("data"))
return false
})
client.RegHandleFunc(biliLiveHelper.CmdSendGift, func(ctx *biliLiveHelper.Context) bool {
data := ctx.Msg
log.Printf("[礼物]%v", data.Get("data"))
return false
})
client.StartListen()
}
1 change: 1 addition & 0 deletions Config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type CoreConfigStruct struct {
Url string
QQ int64
}
BiliLive bool
YiQing bool
ReverseProxy string
DefaultGroupConfig GroupConfig
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Joker/hpp v1.0.0 // indirect
github.com/fogleman/gg v1.3.0
github.com/fsnotify/fsnotify v1.4.9
github.com/gaoyanpao/biliLiveHelper v0.0.0-20210205164551-dca3842ab39b
github.com/go-playground/webhooks/v6 v6.0.0-beta.3
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210427211137-fa175eb84754
Expand Down
45 changes: 45 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gaoyanpao/biliLiveHelper"
_ "github.com/go-playground/webhooks/v6/github"
"github.com/mcoo/requests"
"io/fs"
Expand Down Expand Up @@ -68,6 +69,8 @@ func main() {
Code string
}{}
VerifyLock := sync.Mutex{}
live := bili.NewLiveManager()

c := utils.NewBotCronManager()
c.Start()
bi := bili.NewManager()
Expand Down Expand Up @@ -612,6 +615,48 @@ func main() {
}
b.SendGroupTextMsg(packet.FromGroupID, ups)
}
if len(cm) == 2 && cm[0] == "stopBili" {
s, err := strconv.Atoi(cm[1])
if err != nil {
b.SendGroupTextMsg(packet.FromGroupID, err.Error())
return
}
err = live.RemoveClient(s)
if err != nil {
b.SendGroupTextMsg(packet.FromGroupID, err.Error())
return
}
b.SendGroupTextMsg(packet.FromGroupID, "已经断开连接了")
}
if len(cm) == 2 && cm[0] == "biliLive" {
if !Config.CoreConfig.BiliLive {
b.SendGroupTextMsg(packet.FromGroupID, "该功能没有启动")
return
}
s, err := strconv.Atoi(cm[1])
if err != nil {
b.SendGroupTextMsg(packet.FromGroupID, err.Error())
return
}
c, err := live.AddClient(s)
if err != nil {
b.SendGroupTextMsg(packet.FromGroupID, err.Error())
return
}
c.OnGift = func(ctx *biliLiveHelper.Context) {
data := ctx.Msg
r, _ := requests.Get(data.Get("data").Get("face").MustString())
b.SendGroupPicMsg(packet.FromGroupID, fmt.Sprintf("%s%s%s", data.Get("data").Get("uname").MustString(), data.Get("data").Get("action").MustString(), data.Get("data").Get("giftName").MustString()), r.Content())
}
go c.Start()
info := c.GetRoomInfo()
b.SendGroupTextMsg(packet.FromGroupID, fmt.Sprintf("房间: %s[%d]\n关注: %d\n人气: %d\n直播状态: %v",
info.Title,
info.RoomID,
info.Attention,
info.Online,
bili.GetLiveStatusString(info.LiveStatus)))
}
if len(cm) == 2 && cm[0] == "梗查询" {
b.SendGroupTextMsg(packet.FromGroupID, fmt.Sprintf("正在查询梗%s", cm[1]))
client := &http.Client{}
Expand Down

0 comments on commit 31cff1a

Please sign in to comment.