从弹幕消息的 dm_v2 字段读取用户头像 url (#30)

* 从弹幕消息的 dm_v2 字段读取用户头像url

* 移除调试代码
This commit is contained in:
TomatoPuddin 2023-07-08 13:02:09 +08:00 committed by GitHub
parent 9d92fcdc91
commit 2b04284ae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 2 deletions

View File

@ -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']))

View File

@ -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],

20
blivedm/pb_models.py Normal file
View File

@ -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)

View File

@ -1,2 +1,3 @@
aiohttp==3.7.4
Brotli==1.0.9
pure-protobuf==3.0.0a5