From 88a09bb2176cb73df6d5f8e5f2fffc3ffcc97923 Mon Sep 17 00:00:00 2001 From: Cam Date: Tue, 1 Mar 2022 10:44:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=8F=E4=B8=AABLiver=E7=BB=B4=E6=8A=A4?= =?UTF-8?q?=E4=B8=80=E4=B8=AApackman=E8=80=8C=E4=B8=8D=E6=98=AF=E5=85=B1?= =?UTF-8?q?=E7=94=A8=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=A4=9A=E4=B8=AA=E7=9B=B4=E6=92=AD=E9=97=B4=E7=9A=84?= =?UTF-8?q?=E4=BE=8B=E5=AD=90=E4=BB=A3=E7=A0=81multi=5Froom.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 2 +- blive/core.py | 3 +-- blive/framework.py | 11 +++++++---- blive/msg.py | 1 - multi_room.py | 35 +++++++++++++++++++++++++++++++++++ setup.py | 2 +- 6 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 multi_room.py diff --git a/app.py b/app.py index b86ff1e..07ae543 100644 --- a/app.py +++ b/app.py @@ -37,7 +37,7 @@ async def listen_sc(ctx: BLiverCtx): msg = SuperChatMsg(ctx.body) print("sc 来了") print( - f"""\n感谢 {msg.sender['name']}({msg.sender['medal']['medal_name']}:{msg.sender['medal']['medal_level']})的价值{msg.price}的sc\n\n\t{msg.content}\n""" + f"\n感谢 {msg.sender['name']}({msg.sender['medal']['medal_name']}:{msg.sender['medal']['medal_level']})的价值 {msg.price} 的sc\n\n\t{msg.content}\n" ) diff --git a/blive/core.py b/blive/core.py index 4373351..d85d7dd 100644 --- a/blive/core.py +++ b/blive/core.py @@ -1,8 +1,7 @@ import asyncio from collections import namedtuple -from multiprocessing import RawValue, Lock import json -from random import randint, random +from random import randint import requests import struct import enum diff --git a/blive/framework.py b/blive/framework.py index 491d8c8..908d1ec 100644 --- a/blive/framework.py +++ b/blive/framework.py @@ -11,8 +11,8 @@ from apscheduler.util import _Undefined from requests.exceptions import ConnectionError from .core import ( + BWS_MsgPackage, PackageHeader, - packman, Events, Operation, get_blive_room_info, @@ -84,6 +84,7 @@ class BLiver: else: self.logger = logger self._ws: ClientWebSocketResponse = None + self.packman = BWS_MsgPackage() self.scheduler = AsyncIOScheduler(timezone="Asia/ShangHai") self.processor = Processor(logger=self.logger) self.aio_session = aiohttp.ClientSession() @@ -149,7 +150,7 @@ class BLiver: try: if self._ws is not None and not self._ws.closed: await self._ws.send_bytes( - packman.pack(heartbeat(), Operation.HEARTBEAT) + self.packman.pack(heartbeat(), Operation.HEARTBEAT) ) self.logger.debug("heartbeat sended") return @@ -173,7 +174,9 @@ class BLiver: self._ws = ws # 发送认证 await ws.send_bytes( - packman.pack(certification(self.real_roomid, token), Operation.AUTH) + self.packman.pack( + certification(self.real_roomid, token), Operation.AUTH + ) ) return except ( @@ -209,7 +212,7 @@ class BLiver: continue if msg.type != aiohttp.WSMsgType.BINARY: continue - mq = packman.unpack(msg.data) + mq = self.packman.unpack(msg.data) self.logger.debug("received msg:\n{}", mq) tasks = [self.processor.process(BLiverCtx(self, m)) for m in mq] await asyncio.gather(*tasks) diff --git a/blive/msg.py b/blive/msg.py index c4741d8..4112c02 100644 --- a/blive/msg.py +++ b/blive/msg.py @@ -1,6 +1,5 @@ from abc import ABC import json -from re import L from typing import List """ diff --git a/multi_room.py b/multi_room.py new file mode 100644 index 0000000..d936e39 --- /dev/null +++ b/multi_room.py @@ -0,0 +1,35 @@ +"""监听多个直播间的例子""" + +import asyncio +from blive import BLiver, Events, BLiverCtx +from blive.msg import DanMuMsg + + +# 定义弹幕事件handler +async def listen(ctx: BLiverCtx): + danmu = DanMuMsg(ctx.body) + print( + f'\n{danmu.sender.name} ({danmu.sender.medal.medal_name}:{danmu.sender.medal.medal_level}): "{danmu.content}"\n' + ) + + +async def main(): + # 两个直播间 + ke = BLiver(605) + azi = BLiver(510) + + # 注册handler + ke.register_handler(Events.DANMU_MSG, listen) + azi.register_handler(Events.DANMU_MSG, listen) + + # 以异步task的形式运行 + task1 = ke.run_as_task() + task2 = azi.run_as_task() + + # await 两个任务 + await asyncio.gather(*[task1, task2]) + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/setup.py b/setup.py index c14dd15..5c334e4 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("./README.md", "r") as f: setuptools.setup( name="blive", - version="0.0.3", + version="0.0.4", author="cam", author_email="yulinfeng000@gmail.com", long_description=description,