update README,add new example: with_fastapi and add project dependence in setup.py

This commit is contained in:
Cam 2022-03-09 10:12:23 +08:00
parent b5c15993bd
commit bbb673b640
3 changed files with 95 additions and 2 deletions

View File

@ -109,6 +109,53 @@ while True:
print(msg)
```
## 与 fastapi (其他asyncio生态框架) 配合使用
```python
from fastapi import FastAPI
from blive import BLiver,Events
from blive.msg import DanMuMsg
app = FastAPI()
BLIVER_POOL = {}
# 定义弹幕事件handler
async def handler(ctx):
danmu = DanMuMsg(ctx.body)
print(
f'\n{danmu.sender.name} ({danmu.sender.medal.medal_name}:{danmu.sender.medal.medal_level}): "{danmu.content}"\n'
)
def create_bliver(roomid):
b = BLiver(roomid)
b.register_handler(Events.DANMU_MSG,handler)
return b
@app.get("/create")
async def create_new_bliver(roomid:int):
room = BLIVER_POOL.get(roomid,None)
if not room:
b = create_bliver(roomid)
BLIVER_POOL[roomid] = b.run_as_task() # 启动监听
return {"msg":"创建一个新直播间弹幕监听成功"}
@app.get("/del")
async def rm_bliver(roomid:int):
room = BLIVER_POOL.get(roomid,None)
if room:
room.cancel()
return {"msg":"移除直播间弹幕监听成功"}
@app.get("/show")
async def show():
return list(BLIVER_POOL.keys())
```
## 项目简介
- blive 文件夹为框架代码
@ -119,7 +166,12 @@ while True:
- msg.py 为消息操作类代码
- app.py , multi_room.py 为2个简单示例分别是以框架运行和同时监听多个直播间的实现
- app.py
以框架形式运行例子
- multi_room.py
同时监听多个直播间的实现
- with_fastapi.py
与fastapi 配合使用的例子
## TODO

View File

@ -5,7 +5,7 @@ with open("./README.md", "r") as f:
setuptools.setup(
name="blive",
version="0.0.4",
version="0.0.5",
author="cam",
author_email="yulinfeng000@gmail.com",
long_description=description,
@ -16,4 +16,5 @@ setuptools.setup(
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
],
install_requires=["aiohttp","loguru","requests","APScheduler","brotli"]
)

40
with_fastapi.py Normal file
View File

@ -0,0 +1,40 @@
from fastapi import FastAPI
from blive import BLiver,Events
from blive.msg import DanMuMsg
app = FastAPI()
BLIVER_POOL = {}
def create_bliver(roomid):
# 定义弹幕事件handler
async def listen(ctx):
danmu = DanMuMsg(ctx.body)
print(
f'\n{danmu.sender.name} ({danmu.sender.medal.medal_name}:{danmu.sender.medal.medal_level}): "{danmu.content}"\n'
)
b = BLiver(roomid)
b.register_handler(Events.DANMU_MSG,listen)
return b
@app.get("/create")
async def create_new_bliver(roomid:int):
room = BLIVER_POOL.get(roomid,None)
if not room:
b = create_bliver(roomid)
BLIVER_POOL[roomid] = b.run_as_task()
return {"msg":"创建一个新直播间弹幕监听成功"}
@app.get("/del")
async def rm_bliver(roomid:int):
room = BLIVER_POOL.get(roomid,None)
if room:
room.cancel()
return {"msg":"移除直播间弹幕监听成功"}
@app.get("/show")
async def show():
return list(BLIVER_POOL.keys())