mk example dir

This commit is contained in:
Cam 2022-04-26 13:01:01 +08:00
parent bbb673b640
commit ee135a8648
4 changed files with 15 additions and 11 deletions

View File

@ -166,11 +166,13 @@ async def show():
- msg.py 为消息操作类代码 - msg.py 为消息操作类代码
- app.py - example/app.py
以框架形式运行例子 以框架形式运行例子
- multi_room.py
- example/multi_room.py
同时监听多个直播间的实现 同时监听多个直播间的实现
- with_fastapi.py
- example/with_fastapi.py
与fastapi 配合使用的例子 与fastapi 配合使用的例子
## TODO ## TODO

View File

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