diff --git a/README.md b/README.md index 5b84259..7941b6f 100644 --- a/README.md +++ b/README.md @@ -166,11 +166,13 @@ async def show(): - msg.py 为消息操作类代码 -- app.py +- example/app.py 以框架形式运行例子 -- multi_room.py + +- example/multi_room.py 同时监听多个直播间的实现 -- with_fastapi.py + +- example/with_fastapi.py 与fastapi 配合使用的例子 ## TODO diff --git a/app.py b/example/app.py similarity index 100% rename from app.py rename to example/app.py diff --git a/multi_room.py b/example/multi_room.py similarity index 100% rename from multi_room.py rename to example/multi_room.py diff --git a/with_fastapi.py b/example/with_fastapi.py similarity index 64% rename from with_fastapi.py rename to example/with_fastapi.py index e4c3287..313db15 100644 --- a/with_fastapi.py +++ b/example/with_fastapi.py @@ -1,11 +1,12 @@ from fastapi import FastAPI -from blive import BLiver,Events +from blive import BLiver, Events from blive.msg import DanMuMsg app = FastAPI() BLIVER_POOL = {} + def create_bliver(roomid): # 定义弹幕事件handler async def listen(ctx): @@ -13,26 +14,27 @@ def create_bliver(roomid): 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) + 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) +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":"创建一个新直播间弹幕监听成功"} + return {"msg": "创建一个新直播间弹幕监听成功"} @app.get("/del") -async def rm_bliver(roomid:int): - room = BLIVER_POOL.get(roomid,None) +async def rm_bliver(roomid: int): + room = BLIVER_POOL.get(roomid, None) if room: room.cancel() - return {"msg":"移除直播间弹幕监听成功"} + return {"msg": "移除直播间弹幕监听成功"} @app.get("/show")