biliopen

package module
v0.0.0-...-2417de6 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: MIT Imports: 19 Imported by: 0

README

bili-open-live-go

godoc

(非官方)基于哔哩哔哩官方开放平台 API实现的直播长连协议客户端 Golang 版本

这个项目还在施工中,API 随时可能发生变化,欢迎有动手能力的小伙伴体验,更欢迎一起共建完善造福社区

基本使用

你需要现在官方平台注册为开发者,并创建自己的应用,AppKey 和 AppSecret 通常和账号绑定,申请后官方会发送邮件给你, 如有疑问请联系官方客服~

Get it

在你的 Go 项目中安装依赖:

go get -u github.com/fython/bili-open-live-go

创建客户端

import biliopen "github.com/fython/bili-open-live-go"

client := &biliopen.LiveClient{
	AppKey:    yourAppKey,    // 申请得到的 App Key
	AppSecret: yourAppSecret, // 申请得到的 App Secret
	ProjectID: yourProjectID, // 应用项目 ID
}
client.OnDanmaku = func(dm biliopen.Danmaku) {
    log.Printf("收到弹幕:%+v", dm)
}

建立连接

ctx := context.Background()
if err := client.Connect(ctx, os.Getenv("LIVE_CODE")); err != nil {
	t.Fatal(err)
}

More

暂无文档,阅读 client_test.go 或源码定义了解更多用法

实现清单

  • 基本协议实现
  • 弹幕回调
  • 错误码文案 & 抓取脚本
  • 消息回调字段对齐官方版本 & 抓取版本
  • 服务端心跳包超时
  • 自动重连
  • Game API 完整实现(此项目主要是为了实现直播间长连协议,暂无计划支持)

Contacts

欢迎通过 Git Issues 或个人联系方式交流项目

Email: fythonx@gmail.com

Telegram: @fython

Licenses

MIT License

Copyright (c) 2023 Siubeng (fython)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

generated by buildtools/browser-get-err-code.js. DO NOT EDIT

Index

Constants

View Source
const (
	HeaderBiliCommPrefix       = "X-Bili-"
	HeaderBiliContentMD5       = HeaderBiliCommPrefix + "Content-MD5"
	HeaderBiliTimestamp        = HeaderBiliCommPrefix + "Timestamp"
	HeaderBiliSignatureMethod  = HeaderBiliCommPrefix + "Signature-Method"
	HeaderBiliSignatureNonce   = HeaderBiliCommPrefix + "Signature-Nonce"
	HeaderBiliAccessKeyId      = HeaderBiliCommPrefix + "AccessKeyId"
	HeaderBiliSignatureVersion = HeaderBiliCommPrefix + "Signature-Version"
)
View Source
const (
	ApiHostRelease = "https://live-open.biliapi.com"
)
View Source
const (
	// CmdLiveOpenPlatformDm 在 Websocket 协议中接收到的消息类型:开放平台弹幕,目前只实现了这个类型
	CmdLiveOpenPlatformDm = "LIVE_OPEN_PLATFORM_DM"
)

Variables

This section is empty.

Functions

func GenerateSignature

func GenerateSignature(appSecret string, header http.Header) string

Types

type ApiTransport

type ApiTransport struct {
	AppKey    string
	AppSecret string
	Transport http.RoundTripper
}

ApiTransport implements bili open api http transport with auto signature

these request headers will be generated in RoundTrip method: - X-Bili-Timestamp - X-Bili-Signature-Method - X-Bili-Content-MD5 - Authorization

func (ApiTransport) RoundTrip

func (t ApiTransport) RoundTrip(r *http.Request) (rsp *http.Response, err error)

type CommonError

type CommonError struct {
	Code      CommonErrorCode
	Message   string
	RequestID string
}

CommonError 公共错误

func (CommonError) Error

func (e CommonError) Error() string

type CommonErrorCode

type CommonErrorCode int

CommonErrorCode 公共错误码

func (CommonErrorCode) Desc

func (c CommonErrorCode) Desc() string

func (CommonErrorCode) String

func (c CommonErrorCode) String() string

type CommonResponse

type CommonResponse[T any] struct {
	Code      CommonErrorCode `json:"code"`
	Message   string          `json:"message"`
	RequestID string          `json:"request_id"`
	Data      T               `json:"data"`
}

func (CommonResponse[T]) Err

func (r CommonResponse[T]) Err() error

func (CommonResponse[T]) String

func (r CommonResponse[T]) String() string

type Danmaku

type Danmaku struct {
	// Timestamp 时间戳
	Timestamp int `json:"timestamp"`
	// RoomID 直播间 ID
	RoomID int `json:"room_id"`

	// UID 用户 UID
	UID int `json:"uid"`
	// Username 用户名
	Username string `json:"uname"`
	// UserFace 用户头像
	UserFace string `json:"uface"`
	// Admin 是否房管
	Admin int `json:"admin"`
	// Vip 是否月费会员
	Vip int `json:"vip"`
	// SVip 是否年费会员
	SVip int `json:"svip"`

	// MsgType 是否礼物弹幕(节奏风暴)
	MsgType int `json:"msg_type"`
	// DMType 弹幕类型,枚举值参考 DanmakuType 类型常量
	DMType DanmakuType `json:"dm_type"`

	// Message 弹幕内容
	Message string `json:"msg"`
	// MessageID 弹幕 ID,猜测用于去重
	MessageID string `json:"msg_id"`
	// EmojiImgUrl 表情地址
	EmojiImgUrl string `json:"emoji_img_url"`

	// FansMedalLevel 粉丝牌等级
	FansMedalLevel int `json:"fans_medal_level"`
	// FansMedalName 粉丝牌名称
	FansMedalName string `json:"fans_medal_name"`
	// FansMedalWearingStatus 粉丝牌是否穿戴
	FansMedalWearingStatus bool `json:"fans_medal_wearing_status"`
}

Danmaku 弹幕信息

type DanmakuType

type DanmakuType int

DanmakuType 弹幕类型

const (
	// DanmakuTypeText 文字
	DanmakuTypeText DanmakuType = 0
	// DanmakuTypeSticker 表情
	DanmakuTypeSticker DanmakuType = 1
	// DanmakuTypeVoice 语音
	DanmakuTypeVoice DanmakuType = 2
)

type LiveClient

type LiveClient struct {
	ApiHost   string
	AppKey    string
	AppSecret string
	ProjectID int64

	OnDanmaku func(Danmaku)
	OnClose   func(error)
	// contains filtered or unexported fields
}

LiveClient 直播 API 客户端实现

协议根据官方开发文档实现:https://open-live.bilibili.com/document/74eec767-e594-7ddd-6aba-257e8317c05d

func (*LiveClient) Connect

func (c *LiveClient) Connect(ctx context.Context, liveCode string) error

Connect 建立直播间连接

需要传入主播自己的身份码,而不是直播间 ID,遂不支持监听其他人的直播间

func (*LiveClient) Disconnect

func (c *LiveClient) Disconnect(ctx context.Context) error

Disconnect 断开连接

func (*LiveClient) IsActive

func (c *LiveClient) IsActive() bool

IsActive 检查客户端是否在线

Jump to

Keyboard shortcuts

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