From 39332264854289271afa33d25e804da31b913072 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 23 Mar 2019 23:58:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B9=E4=BE=BF=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blivedm.py | 69 +++++++++++++++++++++++++++++++++++------------------- sample.py | 13 ++++++++-- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/blivedm.py b/blivedm.py index aa19294..25fd3e0 100644 --- a/blivedm.py +++ b/blivedm.py @@ -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 diff --git a/sample.py b/sample.py index 3f2836c..dbefea8 100644 --- a/sample.py +++ b/sample.py @@ -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():