每个BLiver维护一个packman而不是共用,增加一个监听多个直播间的例子代码multi_room.py

This commit is contained in:
Cam 2022-03-01 10:44:38 +08:00
parent 6d9592a7fa
commit 88a09bb217
6 changed files with 45 additions and 9 deletions

2
app.py
View File

@ -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"
)

View File

@ -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

View File

@ -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)

View File

@ -1,6 +1,5 @@
from abc import ABC
import json
from re import L
from typing import List
"""

35
multi_room.py Normal file
View File

@ -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())

View File

@ -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,