blive/README.md

71 lines
1.3 KiB
Markdown
Raw Normal View History

2022-01-06 00:50:50 +08:00
# B 站弹幕监听框架
## 特点
- 简单,只需房间号即可监听
- 异步io 不阻塞,及时获取消息
2022-01-06 19:41:42 +08:00
## B 站直播弹幕 websocket 协议分析
[PROTOCOL 分析](./PROTOCOL.md)
2022-01-06 00:50:50 +08:00
## 快速开始
2022-01-09 11:19:51 +08:00
1. 安装
2022-01-06 00:50:50 +08:00
2022-01-09 11:19:51 +08:00
`pip install blive`
2. 创建 app
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
```python
from blive import BLiver
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
app = BLiver(123) #123为房间号
```
2022-01-06 00:50:50 +08:00
2022-01-09 11:19:51 +08:00
3. 创建处理器
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
```python
from blive import BLiver, Events, BLiverCtx
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
app = BLiver(123)
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
# 标记该方法监听弹幕消息,更多消息类型请参考 Events 类源代码
@app.on(Events.DANMU_MSG)
async def listen_danmu(ctx: BLiverCtx):
danmu = DanMuMsg(ctx.body) #ctx.body 套上相应的消息操作类即可得到消息的基本内容,也可直接操作 ctx.body
print(danmu.content)
print(danmu.sender)
print(danmu.timestamp)
```
2022-01-06 00:50:50 +08:00
2022-01-09 11:19:51 +08:00
4. 运行
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
```python
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
from blive import BLiver, Events, BLiverCtx
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
app = BLiver(123)
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
@app.on(Events.DANMU_MSG)
async def listen_danmu(ctx: BLiverCtx):
danmu = DanMuMsg(ctx.body)
print(danmu.content)
print(danmu.sender)
print(danmu.timestamp)
2022-01-06 00:50:50 +08:00
2022-01-08 13:03:48 +08:00
app.run() # 运行app!
```
2022-01-06 00:50:50 +08:00
## 项目简介
- blive 文件夹为框架代码
- app.py 为一个简单示例
## TODO
- 打包发布
- 更多的消息操作类
- 尝试加入中间件架构