mirror of
https://github.com/xfgryujk/blivedm.git
synced 2025-03-12 18:50:34 +08:00
方便自定义命令处理
This commit is contained in:
parent
79e7406e07
commit
3933226485
69
blivedm.py
69
blivedm.py
@ -4,7 +4,6 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import struct
|
import struct
|
||||||
import sys
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
# noinspection PyProtectedMember
|
# noinspection PyProtectedMember
|
||||||
@ -30,6 +29,38 @@ class BLiveClient:
|
|||||||
HEADER_STRUCT = struct.Struct('>I2H2I')
|
HEADER_STRUCT = struct.Struct('>I2H2I')
|
||||||
HeaderTuple = namedtuple('HeaderTuple', ('total_len', 'header_len', 'proto_ver', 'operation', 'sequence'))
|
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,
|
def __init__(self, room_id, ssl=True, loop=None, session: aiohttp.ClientSession=None,
|
||||||
uid=0):
|
uid=0):
|
||||||
"""
|
"""
|
||||||
@ -203,29 +234,10 @@ class BLiveClient:
|
|||||||
return
|
return
|
||||||
|
|
||||||
cmd = command['cmd']
|
cmd = command['cmd']
|
||||||
if cmd == 'DANMU_MSG': # 收到弹幕
|
if cmd in self._COMMAND_HANDLERS:
|
||||||
await self._on_get_danmaku(command['info'][1], command['info'][2][1])
|
handler = self._COMMAND_HANDLERS[cmd]
|
||||||
elif cmd in ( # 已知命令,本客户端只处理DANMU_MSG
|
if handler is not None:
|
||||||
# 从前端扒来的
|
await handler(self, command)
|
||||||
'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
|
|
||||||
else:
|
else:
|
||||||
logger.warning('未知命令:cmd=%s %s', cmd, command)
|
logger.warning('未知命令:cmd=%s %s', cmd, command)
|
||||||
|
|
||||||
@ -243,3 +255,12 @@ class BLiveClient:
|
|||||||
:param user_name: 弹幕作者
|
:param user_name: 弹幕作者
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def _on_gift(self, gift_name, gift_num, user_name):
|
||||||
|
"""
|
||||||
|
有人送礼
|
||||||
|
:param gift_name: 礼物名
|
||||||
|
:param gift_num: 礼物数
|
||||||
|
:param user_name: 送礼人
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
13
sample.py
13
sample.py
@ -6,12 +6,21 @@ from blivedm import BLiveClient
|
|||||||
|
|
||||||
|
|
||||||
class MyBLiveClient(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):
|
async def _on_get_popularity(self, popularity):
|
||||||
print('当前人气值:', popularity)
|
print(f'当前人气值:{popularity}')
|
||||||
|
|
||||||
async def _on_get_danmaku(self, content, user_name):
|
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():
|
async def async_main():
|
||||||
|
Loading…
Reference in New Issue
Block a user