From 6b3ca05d513de588c168e006de5bc0e61f9a378e Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 25 Nov 2023 16:08:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=BC=B9=E5=B9=95=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E4=B8=AD=E7=9A=84=E5=A4=B4=E5=83=8F=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=92=8CProtobuf=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blivedm/handlers.py | 4 +- blivedm/models/pb.py | 194 ------------------------------------------ blivedm/models/web.py | 20 +---- requirements.txt | 1 - 4 files changed, 2 insertions(+), 217 deletions(-) delete mode 100644 blivedm/models/pb.py diff --git a/blivedm/handlers.py b/blivedm/handlers.py index 9d33b70..76caca6 100644 --- a/blivedm/handlers.py +++ b/blivedm/handlers.py @@ -67,9 +67,7 @@ class BaseHandler(HandlerInterface): """ def __danmu_msg_callback(self, client: ws_base.WebSocketClientBase, command: dict): - return self._on_danmaku( - client, web_models.DanmakuMessage.from_command(command['info'], command.get('dm_v2', '')) - ) + return self._on_danmaku(client, web_models.DanmakuMessage.from_command(command['info'])) _CMD_CALLBACK_DICT: Dict[ str, diff --git a/blivedm/models/pb.py b/blivedm/models/pb.py deleted file mode 100644 index 5e9f552..0000000 --- a/blivedm/models/pb.py +++ /dev/null @@ -1,194 +0,0 @@ -# -*- coding: utf-8 -*- -import dataclasses -import enum -from typing import * - -import pure_protobuf.annotations as pb_anno -import pure_protobuf.message as pb_msg - -try: - Annotated -except NameError: - from typing_extensions import Annotated # Python < 3.9 - - -@dataclasses.dataclass -class SimpleUser(pb_msg.BaseMessage): - face: Annotated[str, pb_anno.Field(4)] = '' - - -@dataclasses.dataclass -class SimpleDm(pb_msg.BaseMessage): - user: Annotated[SimpleUser, pb_anno.Field(20)] = dataclasses.field(default_factory=SimpleUser) - - -# -# 以下代码是预防以后全量使用Protobuf协议 -# - -class BizScene(enum.IntEnum): - None_ = 0 - Lottery = 1 - Survive = 2 - VoiceConn = 3 - PlayBack = 4 - Vote = 5 - - -@dataclasses.dataclass -class Bubble(pb_msg.BaseMessage): - id: Annotated[int, pb_anno.Field(1)] = 0 - color: Annotated[str, pb_anno.Field(2)] = '' - id_v2: Annotated[int, pb_anno.Field(3)] = 0 - - -class DmType(enum.IntEnum): - Normal = 0 - Emoticon = 1 - Voice = 2 - - -@dataclasses.dataclass -class Emoticon(pb_msg.BaseMessage): - unique: Annotated[str, pb_anno.Field(1)] = '' - url: Annotated[str, pb_anno.Field(2)] = '' - is_dynamic: Annotated[bool, pb_anno.Field(3)] = False - in_player_area: Annotated[int, pb_anno.Field(4)] = 0 - bulge_display: Annotated[int, pb_anno.Field(5)] = 0 - height: Annotated[int, pb_anno.Field(6)] = 0 - width: Annotated[int, pb_anno.Field(7)] = 0 - - -# pure_protobuf不支持map的临时解决方案 -@dataclasses.dataclass -class EmoticonMapEntry(pb_msg.BaseMessage): - key: Annotated[str, pb_anno.Field(1)] = '' - value: Annotated[Emoticon, pb_anno.Field(2)] = dataclasses.field(default_factory=Emoticon) - - -@dataclasses.dataclass -class Voice(pb_msg.BaseMessage): - url: Annotated[str, pb_anno.Field(1)] = '' - file_format: Annotated[str, pb_anno.Field(2)] = '' - text: Annotated[str, pb_anno.Field(3)] = '' - file_duration: Annotated[int, pb_anno.Field(4)] = 0 - file_id: Annotated[str, pb_anno.Field(5)] = '' - - -@dataclasses.dataclass -class Aggregation(pb_msg.BaseMessage): - is_aggregation: Annotated[bool, pb_anno.Field(1)] = False - activity_source: Annotated[int, pb_anno.Field(2)] = 0 - activity_identity: Annotated[str, pb_anno.Field(3)] = '' - not_show: Annotated[int, pb_anno.Field(4)] = 0 - - -@dataclasses.dataclass -class Check(pb_msg.BaseMessage): - token: Annotated[str, pb_anno.Field(1)] = '' - ts: Annotated[int, pb_anno.Field(2)] = 0 - - -@dataclasses.dataclass -class Medal(pb_msg.BaseMessage): - level: Annotated[int, pb_anno.Field(1)] = 0 - name: Annotated[str, pb_anno.Field(2)] = '' - special: Annotated[str, pb_anno.Field(3)] = '' - color: Annotated[int, pb_anno.Field(4)] = 0 - icon_id: Annotated[int, pb_anno.Field(5)] = 0 - border_color: Annotated[int, pb_anno.Field(6)] = 0 - gradient_start_color: Annotated[int, pb_anno.Field(7)] = 0 - gradient_end_color: Annotated[int, pb_anno.Field(8)] = 0 - privilege: Annotated[int, pb_anno.Field(9)] = 0 - light: Annotated[int, pb_anno.Field(10)] = 0 - - -@dataclasses.dataclass -class UserLevel(pb_msg.BaseMessage): - level: Annotated[int, pb_anno.Field(1)] = 0 - color: Annotated[int, pb_anno.Field(2)] = 0 - rank: Annotated[str, pb_anno.Field(3)] = '' - online_rank: Annotated[int, pb_anno.Field(4)] = 0 - - -@dataclasses.dataclass -class Title(pb_msg.BaseMessage): - title: Annotated[str, pb_anno.Field(1)] = '' - old_title: Annotated[str, pb_anno.Field(2)] = '' - - -@dataclasses.dataclass -class Identify(pb_msg.BaseMessage): - beginning_url: Annotated[str, pb_anno.Field(1)] = '' - ending_url: Annotated[str, pb_anno.Field(2)] = '' - jump_to_url: Annotated[str, pb_anno.Field(3)] = '' - - -@dataclasses.dataclass -class Wealth(pb_msg.BaseMessage): - level: Annotated[int, pb_anno.Field(1)] = 0 - - -@dataclasses.dataclass -class User(pb_msg.BaseMessage): - uid: Annotated[int, pb_anno.Field(1)] = 0 - name: Annotated[str, pb_anno.Field(2)] = '' - name_color: Annotated[str, pb_anno.Field(3)] = '' - face: Annotated[str, pb_anno.Field(4)] = '' - vip: Annotated[int, pb_anno.Field(5)] = 0 - svip: Annotated[int, pb_anno.Field(6)] = 0 - rank: Annotated[int, pb_anno.Field(7)] = 0 - mobile_verify: Annotated[int, pb_anno.Field(8)] = 0 - lpl_status: Annotated[int, pb_anno.Field(9)] = 0 - attr: Annotated[int, pb_anno.Field(10)] = 0 - medal: Annotated[Medal, pb_anno.Field(11)] = dataclasses.field(default_factory=Medal) - level: Annotated[UserLevel, pb_anno.Field(12)] = dataclasses.field(default_factory=UserLevel) - title: Annotated[Title, pb_anno.Field(13)] = dataclasses.field(default_factory=Title) - identify: Annotated[Identify, pb_anno.Field(14)] = dataclasses.field(default_factory=Identify) - wealth: Annotated[Wealth, pb_anno.Field(15)] = dataclasses.field(default_factory=Wealth) - - -@dataclasses.dataclass -class Room(pb_msg.BaseMessage): - uid: Annotated[int, pb_anno.Field(1)] = 0 - name: Annotated[str, pb_anno.Field(2)] = '' - - -@dataclasses.dataclass -class Prefix(pb_msg.BaseMessage): - type: Annotated[int, pb_anno.Field(1)] = 0 - resource: Annotated[str, pb_anno.Field(2)] = '' - - -@dataclasses.dataclass -class Icon(pb_msg.BaseMessage): - prefix: Annotated[Prefix, pb_anno.Field(1)] = dataclasses.field(default_factory=Prefix) - - -@dataclasses.dataclass -class Dm(pb_msg.BaseMessage): - id_str: Annotated[str, pb_anno.Field(1)] = '' - mode: Annotated[int, pb_anno.Field(2)] = 0 - fontsize: Annotated[int, pb_anno.Field(3)] = 0 - color: Annotated[pb_anno.uint, pb_anno.Field(4)] = 0 - mid_hash: Annotated[str, pb_anno.Field(5)] = '' - content: Annotated[str, pb_anno.Field(6)] = '' - ctime: Annotated[int, pb_anno.Field(7)] = 0 - weight: Annotated[int, pb_anno.Field(8)] = 0 - rnd: Annotated[int, pb_anno.Field(9)] = 0 - attr: Annotated[int, pb_anno.Field(10)] = 0 - # 为了防止加新枚举后不兼容,还是用int了 - # biz_scene: Annotated[BizScene, pb_anno.Field(11)] = BizScene.None_ - biz_scene: Annotated[int, pb_anno.Field(11)] = 0 - bubble: Annotated[Bubble, pb_anno.Field(12)] = dataclasses.field(default_factory=Bubble) - # dm_type: Annotated[DmType, pb_anno.Field(13)] = DmType.Normal - dm_type: Annotated[int, pb_anno.Field(13)] = 0 - emoticons: Annotated[List[EmoticonMapEntry], pb_anno.Field(14)] = dataclasses.field(default_factory=list) - voice: Annotated[Voice, pb_anno.Field(15)] = dataclasses.field(default_factory=Voice) - animation: Annotated[str, pb_anno.Field(16)] = '' - aggregation: Annotated[Aggregation, pb_anno.Field(17)] = dataclasses.field(default_factory=Aggregation) - send_from_me: Annotated[bool, pb_anno.Field(18)] = False - check: Annotated[Check, pb_anno.Field(19)] = dataclasses.field(default_factory=Check) - user: Annotated[User, pb_anno.Field(20)] = dataclasses.field(default_factory=User) - room: Annotated[Room, pb_anno.Field(21)] = dataclasses.field(default_factory=Room) - icon: Annotated[Icon, pb_anno.Field(22)] = dataclasses.field(default_factory=Icon) diff --git a/blivedm/models/web.py b/blivedm/models/web.py index a88aef4..2025695 100644 --- a/blivedm/models/web.py +++ b/blivedm/models/web.py @@ -1,12 +1,8 @@ # -*- coding: utf-8 -*- -import base64 -import binascii import dataclasses import json from typing import * -from . import pb - __all__ = ( 'HeartbeatMessage', 'DanmakuMessage', @@ -71,8 +67,6 @@ class DanmakuMessage: """用户ID""" uname: str = '' """用户名""" - face: str = '' - """用户头像URL""" admin: int = 0 """是否房管""" vip: int = 0 @@ -115,18 +109,7 @@ class DanmakuMessage: """舰队类型,0非舰队,1总督,2提督,3舰长""" @classmethod - def from_command(cls, info: list, dm_v2=''): - proto: Optional[pb.SimpleDm] = None - if dm_v2 != '': - try: - proto = pb.SimpleDm.loads(base64.b64decode(dm_v2)) - except (binascii.Error, KeyError, TypeError, ValueError): - pass - if proto is not None: - face = proto.user.face - else: - face = '' - + def from_command(cls, info: list): if len(info[3]) != 0: medal_level = info[3][0] medal_name = info[3][1] @@ -160,7 +143,6 @@ 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/requirements.txt b/requirements.txt index 1f76322..e3bdd2f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ aiohttp==3.8.6 Brotli==1.0.9 -pure-protobuf==3.0.0a5 yarl==1.8.2