bililive

package module
v0.0.0-...-e59ea6c Latest Latest
Warning

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

Go to latest
Published: May 14, 2021 License: MIT Imports: 18 Imported by: 0

README

说明

bilibili哔哩哔哩 直播弹幕和礼物获取SDK(非官方)

简单用法

package main

import (
	"log"
	"context"
	"github.com/holynnchen/bililive"
)

func main() {
		live := &bililive.Live{
    		Debug:              false, // 不输出日志
    		AnalysisRoutineNum: 1,     // 消息分析协程数量,默认为1,为1可以保证通知顺序与接收到消息顺序相同
    		StormFilter:        true,  // 过滤节奏风暴弹幕
    		Live: func(roomID int) {
    			log.Println("【直播开始】")
    		},
    		End: func(roomID int) {
    			log.Println("【直播结束】")
    		},
    		ReceivePopularValue: func(roomID int, v uint32) {
    			log.Printf("【直播人气】%v", v)
    		},
    		RoomChange: func(roomID int, m *bililive.RoomChangeModel) {
    			log.Printf("【信息变更】标题:【%s】,分区:【%s-%s】", m.Title, m.ParentAreaName, m.AreaName)
    		},
    		RoomRank: func(roomID int, m *bililive.RankModel) {
    			rankTime := time.Unix(m.Timestamp, 0).Format("2006-01-02 15:04:05")
    			log.Printf("【小时排名】%s %s", rankTime, m.RankDesc)
    		},
    		UserEnter: func(roomID int, m *bililive.UserEnterModel) {
    			log.Printf("【用户进入】欢迎 %v(%v) 进入直播间", m.UserName, m.UserID)
    		},
    		GuardEnter: func(roomID int, m *bililive.GuardEnterModel) {
    			log.Printf("【舰长进入】欢迎 舰长 - %v(%v) 进入直播间", m.UserName, m.UserID)
    		},
    		ReceiveMsg: func(roomID int, msg *bililive.MsgModel) {
    			log.Printf("【弹幕消息】%v(%v):  %v", msg.UserName, time.Unix(msg.Timestamp, 0), msg.Content)
    		},
    		ReceiveGift: func(roomID int, gift *bililive.GiftModel) {
    			coin := "银瓜子"
    			if gift.CoinType == "gold" {
    				coin = "金瓜子"
    			}
    			log.Printf("【礼物通知】%s:  %s(%d) * %d [价值 %d个%s]", gift.UserName, gift.GiftName, gift.GiftID, gift.Num, gift.Price*gift.Num, coin)
    		},
    		GiftComboSend: func(roomID int, m *bililive.ComboSendModel) {
    			log.Printf("【礼物连击】%v 赠送 %v 连击 %v 次", m.UserName, m.GiftName, m.ComboNum)
    		},
    		GiftComboEnd: func(roomID int, m *bililive.ComboEndModel) {
    			log.Printf("【连击结束】%v 赠送 %v(价值%v) 总共连击 %v 次", m.UserName, m.GiftName, m.Price, m.ComboNum)
    		},
    		GuardBuy: func(roomID int, m *bililive.GuardBuyModel) {
    			log.Printf("【用户上船】欢迎 %v - %v(%v) 上船", m.GiftName, m.UserName, m.UserID)
    		},
    		FansUpdate: func(roomID int, m *bililive.FansUpdateModel) {
    			log.Printf("【粉丝更新】当前粉丝数 %d", m.Fans)
    		},
    		SpecialGift: func(roomID int, m *bililive.SpecialGiftModel) {
    			if m.Storm.Action == "start" {
    				log.Printf("【节奏风暴】开始,id:%d,数量:%d,内容:%s", m.Storm.ID, m.Storm.Num, m.Storm.Content)
    			}
    			if m.Storm.Action == "end" {
    				log.Printf("【节奏风暴】结束,id:%d", m.Storm.ID)
    			}
    		},
    		SuperChatMessage: func(roomID int, m *bililive.SuperChatMessageModel) {
    			log.Printf("【醒目留言】%s :%s | 价值 %d 元", m.UserInfo.UserName, m.Message, m.Price)
    		},
    	}
    	live.Start(context.Background())
    	live.Join(roomID1, roomID2)
    	live.Wait()
}

Documentation

Index

Constants

View Source
const (
	WS_OP_HEARTBEAT                int32 = 2
	WS_OP_HEARTBEAT_REPLY          int32 = 3
	WS_OP_MESSAGE                  int32 = 5
	WS_OP_USER_AUTHENTICATION      int32 = 7
	WS_OP_CONNECT_SUCCESS          int32 = 8
	WS_PACKAGE_HEADER_TOTAL_LENGTH int32 = 16
	//WS_PACKAGE_OFFSET                int32 = 0
	//WS_HEADER_OFFSET                 int32 = 4
	//WS_VERSION_OFFSET                int32 = 6
	//WS_OPERATION_OFFSET              int32 = 8
	//WS_SEQUENCE_OFFSET               int32 = 12
	//WS_BODY_PROTOCOL_VERSION_NORMAL  int32 = 0
	WS_BODY_PROTOCOL_VERSION_DEFLATE int16 = 2
	WS_HEADER_DEFAULT_VERSION        int16 = 1
	//WS_HEADER_DEFAULT_OPERATION      int32 = 1
	WS_HEADER_DEFAULT_SEQUENCE int32 = 1
	WS_AUTH_OK                 int32 = 0
	WS_AUTH_TOKEN_ERROR        int32 = -101
)
View Source
const (
	DEBUG_ALL            = 1<<64 - 1
	DEBUG_UNKNOW_MSG     = 1
	DEBUG_ERROR          = 1 << 1 // err!=nil
	DEBUG_CONNECT_SUCESS = 1 << 2
	DEBUG_CREATE_CONNECT = 1 << 3
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ComboEndModel

type ComboEndModel struct {
	GiftName   string `json:"gift_name"`   // 礼物名称
	ComboNum   int    `json:"combo_num"`   // 连击数量
	UserName   string `json:"uname"`       // 用户名称
	GiftID     int    `json:"gift_id"`     // 礼物ID
	Price      int    `json:"price"`       // 价格
	GuardLevel int    `json:"guard_level"` // 舰长等级
	StartTime  int64  `json:"start_time"`  // 开始时间
	EndTime    int64  `json:"end_time"`    // 结束时间
}

ComboEndModel 连击结束模型

type ComboSendModel

type ComboSendModel struct {
	UserName string `json:"uname"`     // 用户名称
	GiftName string `json:"gift_name"` // 礼物名称
	GiftID   int    `json:"gift_id"`   // 礼物ID
	ComboNum int    `json:"combo_num"` // 连击数量
}

ComboSendModel 连击模型

type FansUpdateModel

type FansUpdateModel struct {
	RoomID    int `json:"roomid"`
	Fans      int `json:"fans"`
	RedNotice int `json:"red_notice"`
}

FansUpdateModel 粉丝更新模型

type GiftModel

type GiftModel struct {
	GiftName  string `json:"giftName"`       // 礼物名称
	Num       int    `json:"num"`            // 数量
	UserName  string `json:"uname"`          // 用户名称
	UserID    int64  `json:"uid"`            // 用户ID
	GiftID    int    `json:"giftId"`         // 礼物ID
	Price     int    `json:"price"`          // 价格
	CoinType  string `json:"coin_type"`      // 硬币类型
	FaceURL   string `json:"face"`           // 头像url
	Combo     int    `json:"super_gift_num"` // 连击
	Timestamp int64  `json:"timestamp"`      // 时间
}

GiftModel 礼物模型

type GuardBuyModel

type GuardBuyModel struct {
	GiftName   string `json:"gift_name"`   // 礼物名称
	Num        int    `json:"num"`         // 数量
	UserID     int64  `json:"uid"`         // 用户ID
	UserName   string `json:"username"`    // 用户名称
	GiftID     int    `json:"gift_id"`     // 礼物ID
	Price      int    `json:"price"`       // 价格
	GuardLevel int    `json:"guard_level"` // 舰长等级
}

GuardBuyModel 上船模型

type GuardEnterModel

type GuardEnterModel struct {
	UserID     int64  `json:"uid"`
	UserName   string `json:"username"`
	GuardLevel int    `json:"guard_level"`
}

GuardEnterModel 舰长进入模型

type Live

type Live struct {
	Debug               uint64                            // 是否显示日志
	AnalysisRoutineNum  int                               // 消息分析协程数量,默认为1,为1可以保证通知顺序与接收到消息顺序相同
	StormFilter         bool                              // 过滤节奏风暴弹幕,默认false不过滤
	LotteryDanmuFilter  bool                              // 过滤天选弹幕,默认为false不过滤
	Live                func(int)                         // 直播开始通知
	End                 func(int)                         // 直播结束通知
	ReceiveMsg          func(int, *MsgModel)              // 接收消息方法
	ReceiveGift         func(int, *GiftModel)             // 接收礼物方法
	ReceivePopularValue func(int, uint32)                 // 接收人气值方法
	UserEnter           func(int, *UserEnterModel)        // 用户进入方法
	GuardEnter          func(int, *GuardEnterModel)       // 舰长进入方法
	GiftComboSend       func(int, *ComboSendModel)        // 礼物连击方法
	GiftComboEnd        func(int, *ComboEndModel)         // 礼物连击结束方法
	GuardBuy            func(int, *GuardBuyModel)         // 上船
	FansUpdate          func(int, *FansUpdateModel)       // 粉丝数更新
	RoomRank            func(int, *RankModel)             // 小时榜
	RoomChange          func(int, *RoomChangeModel)       // 房间信息变更
	SpecialGift         func(int, *SpecialGiftModel)      // 特殊礼物
	SuperChatMessage    func(int, *SuperChatMessageModel) // 超级留言
	SysMessage          func(int, *SysMsgModel)           // 系统信息
	Proxy               func() func(*http.Request) (*url.URL, error)
	// contains filtered or unexported fields
}

Live 直播间

func (*Live) Join

func (live *Live) Join(roomIDs ...int) error

Join 添加房间

func (*Live) Remove

func (live *Live) Remove(roomIDs ...int) error

Remove 移出房间

func (*Live) Start

func (live *Live) Start(ctx context.Context)

Start 开始接收

func (*Live) Wait

func (live *Live) Wait()

type MsgModel

type MsgModel struct {
	UserID      int64  // 用户ID
	UserName    string // 用户昵称
	UserLevel   int    // 用户等级
	IsAdmin     bool   // 是否房管
	MedalName   string // 勋章名
	MedalUpName string // 勋章主播名称
	MedalRoomID int64  // 勋章直播间ID
	MedalLevel  int    // 勋章等级
	Content     string // 内容
	Timestamp   int64  // 时间
	CT          string // 弹幕token
}

MsgModel 消息

type MsgProcessList

type MsgProcessList struct {
	// contains filtered or unexported fields
}

func NewMsgProcessList

func NewMsgProcessList() *MsgProcessList

func (*MsgProcessList) Get

func (l *MsgProcessList) Get() interface{}

func (*MsgProcessList) Put

func (l *MsgProcessList) Put(data interface{})

type RankModel

type RankModel struct {
	RoomID    int    `json:"roomid"`
	RankDesc  string `json:"rank_desc"`
	Timestamp int64  `json:"timestamp"`
}

RankModel 小时榜模型

type RoomChangeModel

type RoomChangeModel struct {
	Title          string `json:"title"`
	AreaID         int    `json:"area_id"`
	ParentAreaID   int    `json:"parent_area_id"`
	AreaName       string `json:"area_name"`
	ParentAreaName string `json:"parent_area_name"`
}

RoomChangeModel 房间基础信息变更

type SpecialGiftModel

type SpecialGiftModel struct {
	Storm struct {
		ID      int64       `json:"-"`
		TempID  interface{} `json:"id"` // 因为b站通知节奏风暴开始和结束id类型不同,用这个变量作为中转
		Action  string      `json:"action"`
		Content string      `json:"content"`
		Num     int         `json:"num"`
		Time    int         `json:"time"`
	} `json:"39"`
}

SpecialGiftModel 特殊礼物模型

type SuperChatMessageModel

type SuperChatMessageModel struct {
	Price    int    `json:"price"`
	Message  string `json:"message"`
	UserInfo struct {
		UserName string `json:"uname"`
	} `json:"user_info"`
}

SuperChatMessageModel 超级留言模型

type SysMsgModel

type SysMsgModel struct {
	Cmd     string `json:"cmd"`
	Msg     string `json:"msg"`
	MsgText string `json:"msg_text"`
}

SysMsgModel 系统信息

type UserEnterModel

type UserEnterModel struct {
	UserID   int64  `json:"uid"`
	UserName string `json:"uname"`
	IsAdmin  bool   `json:"is_admin"`
	VIP      int    `json:"vip"`
	SVIP     int    `json:"svip"`
}

UserEnterModel 用户进入模型

Jump to

Keyboard shortcuts

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