简化各种代码

This commit is contained in:
John Smith 2019-02-20 14:53:50 +08:00
parent e0959dddfb
commit 79e7406e07

View File

@ -2,6 +2,7 @@
import asyncio
import json
import logging
import struct
import sys
from collections import namedtuple
@ -11,6 +12,8 @@ from ssl import _create_unverified_context
import aiohttp
logger = logging.getLogger(__name__)
class Operation(IntEnum):
SEND_HEARTBEAT = 2
@ -134,13 +137,13 @@ class BLiveClient:
if message.type == aiohttp.WSMsgType.BINARY:
await self._handle_message(message.data)
else:
print('未知的websocket消息', message.type, message.data)
logger.warning('未知的websocket消息type=%s %s', message.type, message.data)
except asyncio.CancelledError:
break
except aiohttp.ClientConnectorError:
# 重连
print('掉线重连中', file=sys.stderr)
logger.warning('掉线重连中')
try:
await asyncio.sleep(5)
except asyncio.CancelledError:
@ -189,7 +192,7 @@ class BLiveClient:
else:
body = message[offset + self.HEADER_STRUCT.size: offset + header.total_len]
print('未知包类型:', header, body, file=sys.stderr)
logger.warning('未知包类型operation=%d %s%s', header.operation, header, body)
offset += header.total_len
@ -200,34 +203,31 @@ class BLiveClient:
return
cmd = command['cmd']
# print(command)
if cmd == 'DANMU_MSG': # 收到弹幕
await self._on_get_danmaku(command['info'][1], command['info'][2][1])
elif cmd == 'SEND_GIFT': # 送礼物
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
elif cmd == 'WELCOME': # 欢迎
pass
elif cmd == 'WELCOME_GUARD': # 欢迎房管
pass
elif cmd == 'SYS_MSG': # 系统消息
pass
elif cmd == 'PREPARING': # 房主准备中
pass
elif cmd == 'LIVE': # 直播开始
pass
elif cmd == 'WISH_BOTTLE': # 许愿瓶?
pass
else:
print('未知命令:', command, file=sys.stderr)
logger.warning('未知命令cmd=%s %s', cmd, command)
async def _on_get_popularity(self, popularity):
"""
@ -243,10 +243,3 @@ class BLiveClient:
:param user_name: 弹幕作者
"""
pass
def _on_stop(self, exc):
"""
协程结束后被调用
:param exc: 如果是异常结束则为异常否则为None
"""
pass