mirror of
https://github.com/xfgryujk/blivedm.git
synced 2024-12-26 12:50:13 +08:00
修改例程,添加同时监听多个直播间
This commit is contained in:
parent
28dfc12a90
commit
7ac4966c92
@ -216,10 +216,11 @@ class BLiveClient:
|
|||||||
|
|
||||||
async def stop_and_close(self):
|
async def stop_and_close(self):
|
||||||
"""
|
"""
|
||||||
停止本客户端并释放本客户端的资源,调用后本客户端将不可用
|
便利函数,停止本客户端并释放本客户端的资源,调用后本客户端将不可用
|
||||||
"""
|
"""
|
||||||
self.stop()
|
if self.is_running:
|
||||||
await self.join()
|
self.stop()
|
||||||
|
await self.join()
|
||||||
await self.close()
|
await self.close()
|
||||||
|
|
||||||
async def join(self):
|
async def join(self):
|
||||||
@ -230,7 +231,7 @@ class BLiveClient:
|
|||||||
logger.warning('room=%s client is stopped, cannot join()', self.room_id)
|
logger.warning('room=%s client is stopped, cannot join()', self.room_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
await self._network_future
|
await asyncio.shield(self._network_future)
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""
|
"""
|
||||||
@ -492,7 +493,7 @@ class BLiveClient:
|
|||||||
# 业务消息,可能有多个包一起发,需要分包
|
# 业务消息,可能有多个包一起发,需要分包
|
||||||
while True:
|
while True:
|
||||||
body = data[offset + header.raw_header_size: offset + header.pack_len]
|
body = data[offset + header.raw_header_size: offset + header.pack_len]
|
||||||
await self.__parse_business_message(header, body)
|
await self._parse_business_message(header, body)
|
||||||
|
|
||||||
offset += header.pack_len
|
offset += header.pack_len
|
||||||
if offset >= len(data):
|
if offset >= len(data):
|
||||||
@ -524,7 +525,7 @@ class BLiveClient:
|
|||||||
logger.warning('room=%d unknown message operation=%d, header=%s, body=%s', self.room_id,
|
logger.warning('room=%d unknown message operation=%d, header=%s, body=%s', self.room_id,
|
||||||
header.operation, header, body)
|
header.operation, header, body)
|
||||||
|
|
||||||
async def __parse_business_message(self, header: HeaderTuple, body: bytes):
|
async def _parse_business_message(self, header: HeaderTuple, body: bytes):
|
||||||
"""
|
"""
|
||||||
解析业务消息
|
解析业务消息
|
||||||
"""
|
"""
|
||||||
|
77
sample.py
77
sample.py
@ -1,51 +1,90 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import random
|
||||||
|
|
||||||
import blivedm
|
import blivedm
|
||||||
|
|
||||||
|
# 直播间ID的取值看直播间URL
|
||||||
|
TEST_ROOM_IDS = [
|
||||||
|
12235923,
|
||||||
|
14327465,
|
||||||
|
21396545,
|
||||||
|
21449083,
|
||||||
|
23105590,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
# 直播间ID的取值看直播间URL
|
await run_single_client()
|
||||||
|
await run_multi_client()
|
||||||
|
|
||||||
|
|
||||||
|
async def run_single_client():
|
||||||
|
"""
|
||||||
|
演示监听一个直播间
|
||||||
|
"""
|
||||||
|
room_id = random.choice(TEST_ROOM_IDS)
|
||||||
# 如果SSL验证失败就把ssl设为False,B站真的有过忘续证书的情况
|
# 如果SSL验证失败就把ssl设为False,B站真的有过忘续证书的情况
|
||||||
client = blivedm.BLiveClient(room_id=21449083, ssl=True)
|
client = blivedm.BLiveClient(room_id, ssl=True)
|
||||||
handler = MyHandler()
|
handler = MyHandler()
|
||||||
client.add_handler(handler)
|
client.add_handler(handler)
|
||||||
|
|
||||||
client.start()
|
client.start()
|
||||||
try:
|
try:
|
||||||
# 5秒后停止,测试用
|
# 演示5秒后停止
|
||||||
# await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
# client.stop()
|
client.stop()
|
||||||
|
|
||||||
await client.join()
|
await client.join()
|
||||||
finally:
|
finally:
|
||||||
await client.close()
|
await client.stop_and_close()
|
||||||
|
|
||||||
|
|
||||||
|
async def run_multi_client():
|
||||||
|
"""
|
||||||
|
演示同时监听多个直播间
|
||||||
|
"""
|
||||||
|
clients = [blivedm.BLiveClient(room_id) for room_id in TEST_ROOM_IDS]
|
||||||
|
handler = MyHandler()
|
||||||
|
for client in clients:
|
||||||
|
client.add_handler(handler)
|
||||||
|
client.start()
|
||||||
|
|
||||||
|
try:
|
||||||
|
await asyncio.gather(*(
|
||||||
|
client.join() for client in clients
|
||||||
|
))
|
||||||
|
finally:
|
||||||
|
await asyncio.gather(*(
|
||||||
|
client.stop_and_close() for client in clients
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
class MyHandler(blivedm.BaseHandler):
|
class MyHandler(blivedm.BaseHandler):
|
||||||
# 演示如何添加自定义回调
|
# # 演示如何添加自定义回调
|
||||||
_CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
|
# _CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
|
||||||
|
#
|
||||||
# 入场消息回调
|
# # 入场消息回调
|
||||||
async def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
|
# async def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
|
||||||
print(f"INTERACT_WORD: self_type={type(self).__name__}, room_id={client.room_id},"
|
# print(f"[{client.room_id}] INTERACT_WORD: self_type={type(self).__name__}, room_id={client.room_id},"
|
||||||
f" uname={command['data']['uname']}")
|
# f" uname={command['data']['uname']}")
|
||||||
_CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback # noqa
|
# _CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback # noqa
|
||||||
|
|
||||||
async def _on_heartbeat(self, client: blivedm.BLiveClient, message: blivedm.HeartbeatMessage):
|
async def _on_heartbeat(self, client: blivedm.BLiveClient, message: blivedm.HeartbeatMessage):
|
||||||
print(f'当前人气值:{message.popularity}')
|
print(f'[{client.room_id}] 当前人气值:{message.popularity}')
|
||||||
|
|
||||||
async def _on_danmaku(self, client: blivedm.BLiveClient, message: blivedm.DanmakuMessage):
|
async def _on_danmaku(self, client: blivedm.BLiveClient, message: blivedm.DanmakuMessage):
|
||||||
print(f'{message.uname}:{message.msg}')
|
print(f'[{client.room_id}] {message.uname}:{message.msg}')
|
||||||
|
|
||||||
async def _on_gift(self, client: blivedm.BLiveClient, message: blivedm.GiftMessage):
|
async def _on_gift(self, client: blivedm.BLiveClient, message: blivedm.GiftMessage):
|
||||||
print(f'{message.uname} 赠送{message.gift_name}x{message.num} ({message.coin_type}币x{message.total_coin})')
|
print(f'[{client.room_id}] {message.uname} 赠送{message.gift_name}x{message.num}'
|
||||||
|
f' ({message.coin_type}瓜子x{message.total_coin})')
|
||||||
|
|
||||||
async def _on_buy_guard(self, client: blivedm.BLiveClient, message: blivedm.GuardBuyMessage):
|
async def _on_buy_guard(self, client: blivedm.BLiveClient, message: blivedm.GuardBuyMessage):
|
||||||
print(f'{message.username} 购买{message.gift_name}')
|
print(f'[{client.room_id}] {message.username} 购买{message.gift_name}')
|
||||||
|
|
||||||
async def _on_super_chat(self, client: blivedm.BLiveClient, message: blivedm.SuperChatMessage):
|
async def _on_super_chat(self, client: blivedm.BLiveClient, message: blivedm.SuperChatMessage):
|
||||||
print(f'醒目留言 ¥{message.price} {message.uname}:{message.message}')
|
print(f'[{client.room_id}] 醒目留言 ¥{message.price} {message.uname}:{message.message}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user