Code Monkey home page Code Monkey logo

bili-live-api's Introduction

B站直播API

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

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

支持

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

详见dto/目录下的文件

快速开始

安装

go get github.com/botplayerneo/bili-live-api

使用

package main

import (
	"fmt"

	api "github.com/botplayerneo/bili-live-api"
	"github.com/botplayerneo/bili-live-api/dto"
	"github.com/botplayerneo/bili-live-api/log"
	"github.com/botplayerneo/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

bili-live-api's People

Contributors

playerneo42 avatar

Stargazers

 avatar James Brown avatar  avatar HCLonely avatar catmade avatar

Watchers

 avatar

Forkers

spellingdragon

bili-live-api's Issues

能否在log中更改为可配置默认的logger

如题,我想用这个包作为我下游依赖,同时有非常多的直播间进行websocket链接,但是用底层包的之后每次新连接ws都会print进房成功,我看应该是log包中的default使用的是终端,能否加一个config可以选择装载自定义的logger,感谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.