方便自定义命令处理

This commit is contained in:
John Smith 2019-03-23 23:58:02 +08:00
parent 79e7406e07
commit 3933226485
2 changed files with 56 additions and 26 deletions

View File

@ -4,7 +4,6 @@ import asyncio
import json
import logging
import struct
import sys
from collections import namedtuple
from enum import IntEnum
# noinspection PyProtectedMember
@ -30,6 +29,38 @@ class BLiveClient:
HEADER_STRUCT = struct.Struct('>I2H2I')
HeaderTuple = namedtuple('HeaderTuple', ('total_len', 'header_len', 'proto_ver', 'operation', 'sequence'))
_COMMAND_HANDLERS = {
# 收到弹幕
'DANMU_MSG': lambda client, command: client._on_get_danmaku(
command['info'][1], command['info'][2][1]
),
# 有人送礼
'SEND_GIFT': lambda client, command: client._on_gift(
command['data']['giftName'], command['data']['num'], command['data']['uname']
)
}
for cmd in ( # 其他已知命令
# 从前端扒来的
'66FFFF', 'SYS_MSG', 'SYS_GIFT', 'GUARD_MSG', 'LIVE', 'PREPARING',
'END', 'CLOSE', 'BLOCK', 'ROUND', 'WELCOME', 'REFRESH',
'ACTIVITY_RED_PACKET', 'ROOM_LIMIT', 'PK_PRE', 'PK_END', 'PK_SETTLE',
'PK_MIC_END', 'live', 'preparing', 'end', 'close', 'block', 'pre-round',
'round', 'error', 'player-state-play', 'player-state-pause', 'http:',
'https:', 'ws:', 'wss:', 'videoVolume', 'homeVideoVolume', 'div',
'canvas', 'initialized', 'playerStateChange', 'liveStateChange',
'videoStateChange', 'fullscreenChange', 'playing', 'paused', 'switchLine',
'switchQuality', 'webFullscreen', 'feedBackClick', 'blockSettingClick',
'set', 'initDanmaku', 'addDanmaku', 'sendDanmaku', 'receiveOnlineCount',
'receiveMessage', 'userLogin', 'giftPackageClick', 'sendGift', 'guidChange',
'reload', 'danmaku', 'block', 'gift', 'firstLoadedAPIPlayer',
'firstLoadedAPIPlayurl', 'firstLoadStart', 'firstLoadedMetaData',
'firstPlaying', 'enterTheRoom', 'operableElementsChange',
# 其他遇到的
'COMBO_SEND', 'COMBO_END', 'ROOM_RANK', 'NOTICE_MSG', 'WELCOME_GUARD',
'WISH_BOTTLE', 'RAFFLE_START', 'ENTRY_EFFECT', 'ROOM_REAL_TIME_MESSAGE_UPDATE'
):
_COMMAND_HANDLERS[cmd] = None
def __init__(self, room_id, ssl=True, loop=None, session: aiohttp.ClientSession=None,
uid=0):
"""
@ -203,29 +234,10 @@ class BLiveClient:
return
cmd = command['cmd']
if cmd == 'DANMU_MSG': # 收到弹幕
await self._on_get_danmaku(command['info'][1], command['info'][2][1])
elif cmd in ( # 已知命令本客户端只处理DANMU_MSG
# 从前端扒来的
'66FFFF', 'SYS_MSG', 'SYS_GIFT', 'GUARD_MSG', 'SEND_GIFT', 'LIVE',
'PREPARING', 'END', 'CLOSE', 'BLOCK', 'ROUND', 'WELCOME', 'REFRESH',
'ACTIVITY_RED_PACKET', 'ROOM_LIMIT', 'PK_PRE', 'PK_END', 'PK_SETTLE',
'PK_MIC_END', 'live', 'preparing', 'end', 'close', 'block', 'pre-round',
'round', 'error', 'player-state-play', 'player-state-pause', 'http:',
'https:', 'ws:', 'wss:', 'videoVolume', 'homeVideoVolume', 'div',
'canvas', 'initialized', 'playerStateChange', 'liveStateChange',
'videoStateChange', 'fullscreenChange', 'playing', 'paused', 'switchLine',
'switchQuality', 'webFullscreen', 'feedBackClick', 'blockSettingClick',
'set', 'initDanmaku', 'addDanmaku', 'sendDanmaku', 'receiveOnlineCount',
'receiveMessage', 'userLogin', 'giftPackageClick', 'sendGift', 'guidChange',
'reload', 'danmaku', 'block', 'gift', 'firstLoadedAPIPlayer',
'firstLoadedAPIPlayurl', 'firstLoadStart', 'firstLoadedMetaData',
'firstPlaying', 'enterTheRoom', 'operableElementsChange',
# 其他遇到的
'COMBO_SEND', 'COMBO_END', 'ROOM_RANK', 'NOTICE_MSG', 'WELCOME_GUARD',
'WISH_BOTTLE', 'RAFFLE_START', 'ENTRY_EFFECT'
):
pass
if cmd in self._COMMAND_HANDLERS:
handler = self._COMMAND_HANDLERS[cmd]
if handler is not None:
await handler(self, command)
else:
logger.warning('未知命令cmd=%s %s', cmd, command)
@ -243,3 +255,12 @@ class BLiveClient:
:param user_name: 弹幕作者
"""
pass
async def _on_gift(self, gift_name, gift_num, user_name):
"""
有人送礼
:param gift_name: 礼物名
:param gift_num: 礼物数
:param user_name: 送礼人
"""
pass

View File

@ -6,12 +6,21 @@ from blivedm import BLiveClient
class MyBLiveClient(BLiveClient):
# 演示如何自定义handler
_COMMAND_HANDLERS = BLiveClient._COMMAND_HANDLERS.copy()
_COMMAND_HANDLERS['SEND_GIFT'] = lambda client, command: client._my_on_gift(
command['data']['giftName'], command['data']['num'], command['data']['uname'],
command['data']['coin_type'], command['data']['total_coin']
)
async def _on_get_popularity(self, popularity):
print('当前人气值:', popularity)
print(f'当前人气值:{popularity}')
async def _on_get_danmaku(self, content, user_name):
print(user_name, '说:', content)
print(f'{user_name}{content}')
async def _my_on_gift(self, gift_name, gift_num, user_name, coin_type, total_coin):
print(f'{user_name} 赠送{gift_name}x{gift_num} {coin_type}币x{total_coin}')
async def async_main():