From 2b04284ae31b0b0e25e5a252fd87d0db76c68802 Mon Sep 17 00:00:00 2001 From: TomatoPuddin Date: Sat, 8 Jul 2023 13:02:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8E=E5=BC=B9=E5=B9=95=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=9A=84=20dm=5Fv2=20=E5=AD=97=E6=AE=B5=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=A4=B4=E5=83=8F=20url=20(#30)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 从弹幕消息的 dm_v2 字段读取用户头像url * 移除调试代码 --- blivedm/handlers.py | 2 +- blivedm/models.py | 23 ++++++++++++++++++++++- blivedm/pb_models.py | 20 ++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 blivedm/pb_models.py diff --git a/blivedm/handlers.py b/blivedm/handlers.py index c4dd583..6d5cdce 100644 --- a/blivedm/handlers.py +++ b/blivedm/handlers.py @@ -61,7 +61,7 @@ class BaseHandler(HandlerInterface): return self._on_heartbeat(client, models.HeartbeatMessage.from_command(command['data'])) def __danmu_msg_callback(self, client: client_.BLiveClient, command: dict): - return self._on_danmaku(client, models.DanmakuMessage.from_command(command['info'])) + return self._on_danmaku(client, models.DanmakuMessage.from_command(command)) def __send_gift_callback(self, client: client_.BLiveClient, command: dict): return self._on_gift(client, models.GiftMessage.from_command(command['data'])) diff --git a/blivedm/models.py b/blivedm/models.py index 7bf372e..abfa149 100644 --- a/blivedm/models.py +++ b/blivedm/models.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- import dataclasses import json +import base64 from typing import * +from . import pb_models + __all__ = ( 'HeartbeatMessage', 'DanmakuMessage', @@ -67,6 +70,8 @@ class DanmakuMessage: """用户ID""" uname: str = None """用户名""" + face: str = None + """用户头像URL""" admin: int = None """是否房管""" vip: int = None @@ -109,7 +114,9 @@ class DanmakuMessage: """舰队类型,0非舰队,1总督,2提督,3舰长""" @classmethod - def from_command(cls, info: dict): + def from_command(cls, command: dict): + info = command['info'] + if len(info[3]) != 0: medal_level = info[3][0] medal_name = info[3][1] @@ -125,6 +132,19 @@ class DanmakuMessage: mcolor = 0 special_medal = 0 + try: + face = pb_models.DanmakuMessageV2.loads(base64.b64decode(command['dm_v2'])).user.face + except: + face = None + """ + 示例: + {'dm_v2': 'CiIxMmM2ZDg4MGQ5Yjc3Njc3NTI1NTA1NjFjYjY0YTJmYjU5EAEYGSDP2v8HKghkODU4ZWQ3MzIG5ZGc5ZGcOMHUk+WRMU + jl2cv+AWIAaAFydwoG5ZGc5ZGcEm0KE3Jvb21fMTA0MTMwNTFfMzY5NDkSSmh0dHBzOi8vaTAuaGRzbGIuY29tL2Jmcy9nYXJiLzMxYj + I4ZjRlZjQ0NmYxNmYzZDEyZGU3ZTYzMmFlNjBhMmE0NDAyZWIucG5nGAEgASgBMKIBOKIBigEAmgEQCghFRTQ5MzU1OBCd9oulBqIBpQ + EIwofiBBIP5biD5LiB55Wq6IyE6IyEIkpodHRwczovL2kwLmhkc2xiLmNvbS9iZnMvZmFjZS9mY2NjY2MxOTQ3N2M0YzYzMGE0MjMwMj + liYmViYjk1N2NkZGFkOWMyLmpwZziQTkABWiMIERIJ54ix6I2U5LidIKS6ngYwpLqeBjikup4GQKS6ngZQAWIPCBUQ3q3iAhoGPjUwMD + AwagByAHoCCB+qARoIt+vxwQQSDeiNlOaenVl1cmliaXUY+8f7BA=='} + """ return cls( mode=info[0][1], font_size=info[0][2], @@ -143,6 +163,7 @@ class DanmakuMessage: uid=info[2][0], uname=info[2][1], + face=face, admin=info[2][2], vip=info[2][3], svip=info[2][4], diff --git a/blivedm/pb_models.py b/blivedm/pb_models.py new file mode 100644 index 0000000..bec1ada --- /dev/null +++ b/blivedm/pb_models.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from dataclasses import dataclass, field +from typing_extensions import Annotated + +from pure_protobuf.annotations import Field +from pure_protobuf.message import BaseMessage + +__all__ = ( + 'DanmakuMessageV2', +) + + +@dataclass +class UserInfo(BaseMessage): + face: Annotated[str, Field(4)] = None + + +@dataclass +class DanmakuMessageV2(BaseMessage): + user: Annotated[UserInfo, Field(20)] = field(default_factory=UserInfo) diff --git a/requirements.txt b/requirements.txt index f6ec99b..a1be591 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ aiohttp==3.7.4 Brotli==1.0.9 +pure-protobuf==3.0.0a5