diff --git a/aiotieba/api/_classdef/contents.py b/aiotieba/api/_classdef/contents.py index 4eec119e..e6d70b7a 100644 --- a/aiotieba/api/_classdef/contents.py +++ b/aiotieba/api/_classdef/contents.py @@ -312,6 +312,81 @@ def user_id(self) -> int: ... +class FragVoice(object): + """ + 视频碎片 + + Attributes: + md5 (str): 音频 md5 + id (str): 音频 id + name (str): 音频文件名 + """ + + __slots__ = [ + '_md5', + '_id', + '_name', + ] + + def __init__(self, data_proto: TypeMessage) -> None: + self._name = data_proto.voice_md5 + self._md5, _, self._id = data_proto.voice_md5.partition('_') + + def __repr__(self) -> str: + return str( + { + 'name': self._name + } + ) + + @property + def name(self) -> str: + """ + 音频文件名 + """ + + return self._name + + @property + def md5(self) -> str: + """ + 音频 md5 + """ + + return self._md5 + + @property + def id(self) -> str: + """ + 音频 id + """ + + return self._id + + +class TypeFragVoice(Protocol): + @property + def name(self) -> str: + """ + 音频文件名 + """ + ... + + @property + def md5(self) -> str: + """ + 音频 md5 + """ + ... + + @property + def id(self) -> str: + """ + 音频 id + """ + ... + + class FragVideo(object): """ 视频碎片 diff --git a/aiotieba/api/get_comments/_classdef.py b/aiotieba/api/get_comments/_classdef.py index d95ebd47..051b5e7a 100644 --- a/aiotieba/api/get_comments/_classdef.py +++ b/aiotieba/api/get_comments/_classdef.py @@ -5,6 +5,7 @@ from .._classdef.contents import ( FragAt, FragEmoji, + FragVoice, FragLink, FragmentUnknown, FragText, @@ -15,6 +16,7 @@ FragAt_c = FragAt_cp = FragAt FragEmoji_c = FragEmoji_cp = FragEmoji +FragVoice_c = FragVoice_cp = FragVoice FragLink_c = FragLink_cp = FragLink FragmentUnknown_c = FragmentUnknown_cp = FragmentUnknown FragText_c = FragText_cp = FragText @@ -32,6 +34,7 @@ class Contents_c(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_c]): 表情碎片列表 + voices (list[FragVoice_c]): 音頻碎片列表 ats (list[FragAt_c]): @碎片列表 links (list[FragLink_c]): 链接碎片列表 tiebapluses (list[FragTiebaPlus_c]): 贴吧plus碎片列表 @@ -43,6 +46,7 @@ class Contents_c(Containers[TypeFragment]): '_text', '_texts', '_emojis', + '_voices', '_ats', '_links', '_tiebapluses', @@ -69,7 +73,8 @@ def _init_by_type(proto): self._links.append(fragment) self._texts.append(fragment) elif _type == 10: - fragment = FragmentUnknown_c() + fragment = FragVoice_c(proto) + self._voices.append(fragment) self._has_voice = True # 35|36:tid=7769728331 / 37:tid=7760184147 elif _type in [35, 36, 37]: @@ -88,6 +93,7 @@ def _init_by_type(proto): self._texts = [] self._links = [] self._emojis = [] + self._voices = [] self._ats = [] self._tiebapluses = [] self._has_voice = False @@ -101,6 +107,7 @@ def _init_null(self) -> "Contents_c": self._text = "" self._texts = [] self._emojis = [] + self._voices = [] self._ats = [] self._links = [] self._tiebapluses = [] @@ -136,6 +143,14 @@ def emojis(self) -> List[FragEmoji_c]: return self._emojis + @property + def voices(self) -> List[FragVoice_c]: + """ + 视频碎片列表 + """ + + return self._voices + @property def ats(self) -> List[FragAt_c]: """ @@ -1209,6 +1224,7 @@ class Contents_cp(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_cp]): 表情碎片列表 + voices (list[FragVoice_cp]): 音頻碎片列表 imgs (list[FragImage_cp]): 图像碎片列表 ats (list[FragAt_cp]): @碎片列表 links (list[FragLink_cp]): 链接碎片列表 @@ -1221,6 +1237,7 @@ class Contents_cp(Containers[TypeFragment]): '_text', '_texts', '_emojis', + '_voices', '_imgs', '_ats', '_links', @@ -1252,7 +1269,8 @@ def _init_by_type(proto): self._links.append(fragment) self._texts.append(fragment) elif _type == 10: - fragment = FragmentUnknown_cp() + fragment = FragVoice_cp(proto) + self._voices.append(fragment) self._has_voice = True # 35|36:tid=7769728331 / 37:tid=7760184147 elif _type in [35, 36, 37]: @@ -1272,6 +1290,7 @@ def _init_by_type(proto): self._links = [] self._imgs = [] self._emojis = [] + self._voices = [] self._ats = [] self._tiebapluses = [] self._has_voice = False @@ -1286,6 +1305,7 @@ def _init_null(self) -> "Contents_cp": self._texts = [] self._emojis = [] self._imgs = [] + self._voices = [] self._ats = [] self._links = [] self._tiebapluses = [] @@ -1329,6 +1349,14 @@ def imgs(self) -> List[FragImage_cp]: return self._imgs + @property + def voices(self) -> List[FragVoice_cp]: + """ + 视频碎片列表 + """ + + return self._voices + @property def ats(self) -> List[FragAt_cp]: """ diff --git a/aiotieba/api/get_posts/_classdef.py b/aiotieba/api/get_posts/_classdef.py index 1814b337..6be037b1 100644 --- a/aiotieba/api/get_posts/_classdef.py +++ b/aiotieba/api/get_posts/_classdef.py @@ -5,6 +5,7 @@ from .._classdef.contents import ( FragAt, FragEmoji, + FragVoice, FragVideo, FragLink, FragmentUnknown, @@ -18,6 +19,7 @@ FragAt_p = FragAt_pt = FragAt_pc = FragAt FragEmoji_p = FragEmoji_pt = FragEmoji_pc = FragEmoji +FragVoice_p = FragVoice_pt = FragVoice_pc = FragVoice FragVideo_p = FragVideo_pt = FragVideo FragLink_p = FragLink_pt = FragLink_pc = FragLink FragmentUnknown_p = FragmentUnknown_pt = FragmentUnknown_pc = FragmentUnknown @@ -159,6 +161,7 @@ class Contents_p(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_p]): 表情碎片列表 imgs (list[FragImage_p]): 图像碎片列表 + voices (list[FragVoice_p]): 音頻碎片列表 videos (list[FragVideo_p]): 视频碎片列表 ats (list[FragAt_p]): @碎片列表 links (list[FragLink_p]): 链接碎片列表 @@ -173,6 +176,7 @@ class Contents_p(Containers[TypeFragment]): '_texts', '_emojis', '_imgs', + '_voices', '_videos', '_ats', '_links', @@ -205,7 +209,8 @@ def _init_by_type(proto): self._links.append(fragment) self._texts.append(fragment) elif _type == 10: - fragment = FragmentUnknown_p() + fragment = FragVoice_p(proto) + self._voices.append(fragment) self._has_voice = True elif _type == 5: # video fragment = FragVideo_p(proto) @@ -229,6 +234,7 @@ def _init_by_type(proto): self._links = [] self._imgs = [] self._emojis = [] + self._voices = [] self._videos = [] self._ats = [] self._tiebapluses = [] @@ -245,6 +251,7 @@ def _init_null(self) -> "Contents_p": self._texts = [] self._emojis = [] self._imgs = [] + self._voices = [] self._videos = [] self._ats = [] self._links = [] @@ -298,6 +305,14 @@ def videos(self) -> List[FragVideo_p]: return self._videos + @property + def voices(self) -> List[FragVoice_p]: + """ + 视频碎片列表 + """ + + return self._voices + @property def ats(self) -> List[FragAt_p]: """ @@ -350,6 +365,7 @@ class Contents_pc(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_pc]): 表情碎片列表 + voices (list[FragVoice_pc]): 音頻碎片列表 ats (list[FragAt_pc]): @碎片列表 links (list[FragLink_pc]): 链接碎片列表 tiebapluses (list[FragTiebaPlus_pc]): 贴吧plus碎片列表 @@ -361,6 +377,7 @@ class Contents_pc(Containers[TypeFragment]): '_text', '_texts', '_emojis', + '_voices', '_ats', '_links', '_tiebapluses', @@ -387,7 +404,8 @@ def _init_by_type(proto): self._links.append(fragment) self._texts.append(fragment) elif _type == 10: - fragment = FragmentUnknown_pc() + fragment = FragVoice_pc(proto) + self._voices.append(fragment) self._has_voice = True # 35|36:tid=7769728331 / 37:tid=7760184147 elif _type in [35, 36, 37]: @@ -406,6 +424,7 @@ def _init_by_type(proto): self._texts = [] self._links = [] self._emojis = [] + self._voices = [] self._ats = [] self._tiebapluses = [] self._has_voice = False @@ -419,6 +438,7 @@ def _init_null(self) -> "Contents_pc": self._text = "" self._texts = [] self._emojis = [] + self._voices = [] self._ats = [] self._links = [] self._tiebapluses = [] @@ -454,6 +474,14 @@ def emojis(self) -> List[FragEmoji_pc]: return self._emojis + @property + def voices(self) -> List[FragVoice_pc]: + """ + 视频碎片列表 + """ + + return self._voices + @property def ats(self) -> List[FragAt_pc]: """ diff --git a/tests/test_get_comments.py b/tests/test_get_comments.py index cc8bff70..3205d557 100644 --- a/tests/test_get_comments.py +++ b/tests/test_get_comments.py @@ -72,6 +72,10 @@ async def test_Comments(client: tb.Client): # FragVoice assert post.contents.has_voice is True + frag = post.contents.voices[0] + assert frag.name != '' + assert frag.md5 != '' + assert frag.id != '' # FragImage frag = post.contents.imgs[0] @@ -135,6 +139,10 @@ async def test_Comments(client: tb.Client): # FragVoice assert comment.contents.has_voice is True + frag = comment.contents.voices[0] + assert frag.name != '' + assert frag.md5 != '' + assert frag.id != '' # FragEmoji frag = comment.contents.emojis[0] diff --git a/tests/test_get_posts.py b/tests/test_get_posts.py index 3c702828..b41ed91d 100644 --- a/tests/test_get_posts.py +++ b/tests/test_get_posts.py @@ -94,6 +94,10 @@ async def test_Posts(client: tb.Client): # FragVoice assert post.contents.has_voice is True + frag = post.contents.voices[0] + assert frag.name != '' + assert frag.md5 != '' + assert frag.id != '' # FragVideo assert post.contents.has_video is False