Coder Social home page Coder Social logo

blivedm-go's Introduction

blivedm-go

bilibili 直播弹幕 golang 库

安装

go get github.com/Akegarasu/blivedm-go

快速开始

基础使用

该库支持以下几种基本事件,并且支持监听自定义事件。

  • 弹幕
  • 醒目留言
  • 礼物
  • 上舰
  • 开播
  • USER_TOAST_MSG
package main

import (
	"fmt"
	"github.com/Akegarasu/blivedm-go/client"
	"github.com/Akegarasu/blivedm-go/message"
	_ "github.com/Akegarasu/blivedm-go/utils"
	log "github.com/sirupsen/logrus"
	"github.com/tidwall/gjson"
)

func main() {
	log.SetLevel(log.DebugLevel)
	c := client.NewClient(732) // 房间号
	c.SetCookie("") // 由于 B站 反爬虫改版,现在需要使用已登陆账号的 Cookie 才可以正常获取弹幕。如果不设置 Cookie,获取到的弹幕昵称、UID都被限制。还有可能弹幕限流,无法获取到全部内容。
	//弹幕事件
	c.OnDanmaku(func(danmaku *message.Danmaku) {
		if danmaku.Type == message.EmoticonDanmaku {
			fmt.Printf("[弹幕表情] %s:表情URL: %s\n", danmaku.Sender.Uname, danmaku.Emoticon.Url)
		} else {
			fmt.Printf("[弹幕] %s:%s\n", danmaku.Sender.Uname, danmaku.Content)
		}
	})
	// 醒目留言事件
	c.OnSuperChat(func(superChat *message.SuperChat) {
		fmt.Printf("[SC|%d元] %s: %s\n", superChat.Price, superChat.UserInfo.Uname, superChat.Message)
	})
	// 礼物事件
	c.OnGift(func(gift *message.Gift) {
		if gift.CoinType == "gold" {
			fmt.Printf("[礼物] %s 的 %s %d 个 共%.2f元\n", gift.Uname, gift.GiftName, gift.Num, float64(gift.Num*gift.Price)/1000)
		}
	})
	// 上舰事件
	c.OnGuardBuy(func(guardBuy *message.GuardBuy) {
		fmt.Printf("[大航海] %s 开通了 %d 等级的大航海,金额 %d 元\n", guardBuy.Username, guardBuy.GuardLevel, guardBuy.Price/1000)
	})
	// 监听自定义事件
	c.RegisterCustomEventHandler("STOP_LIVE_ROOM_LIST", func(s string) {
		data := gjson.Get(s, "data").String()
		fmt.Printf("STOP_LIVE_ROOM_LIST: %s\n", data)
	})

	err := c.Start()
	if err != nil {
		log.Fatal(err)
	}
	log.Println("started")
	// 需要自行阻塞什么方法都可以
	select {}
}

进阶使用

监听自定义事件

通过自定义监听事件,可以支持更多事件处理。
其中,cmd为要监听的cmd名(下附常见cmd名), handler为接收事件消息(字符串的JSON)的函数
注意
优先执行自定义 eventHandler ,会覆盖库内自带的 handler
例如,如果你RegisterCustomEventHandler("DANMU_MSG", ...
那么你使用OnDanmaku则不会再生效

func (c *Client) RegisterCustomEventHandler(cmd string, handler func(s string))
// 监听自定义事件
c.RegisterCustomEventHandler("STOP_LIVE_ROOM_LIST", func(s string) {
    data := gjson.Get(s, "data").String()
    fmt.Printf(data)
})

常见 CMD

注:来自blivedm

cmd = (
        'INTERACT_WORD', 'ROOM_BANNER', 'ROOM_REAL_TIME_MESSAGE_UPDATE', 'NOTICE_MSG', 'COMBO_SEND',
        'COMBO_END', 'ENTRY_EFFECT', 'WELCOME_GUARD', 'WELCOME', 'ROOM_RANK', 'ACTIVITY_BANNER_UPDATE_V2',
        'PANEL', 'SUPER_CHAT_MESSAGE_JPN', 'USER_TOAST_MSG', 'ROOM_BLOCK_MSG', 'LIVE', 'PREPARING',
        'room_admin_entrance', 'ROOM_ADMINS', 'ROOM_CHANGE'
    )

blivedm-go's People

Contributors

acexiamo avatar akegarasu avatar aynakeya avatar tymon42 avatar ww4396 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

blivedm-go's Issues

Bug: concurrent write to websocket connection

race condition.

panic: concurrent write to websocket connection

Two goroutines are simultaneously writing messages: one from heartBeatLoop and the other from wsLoop during reconnection.

trackback

panic: concurrent write to websocket connection

goroutine 179 [running]:
github.com/gorilla/websocket.(*messageWriter).flushFrame(0xc005ddec90, 0x1, {0x0?, 0x0?, 0x0?})
	C:/Users/vboxuser/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:617 +0x4b8
github.com/gorilla/websocket.(*messageWriter).Close(0xc0090494a0?)
	C:/Users/vboxuser/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:731 +0x35
github.com/gorilla/websocket.(*Conn).beginMessage(0xc00741e580, 0xc0090494a0, 0x2)
	C:/Users/vboxuser/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:480 +0x3a
github.com/gorilla/websocket.(*Conn).NextWriter(0xc00741e580, 0x2)
	C:/Users/vboxuser/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:520 +0x3f
github.com/gorilla/websocket.(*Conn).WriteMessage(0x20c2e75b?, 0x0?, {0xc00aed83c0, 0x125, 0x140})
	C:/Users/vboxuser/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:773 +0x137
github.com/AynaLivePlayer/blivedm-go/client.(*Client).sendEnterPacket(0xc0001b42c0)
	C:/Users/vboxuser/go/pkg/mod/github.com/!ayna!live!player/[email protected]/client/client.go:195 +0x55
github.com/AynaLivePlayer/blivedm-go/client.(*Client).connect(0xc0001b42c0)
	C:/Users/vboxuser/go/pkg/mod/github.com/!ayna!live!player/[email protected]/client/client.go:118 +0x2cf
github.com/AynaLivePlayer/blivedm-go/client.(*Client).wsLoop(0xc0001b42c0)
	C:/Users/vboxuser/go/pkg/mod/github.com/!ayna!live!player/[email protected]/client/client.go:137 +0x1d2
created by github.com/AynaLivePlayer/blivedm-go/client.(*Client).Start in goroutine 51
	C:/Users/vboxuser/go/pkg/mod/github.com/!ayna!live!player/[email protected]/client/client.go:174 +0x78

丢失弹幕

丢失弹幕的概率特别大。似乎这下需要真的挂个Cookie进去了。

`OnGuard` 命令可能不再可用于检测大航海。

OnGuard 事件检测的大航海事件中,舰长/提督/总督价格始终不变,虽然能正确反应上舰但无法正确反馈金额。
USER_TOAST_MSG 可以正确返回大航海的金额、时间、角色、Uid/Uname 字段。

{
    "cmd": "USER_TOAST_MSG",
    "data": {
        "anchor_show": true,
        "color": "#00D1F1",
        "dmscore": 90,
        "effect_id": 397,
        "end_time": 1662041450,
        "face_effect_id": 44,
        "gift_id": 10003,
        "guard_level": 3,
        "is_show": 0,
        "num": 1,
        "op_type": 3,
        "payflow_id": "2209012210331402119576078",
        "price": 138000,
        "role_name": "舰长",
        "room_effect_id": 590,
        "start_time": 1662041450,
        "svga_block": 0,
        "target_guard_count": 24,
        "toast_msg": "<%七星虞乐%> 自动续费了舰长",
        "uid": 30491957,
        "unit": "月",
        "user_show": true,
        "username": "七星虞乐"
    }
}

重复输出level=info msg=reconnect日志的bug

在跑demo的时候重复出现level=info msg=reconnect日志,进程一直不能正常工作,排查发现当websocket.DefaultDialer.Dial所作用的弹幕服务器在调用ReadMessage失败时,会触发reconnect,而reconnect并不会更换弹幕服务器,从而导致一直在reconnect不能正常工作
image

解决方案:retryCount改为全局变量

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.