From b6b6d5c540fd059de1b51da415396675f11ec1bb Mon Sep 17 00:00:00 2001 From: zhangaiwen Date: Mon, 14 Aug 2023 17:02:59 +0800 Subject: [PATCH 1/3] add FragEmoji.src and FragEmoji.text --- aiotieba/api/_classdef/contents.py | 38 ++++++++++++++++++++++++++++-- tests/test_get_comments.py | 2 ++ tests/test_get_posts.py | 2 ++ tests/test_get_threads.py | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/aiotieba/api/_classdef/contents.py b/aiotieba/api/_classdef/contents.py index 7e03da64..7b3705d3 100644 --- a/aiotieba/api/_classdef/contents.py +++ b/aiotieba/api/_classdef/contents.py @@ -47,15 +47,19 @@ class FragEmoji(object): Attributes: desc (str): 表情描述 + src (str): 表情图片链接 + text (str): 表情图片名称 """ - __slots__ = ['_desc'] + __slots__ = ['_desc', '_src', '_text'] def __init__(self, data_proto: TypeMessage) -> None: self._desc = data_proto.c + self._src = data_proto.src + self._text = data_proto.text def __repr__(self) -> str: - return str({'desc': self.desc}) + return str({'desc': self.desc, 'text': self.text, 'src': self.src}) @property def desc(self) -> str: @@ -65,6 +69,22 @@ def desc(self) -> str: return self._desc + @property + def src(self) -> str: + """ + 表情图片链接 + """ + + return self._src + + @property + def text(self) -> str: + """ + 表情图片名称 + """ + + return self._text + class TypeFragEmoji(Protocol): @property @@ -74,6 +94,20 @@ def desc(self) -> str: """ ... + @property + def src(self) -> str: + """ + 表情图片链接 + """ + ... + + @property + def text(self) -> str: + """ + 表情图片名称 + """ + ... + class FragImage(object): """ diff --git a/tests/test_get_comments.py b/tests/test_get_comments.py index 5445ff7b..cc8bff70 100644 --- a/tests/test_get_comments.py +++ b/tests/test_get_comments.py @@ -85,6 +85,8 @@ async def test_Comments(client: tb.Client): # FragEmoji frag = post.contents.emojis[0] assert frag.desc != '' + assert frag.src == '' + assert frag.text == 'image_emoticon3' # FragTiebaplus frag = post.contents.tiebapluses[0] diff --git a/tests/test_get_posts.py b/tests/test_get_posts.py index c92d82e5..f19cf8eb 100644 --- a/tests/test_get_posts.py +++ b/tests/test_get_posts.py @@ -107,6 +107,8 @@ async def test_Posts(client: tb.Client): # FragEmoji frag = post.contents.emojis[0] assert frag.desc != '' + assert frag.src == '' + assert frag.text == 'image_emoticon3' # FragTiebaplus frag = post.contents.tiebapluses[0] diff --git a/tests/test_get_threads.py b/tests/test_get_threads.py index 5396defa..60f6c822 100644 --- a/tests/test_get_threads.py +++ b/tests/test_get_threads.py @@ -80,6 +80,8 @@ async def test_Threads(client: tb.Client): # FragEmoji frag = thread.contents.emojis[0] assert frag.desc != '' + assert frag.src == '' + assert frag.text == 'image_emoticon2' # FragTiebaplus frag = thread.contents.tiebapluses[0] From 8cb84be111f2983ac25628491db2158373a1dad2 Mon Sep 17 00:00:00 2001 From: zhangaiwen Date: Tue, 15 Aug 2023 13:14:52 +0800 Subject: [PATCH 2/3] add FragVideo --- aiotieba/api/_classdef/contents.py | 142 ++++++++++++++++++++++++++ aiotieba/api/get_posts/_classdef.py | 45 +++++++- aiotieba/api/get_threads/_classdef.py | 32 +++++- aiotieba/api/profile/_classdef.py | 19 +++- tests/test_get_posts.py | 58 +++++++++++ tests/test_get_threads.py | 18 ++++ 6 files changed, 308 insertions(+), 6 deletions(-) diff --git a/aiotieba/api/_classdef/contents.py b/aiotieba/api/_classdef/contents.py index 7b3705d3..4eec119e 100644 --- a/aiotieba/api/_classdef/contents.py +++ b/aiotieba/api/_classdef/contents.py @@ -312,6 +312,148 @@ def user_id(self) -> int: ... +class FragVideo(object): + """ + 视频碎片 + + Attributes: + text (str): 视频页面链接 + src (str): 视频链接 + thumb_src (str): 缩略图链接 + width (int): 视频画面宽度 + height (int): 视频画面高度 + size (int): 视频体积大小 + """ + + __slots__ = [ + '_text', + '_src', + '_thumb_src', + '_width', + '_height', + '_size', + ] + + def __init__(self, data_proto: TypeMessage) -> None: + self._text = data_proto.text + self._src = data_proto.link + self._thumb_src = data_proto.src + self._size = data_proto.origin_size + + _width, _, _height = data_proto.bsize.partition(',') + self._width = int(_width if _width else 0) + self._height = int(_height if _height else 0) + + def __repr__(self) -> str: + return str( + { + 'text': self._text, + 'src': self._src, + 'width': self._width, + 'height': self._height, + } + ) + + @property + def text(self) -> str: + """ + 视频页面链接 + """ + + return self._text + + @property + def src(self) -> str: + """ + 视频链接 + """ + + return self._src + + @property + def thumb_src(self) -> str: + """ + 缩略图链接 + """ + + return self._thumb_src + + @property + def width(self) -> int: + """ + 视频画面宽度 + """ + + return self._width + + @property + def height(self) -> int: + """ + 视频画面高度 + """ + + return self._height + + @property + def size(self) -> int: + """ + 视频体积大小 + + Note: + 以字节为单位 + """ + + return self._size + + +class TypeFragVideo(Protocol): + @property + def text(self) -> str: + """ + 视频页面链接 + """ + ... + + @property + def src(self) -> str: + """ + 视频链接 + """ + ... + + @property + def thumb_src(self) -> str: + """ + 缩略图链接 + """ + ... + + @property + def width(self) -> int: + """ + 视频画面宽度 + """ + + return self._width + + @property + def height(self) -> int: + """ + 视频画面高度 + """ + ... + + @property + def size(self) -> int: + """ + 视频体积大小 + + Note: + 以字节为单位 + """ + ... + + class FragLink(object): """ 链接碎片 diff --git a/aiotieba/api/get_posts/_classdef.py b/aiotieba/api/get_posts/_classdef.py index 4d804ab5..1814b337 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, + FragVideo, FragLink, FragmentUnknown, FragText, @@ -17,6 +18,7 @@ FragAt_p = FragAt_pt = FragAt_pc = FragAt FragEmoji_p = FragEmoji_pt = FragEmoji_pc = FragEmoji +FragVideo_p = FragVideo_pt = FragVideo FragLink_p = FragLink_pt = FragLink_pc = FragLink FragmentUnknown_p = FragmentUnknown_pt = FragmentUnknown_pc = FragmentUnknown FragText_p = FragText_pt = FragText_pc = FragText @@ -157,11 +159,13 @@ class Contents_p(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_p]): 表情碎片列表 imgs (list[FragImage_p]): 图像碎片列表 + videos (list[FragVideo_p]): 视频碎片列表 ats (list[FragAt_p]): @碎片列表 links (list[FragLink_p]): 链接碎片列表 tiebapluses (list[FragTiebaPlus_p]): 贴吧plus碎片列表 has_voice (bool): 是否包含音频 + has_video (bool): 是否包含视频 """ __slots__ = [ @@ -169,10 +173,12 @@ class Contents_p(Containers[TypeFragment]): '_texts', '_emojis', '_imgs', + '_videos', '_ats', '_links', '_tiebapluses', '_has_voice', + '_has_video' ] def _init(self, protos: Iterable[TypeMessage]) -> "Contents_p": @@ -202,7 +208,9 @@ def _init_by_type(proto): fragment = FragmentUnknown_p() self._has_voice = True elif _type == 5: # video - fragment = FragmentUnknown_p() + fragment = FragVideo_p(proto) + self._videos.append(fragment) + self._has_video = True # 35|36:tid=7769728331 / 37:tid=7760184147 elif _type in [35, 36, 37]: fragment = FragTiebaPlus_p(proto) @@ -221,9 +229,11 @@ def _init_by_type(proto): self._links = [] self._imgs = [] self._emojis = [] + self._videos = [] self._ats = [] self._tiebapluses = [] self._has_voice = False + self._has_video = False self._objs = [_init_by_type(p) for p in protos] @@ -235,10 +245,12 @@ def _init_null(self) -> "Contents_p": self._texts = [] self._emojis = [] self._imgs = [] + self._videos = [] self._ats = [] self._links = [] self._tiebapluses = [] self._has_voice = False + self._has_video = False return self def __repr__(self) -> str: @@ -278,6 +290,14 @@ def imgs(self) -> List[FragImage_p]: return self._imgs + @property + def videos(self) -> List[FragVideo_p]: + """ + 视频碎片列表 + """ + + return self._videos + @property def ats(self) -> List[FragAt_p]: """ @@ -310,6 +330,14 @@ def has_voice(self) -> bool: return self._has_voice + @property + def has_video(self) -> bool: + """ + 是否包含视频 + """ + + return self._has_video + class Contents_pc(Containers[TypeFragment]): """ @@ -1466,6 +1494,7 @@ class Contents_pt(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_pt]): 表情碎片列表 imgs (list[FragImage_pt]): 图像碎片列表 + videos (list[FragVideo_pt]): 视频碎片列表 ats (list[FragAt_pt]): @碎片列表 links (list[FragLink_pt]): 链接碎片列表 tiebapluses (list[FragTiebaPlus_pt]): 贴吧plus碎片列表 @@ -1479,6 +1508,7 @@ class Contents_pt(Containers[TypeFragment]): '_texts', '_emojis', '_imgs', + '_videos', '_ats', '_links', '_tiebapluses', @@ -1506,7 +1536,8 @@ def _init_by_type(proto) -> TypeFragment: self._links.append(fragment) self._texts.append(fragment) elif _type == 5: # video - fragment = FragmentUnknown_pt() + fragment = FragVideo_pt(proto) + self._videos.append(fragment) self._has_video = True # 35|36:tid=7769728331 / 37:tid=7760184147 elif _type in [35, 36, 37]: @@ -1525,6 +1556,7 @@ def _init_by_type(proto) -> TypeFragment: self._texts = [] self._links = [] self._emojis = [] + self._videos = [] self._ats = [] self._tiebapluses = [] self._has_voice = False @@ -1540,6 +1572,7 @@ def _init_null(self) -> "Contents_pt": self._texts = [] self._emojis = [] self._imgs = [] + self._videos = [] self._ats = [] self._links = [] self._tiebapluses = [] @@ -1584,6 +1617,14 @@ def imgs(self) -> List[FragImage_pt]: return self._imgs + @property + def videos(self) -> List[FragVideo_pt]: + """ + 视频碎片列表 + """ + + return self._videos + @property def ats(self) -> List[FragAt_pt]: """ diff --git a/aiotieba/api/get_threads/_classdef.py b/aiotieba/api/get_threads/_classdef.py index 2af928a4..968daf50 100644 --- a/aiotieba/api/get_threads/_classdef.py +++ b/aiotieba/api/get_threads/_classdef.py @@ -4,6 +4,7 @@ from .._classdef.contents import ( FragAt, FragEmoji, + FragVideo, FragLink, FragmentUnknown, FragText, @@ -16,6 +17,7 @@ FragAt_t = FragAt_st = FragAt FragEmoji_t = FragEmoji_st = FragEmoji +FragVideo_t = FragVideo_st = FragVideo FragLink_t = FragLink_st = FragLink FragmentUnknown_t = FragmentUnknown_st = FragmentUnknown FragText_t = FragText_st = FragText @@ -156,6 +158,7 @@ class Contents_t(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_t]): 表情碎片列表 imgs (list[FragImage_t]): 图像碎片列表 + videos (list[FragVideo_t]): 视频碎片列表 ats (list[FragAt_t]): @碎片列表 links (list[FragLink_t]): 链接碎片列表 tiebapluses (list[FragTiebaPlus_t]): 贴吧plus碎片列表 @@ -169,6 +172,7 @@ class Contents_t(Containers[TypeFragment]): '_texts', '_emojis', '_imgs', + '_videos', '_ats', '_links', '_tiebapluses', @@ -200,7 +204,8 @@ def _init_by_type(proto): self._links.append(fragment) self._texts.append(fragment) elif _type == 5: # video - fragment = FragmentUnknown_t() + fragment = FragVideo_t(proto) + self._videos.append(fragment) self._has_video = True elif _type == 10: fragment = FragmentUnknown_t() @@ -223,6 +228,7 @@ def _init_by_type(proto): self._links = [] self._imgs = [] self._emojis = [] + self._videos = [] self._ats = [] self._tiebapluses = [] self._has_voice = False @@ -238,6 +244,7 @@ def _init_null(self) -> "Contents_t": self._texts = [] self._emojis = [] self._imgs = [] + self._videos = [] self._ats = [] self._links = [] self._tiebapluses = [] @@ -282,6 +289,14 @@ def imgs(self) -> List[FragImage_t]: return self._imgs + @property + def videos(self) -> List[FragVideo_t]: + """ + 视频碎片列表 + """ + + return self._videos + @property def ats(self) -> List[FragAt_t]: """ @@ -803,6 +818,7 @@ class Contents_st(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_st]): 表情碎片列表 imgs (list[FragImage_st]): 图像碎片列表 + videos (list[FragVideo_st]): 视频碎片列表 ats (list[FragAt_st]): @碎片列表 links (list[FragLink_st]): 链接碎片列表 tiebapluses (list[FragTiebaPlus_st]): 贴吧plus碎片列表 @@ -816,6 +832,7 @@ class Contents_st(Containers[TypeFragment]): '_texts', '_emojis', '_imgs', + '_videos', '_ats', '_links', '_tiebapluses', @@ -843,7 +860,8 @@ def _init_by_type(proto) -> TypeFragment: self._links.append(fragment) self._texts.append(fragment) elif _type == 5: # video - fragment = FragmentUnknown_st() + fragment = FragVideo_st(proto) + self._videos.append(fragment) self._has_video = True # 35|36:tid=7769728331 / 37:tid=7760184147 elif _type in [35, 36, 37]: @@ -862,6 +880,7 @@ def _init_by_type(proto) -> TypeFragment: self._texts = [] self._links = [] self._emojis = [] + self._videos = [] self._ats = [] self._tiebapluses = [] self._has_voice = False @@ -877,6 +896,7 @@ def _init_null(self) -> "Contents_st": self._texts = [] self._emojis = [] self._imgs = [] + self._videos = [] self._ats = [] self._links = [] self._tiebapluses = [] @@ -921,6 +941,14 @@ def imgs(self) -> List[FragImage_st]: return self._imgs + @property + def videos(self) -> List[FragVideo_st]: + """ + 视频碎片列表 + """ + + return self._videos + @property def ats(self) -> List[FragAt_st]: """ diff --git a/aiotieba/api/profile/_classdef.py b/aiotieba/api/profile/_classdef.py index 04f35437..d76df3ff 100644 --- a/aiotieba/api/profile/_classdef.py +++ b/aiotieba/api/profile/_classdef.py @@ -1,10 +1,12 @@ from typing import Iterable, List, Optional from .._classdef import Containers, TypeMessage, VoteInfo -from .._classdef.contents import FragAt, FragEmoji, FragLink, FragmentUnknown, FragText, TypeFragment, TypeFragText +from .._classdef.contents import FragAt, FragEmoji, FragVideo, FragLink, FragmentUnknown, FragText, TypeFragment, \ + TypeFragText FragAt_pf = FragAt FragEmoji_pf = FragEmoji +FragVideo_pf = FragVideo FragLink_pf = FragLink FragmentUnknown_pf = FragmentUnknown FragText_pf = FragText @@ -556,6 +558,7 @@ class Contents_pf(Containers[TypeFragment]): texts (list[TypeFragText]): 纯文本碎片列表 emojis (list[FragEmoji_pf]): 表情碎片列表 imgs (list[FragImage_pf]): 图像碎片列表 + videos (list[FragVideo_pf]): 视频碎片列表 ats (list[FragAt_pf]): @碎片列表 links (list[FragLink_pf]): 链接碎片列表 @@ -568,6 +571,7 @@ class Contents_pf(Containers[TypeFragment]): '_texts', '_emojis', '_imgs', + '_videos', '_ats', '_links', '_has_voice', @@ -596,7 +600,8 @@ def _init_by_type(proto): self._links.append(fragment) self._texts.append(fragment) elif _type == 5: # video - fragment = FragmentUnknown_pf() + fragment = FragVideo_pf(proto) + self._videos.append(fragment) self._has_video = True elif _type == 10: fragment = FragmentUnknown_pf() @@ -614,6 +619,7 @@ def _init_by_type(proto): self._links = [] self._imgs = [] self._emojis = [] + self._videos = [] self._ats = [] self._has_voice = False self._has_video = False @@ -627,6 +633,7 @@ def _init_null(self) -> "Contents_pf": self._text = "" self._texts = [] self._emojis = [] + self._videos = [] self._imgs = [] self._ats = [] self._links = [] @@ -671,6 +678,14 @@ def imgs(self) -> List[FragImage_pf]: return self._imgs + @property + def videos(self) -> List[FragVideo_pf]: + """ + 视频碎片列表 + """ + + return self._videos + @property def ats(self) -> List[FragAt_pf]: """ diff --git a/tests/test_get_posts.py b/tests/test_get_posts.py index f19cf8eb..3c702828 100644 --- a/tests/test_get_posts.py +++ b/tests/test_get_posts.py @@ -95,6 +95,9 @@ async def test_Posts(client: tb.Client): # FragVoice assert post.contents.has_voice is True + # FragVideo + assert post.contents.has_video is False + # FragImage frag = post.contents.imgs[0] assert frag.src != '' @@ -128,6 +131,61 @@ async def test_Posts(client: tb.Client): assert frag.url.host == "stackoverflow.com" assert frag.is_external is True + # Posts with video + posts = await client.get_posts(7544266790) + + ##### Forum_p ##### + forum = posts.forum + assert forum.fid == 27738629 + assert forum.fname == '贴吧视频号' + assert forum.member_num > 0 + assert forum.post_num > 0 + + ##### Thread_p ##### + thread = posts.thread + + ##### Post ##### + assert len(posts) >= 2 + post = posts[0] + + # UserInfo_p + user = post.user + assert user.user_id > 0 + assert user.portrait != '' + assert user.user_name != '' + assert user.nick_name_new != '' + assert user.show_name == user.nick_name_new + assert user.level > 0 + assert user.glevel > 0 + assert user.ip != '' + assert user.priv_like != 0 + assert user.priv_reply != 0 + + # Post + assert post.text != '' + assert post.fid > 0 + assert post.fname != '' + assert post.tid > 0 + assert post.pid > 0 + assert post.author_id == user.user_id + assert post.floor > 0 + assert post.reply_num == 0 + assert post.create_time > 0 + assert post.is_thread_author == (post.author_id == thread.author_id) + + # FragText + frag = post.contents.texts[0] + assert frag.text != '' + + # FragVideo + assert post.contents.has_video is True + frag = post.contents.videos[0] + assert frag.text != '' + assert frag.src != '' + assert frag.thumb_src != '' + assert frag.width == 1920 + assert frag.height == 1080 + assert frag.size > 0 @pytest.mark.flaky(reruns=3, reruns_delay=2.0) @pytest.mark.asyncio diff --git a/tests/test_get_threads.py b/tests/test_get_threads.py index 60f6c822..ca98fad0 100644 --- a/tests/test_get_threads.py +++ b/tests/test_get_threads.py @@ -68,6 +68,9 @@ async def test_Threads(client: tb.Client): # FragVoice assert thread.contents.has_voice is True + # FragVideo + assert thread.contents.has_video is False + # FragImage frag = thread.contents.imgs[0] assert frag.src != '' @@ -122,6 +125,9 @@ async def test_Threads(client: tb.Client): # FragVoice assert sthread.contents.has_voice is True + # FragVideo + assert thread.contents.has_video is False + # FragImage frag = sthread.contents.imgs[0] assert frag.src != '' @@ -137,3 +143,15 @@ async def test_Threads(client: tb.Client): option = vote_info.options[0] assert option.vote_num > 0 assert option.text != '' + + elif thread.tid == 972664850: + + # FragVideo + assert thread.contents.has_video is True + frag = thread.contents.videos[0] + assert frag.text != '' + assert frag.src == '' + assert frag.thumb_src == '' + assert frag.width == 0 + assert frag.height == 0 + assert frag.size == 0 From e888bf544fdb167e8bc1411f3aa94849d4b40804 Mon Sep 17 00:00:00 2001 From: zhangaiwen Date: Tue, 15 Aug 2023 16:14:17 +0800 Subject: [PATCH 3/3] add FragVoice --- aiotieba/api/_classdef/contents.py | 75 ++++++++++++++++++++++++++ aiotieba/api/get_comments/_classdef.py | 32 ++++++++++- aiotieba/api/get_posts/_classdef.py | 32 ++++++++++- tests/test_get_comments.py | 8 +++ tests/test_get_posts.py | 4 ++ 5 files changed, 147 insertions(+), 4 deletions(-) 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