Skip to content

Commit

Permalink
兼容B站弹幕升级
Browse files Browse the repository at this point in the history
  • Loading branch information
xfgryujk committed Jan 12, 2020
1 parent 4c2e216 commit e93f6b2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion blivedm
Submodule blivedm updated 2 files
+2 −2 blivedm.py
+2 −2 sample.py
57 changes: 57 additions & 0 deletions views/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,64 @@ async def _fetch_avatar_loop():
asyncio.ensure_future(_fetch_avatar_loop())


# 重新定义parse_XXX是为了减少对字段名的依赖,防止B站改字段名
def _parse_danmaku(client: blivedm.BLiveClient, command):
info = command['info']
if info[3]:
room_id = info[3][3]
medal_level = info[3][0]
else:
room_id = medal_level = 0
return client._on_receive_danmaku(blivedm.DanmakuMessage(
None, None, None, info[0][4], None, None, info[0][9], None,
info[1],
info[2][0], info[2][1], info[2][2], None, None, info[2][5], info[2][6], None,
medal_level, None, None, room_id, None, None,
info[4][0], None, None,
None, None,
info[7]
))


def _parse_gift(client: blivedm.BLiveClient, command):
data = command['data']
return client._on_receive_gift(blivedm.GiftMessage(
data['giftName'], data['num'], data['uname'], data['face'], None,
None, data['timestamp'], None, None,
None, None, None, data['coin_type'], data['total_coin']
))


def _parse_buy_guard(client: blivedm.BLiveClient, command):
data = command['data']
return client._on_buy_guard(blivedm.GuardBuyMessage(
data['uid'], data['username'], None, None, None,
None, None, data['start_time'], None
))


def _parse_super_chat(client: blivedm.BLiveClient, command):
data = command['data']
return client._on_super_chat(blivedm.SuperChatMessage(
data['price'], data['message'], None, data['start_time'],
None, None, data['id'], None,
None, None, data['user_info']['uname'],
data['user_info']['face'], None,
None, None,
None, None, None,
None
))


class Room(blivedm.BLiveClient):
_COMMAND_HANDLERS = {
**blivedm.BLiveClient._COMMAND_HANDLERS,
'DANMU_MSG': _parse_danmaku,
'SEND_GIFT': _parse_gift,
'GUARD_BUY': _parse_buy_guard,
'SUPER_CHAT_MESSAGE': _parse_super_chat
}

def __init__(self, room_id):
super().__init__(room_id, session=_http_session, heartbeat_interval=10)
self.clients: List['ChatHandler'] = []
Expand Down

0 comments on commit e93f6b2

Please sign in to comment.