BilibiliDanmu

package module
v0.0.0-...-2dc90fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

README

BilibiliDanmu

B站弹幕API

关于

原项目:https://github.com/BladeHiker/BiliDanmu_go
感谢开源,但是因为我还不熟悉go的mod并且需要进行较大修改所以fork了一份重新开了一份((((

实现内容:

  • 基本弹幕获取
  • 礼物获取
  • 房管/舰队进出获取

使用方式:

c, err := BilibiliDanmu.NewClient(uint32(roomID))
if err != nil {
  log.Println("models.NewClient err: ", err)
  return
}
pool, err := c.Start();
if err != nil {
  log.Println("c.Start err :", err)
  return
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
for {
  select {
    case uc := <-pool.MsgUncompressed:
      // 目前只处理未压缩数据的关注数变化信息
      if cmd := json.Get([]byte(uc), "cmd").ToString(); BilibiliDanmu.CMD(cmd) == BilibiliDanmu.CMDRoomRealTimeMessageUpdate {
        fans := json.Get([]byte(uc), "data", "fans").ToInt()
        log.Println("当前房间关注数变动:", fans)
      }
    case src := <-pool.UserMsg:
      m := BilibiliDanmu.NewDanmu()
      m.GetDanmuMsg([]byte(src))
      log.Printf("%d-%s | %d-%s: %s\n", m.MedalLevel, m.MedalName, m.Ulevel, m.Uname, m.Text)

    case src := <-pool.UserGift:
      g := BilibiliDanmu.NewGift()
      g.GetGiftMsg([]byte(src))
      log.Printf("%s %s 价值 %d 的 %s\n", g.UUname, g.Action, g.Price, g.GiftName)
    case src := <-pool.UserEnter:
      name := json.Get([]byte(src), "data", "uname").ToString()
      log.Printf("欢迎VIP %s 进入直播间", name)
    case src := <-pool.UserGuard:
      name := json.Get([]byte(src), "data", "username").ToString()
      log.Printf("欢迎房管 %s 进入直播间", name)
    case src := <-pool.UserEntry:
      cw := json.Get([]byte(src), "data", "copy_writing").ToString()
      log.Printf("%s", cw)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RealID      = "http://api.live.bilibili.com/room/v1/Room/room_init" // params: id=xxx
	DanMuServer = "ks-live-dmcmt-bj6-pm-02.chat.bilibili.com:443"
)

Functions

func ByteArrToDecimal

func ByteArrToDecimal(src []byte) (sum int)

返回字节数组表示数的十进制形式

func GetRealRoomID

func GetRealRoomID(short int) (realID uint32, err error)

func GetToken

func GetToken(roomid uint32) (key string)

GetToken return the necessary token for connecting to the server

func ZlibInflate

func ZlibInflate(compress []byte) ([]byte, error)

Types

type CMD

type CMD string
var (
	CMDDanmuMsg                  CMD = "DANMU_MSG"                     // 普通弹幕信息
	CMDSendGift                  CMD = "SEND_GIFT"                     // 普通的礼物,不包含礼物连击
	CMDWELCOME                   CMD = "WELCOME"                       // 欢迎VIP
	CMDWelcomeGuard              CMD = "WELCOME_GUARD"                 // 欢迎房管
	CMDEntry                     CMD = "ENTRY_EFFECT"                  // 欢迎舰长等头衔
	CMDRoomRealTimeMessageUpdate CMD = "ROOM_REAL_TIME_MESSAGE_UPDATE" // 房间关注数变动
)

type Client

type Client struct {
	Room    RoomInfo     `json:"room"`
	Request *RequestInfo `json:"request"`

	Connected bool `json:"connected"`
	// contains filtered or unexported fields
}

Client instance

func NewClient

func NewClient(roomid uint32) (c *Client, err error)

new websocket("wss)

func (*Client) HeartBeat

func (c *Client) HeartBeat()

func (*Client) ReceiveMsg

func (c *Client) ReceiveMsg(pool *Pool)

func (*Client) SendPackage

func (c *Client) SendPackage(packetlen uint32, magic uint16, ver uint16, typeID uint32, param uint32, data []byte) (err error)

func (*Client) Start

func (c *Client) Start() (pool *Pool, err error)

type DanMuMsg

type DanMuMsg struct {
	UID        uint32 `json:"uid"`
	Uname      string `json:"uname"`
	Ulevel     uint32 `json:"ulevel"`
	Text       string `json:"text"`
	MedalLevel uint32 `json:"medal_level"`
	MedalName  string `json:"medal_name"`
	MedalId    uint32 `json:"medal_uid"`
}

func NewDanmu

func NewDanmu() *DanMuMsg

func (*DanMuMsg) GetDanmuMsg

func (d *DanMuMsg) GetDanmuMsg(source []byte)

type Gift

type Gift struct {
	UUname   string `json:"u_uname"`
	Action   string `json:"action"`
	Num      uint32 `json:"num"`
	Price    uint32 `json:"price"`
	GiftName string `json:"gift_name"`
}

func NewGift

func NewGift() *Gift

func (*Gift) GetGiftMsg

func (g *Gift) GetGiftMsg(source []byte)

type Notice

type Notice struct {
}

type Pool

type Pool struct {
	UserMsg         chan string
	UserGift        chan string
	UserEnter       chan string
	UserGuard       chan string
	MsgUncompressed chan string
	UserEntry       chan string
}

Pool`s fields map CMD value

func NewPool

func NewPool() *Pool

type RequestInfo

type RequestInfo struct {
	Uid       uint8  `json:"uid"`
	Roomid    uint32 `json:"roomid"`
	Protover  uint8  `json:"protover"`
	Platform  string `json:"platform"`
	Clientver string `json:"clientver"`
	Type      uint8  `json:"type"`
	Key       string `json:"key"`
}

data on handshake packets

func NewRequestInfo

func NewRequestInfo(roomid uint32) *RequestInfo

NewRequestInfo return initialized structure

type RoomInfo

type RoomInfo struct {
	RoomId     uint32 `json:"room_id"`
	UpUid      uint32 `json:"up_uid"`
	Title      string `json:"title"`
	Online     uint32 `json:"online"`
	Tags       string `json:"tags"`
	LiveStatus bool   `json:"live_status"`
	LockStatus bool   `json:"lock_status"`
}

Basic information of the live room

func GetRoomInfo

func GetRoomInfo(roomid uint32) (roomInfo RoomInfo)

type WelCome

type WelCome struct {
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL