Skip to content

Commit

Permalink
从弹幕消息的 dm_v2 字段读取用户头像 url (#30)
Browse files Browse the repository at this point in the history
* 从弹幕消息的 dm_v2 字段读取用户头像url

* 移除调试代码
  • Loading branch information
TomatoPuddin committed Jul 8, 2023
1 parent 9d92fcd commit 2b04284
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion blivedm/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __heartbeat_callback(self, client: client_.BLiveClient, command: dict):
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']))
Expand Down
23 changes: 22 additions & 1 deletion blivedm/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
import dataclasses
import json
import base64
from typing import *

from . import pb_models

__all__ = (
'HeartbeatMessage',
'DanmakuMessage',
Expand Down Expand Up @@ -67,6 +70,8 @@ class DanmakuMessage:
"""用户ID"""
uname: str = None
"""用户名"""
face: str = None
"""用户头像URL"""
admin: int = None
"""是否房管"""
vip: int = None
Expand Down Expand Up @@ -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]
Expand All @@ -125,6 +132,19 @@ def from_command(cls, info: dict):
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],
Expand All @@ -143,6 +163,7 @@ def from_command(cls, info: dict):

uid=info[2][0],
uname=info[2][1],
face=face,
admin=info[2][2],
vip=info[2][3],
svip=info[2][4],
Expand Down
20 changes: 20 additions & 0 deletions blivedm/pb_models.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
aiohttp==3.7.4
Brotli==1.0.9
pure-protobuf==3.0.0a5

0 comments on commit 2b04284

Please sign in to comment.