api

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

B站直播API

B站直播相关API的Go语言实现

目前仅支持直播间弹幕相关API

支持

  • 礼物
  • 超级留言
  • 弹幕
  • 其他 websocket 事件

详见dto/目录下的文件

快速开始

安装
go get github.com/spellingDragon/bili-live-api

使用
package main

import (
	"fmt"

	api "github.com/spellingDragon/bili-live-api"
	"github.com/spellingDragon/bili-live-api/dto"
	"github.com/spellingDragon/bili-live-api/log"
	"github.com/spellingDragon/bili-live-api/websocket"

)

func main() {
	// 设置内部 logger 打印日志等级
	// 或通过 log.ReplaceDefault(customLogger) 来使用自定义 logger
	log.SetLogLevel(log.LevelWarn)

	// 参数为房间号
	l := api.NewLive(510)

	// 内部通过 type switch 来判断 handler 类型
	// handler 类型定义在 websocket 包中
	err := l.RegisterHandlers(
		danmakuHandler(),
		giftHandler(),
		guardHandler(),
		superChatHandler(),
	)
	if err != nil {
		panic(err)
	}

	l.Start()
}

func danmakuHandler() websocket.DanmakuHandler {
	return func(danmaku *dto.Danmaku) {
		fmt.Printf("%s:%s\n", danmaku.Uname, danmaku.Content)
	}
}

func giftHandler() websocket.GiftHandler {
	return func(gift *dto.Gift) {
		fmt.Printf("%s 赠送 [礼物%.1f¥]%s x %d\n", gift.Uname, float64(gift.Price)/1000, gift.GiftName, gift.Num)
	}
}

func guardHandler() websocket.GuardHandler {
	return func(guard *dto.Guard) {
		fmt.Printf("%s 开通舰长,级别%d", guard.Username, guard.GuardLevel)
	}
}

func superChatHandler() websocket.SuperChatHandler {
	return func(superChat *dto.SuperChat) {
		fmt.Printf("[%d¥SC]%s:%s\n", superChat.Price, superChat.UserInfo.Uname, superChat.Message)
	}
}

相关

项目组织结构参考: botgo

Documentation

Index

Constants

View Source
const DefaultCookiePath = "cookie.json"

Variables

This section is empty.

Functions

This section is empty.

Types

type Live

type Live struct {
	Client       *websocket.Client
	ResourceAPI  *resource.API
	RoomID       int
	RoomInfo     *resource.RoomInfo
	UserInfo     *resource.UserInfo
	FollowerInfo *resource.FollowerInfo
}

Live 使用 NewLive() 来初始化

func NewLive

func NewLive(roomID int) *Live

NewLive 构造函数

func (*Live) GetStreamURL added in v0.4.3

func (l *Live) GetStreamURL(qn int) string

func (*Live) Listen

func (l *Live) Listen() error

func (*Live) RefreshRoom

func (l *Live) RefreshRoom() error

func (*Live) RegisterHandlers

func (l *Live) RegisterHandlers(handlers ...interface{}) error

RegisterHandlers 注册不同的事件处理 handler类型需要是定义在 websocket/handler_registration.go 中的类型,如: - websocket.DanmakuHandler - websocket.GiftHandler - websocket.GuardHandler

func (*Live) Start

func (l *Live) Start()

Start 接收房间号,开始websocket心跳连接并阻塞

func (*Live) Stop

func (l *Live) Stop()

Start 接收房间号,开始websocket心跳连接并阻塞

Directories

Path Synopsis
Package dto 接口通用数据传输对象
Package dto 接口通用数据传输对象
Package log 是 SDK 的 logger 接口定义与内置的 logger。
Package log 是 SDK 的 logger 接口定义与内置的 logger。

Jump to

Keyboard shortcuts

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