Skip to content

Commit

Permalink
add FragVoice
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangaiwen committed Aug 15, 2023
1 parent 8cb84be commit e888bf5
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 4 deletions.
75 changes: 75 additions & 0 deletions aiotieba/api/_classdef/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
视频碎片
Expand Down
32 changes: 30 additions & 2 deletions aiotieba/api/get_comments/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .._classdef.contents import (
FragAt,
FragEmoji,
FragVoice,
FragLink,
FragmentUnknown,
FragText,
Expand All @@ -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
Expand All @@ -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碎片列表
Expand All @@ -43,6 +46,7 @@ class Contents_c(Containers[TypeFragment]):
'_text',
'_texts',
'_emojis',
'_voices',
'_ats',
'_links',
'_tiebapluses',
Expand All @@ -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]:
Expand All @@ -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
Expand All @@ -101,6 +107,7 @@ def _init_null(self) -> "Contents_c":
self._text = ""
self._texts = []
self._emojis = []
self._voices = []
self._ats = []
self._links = []
self._tiebapluses = []
Expand Down Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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]): 链接碎片列表
Expand All @@ -1221,6 +1237,7 @@ class Contents_cp(Containers[TypeFragment]):
'_text',
'_texts',
'_emojis',
'_voices',
'_imgs',
'_ats',
'_links',
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand All @@ -1286,6 +1305,7 @@ def _init_null(self) -> "Contents_cp":
self._texts = []
self._emojis = []
self._imgs = []
self._voices = []
self._ats = []
self._links = []
self._tiebapluses = []
Expand Down Expand Up @@ -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]:
"""
Expand Down
32 changes: 30 additions & 2 deletions aiotieba/api/get_posts/_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .._classdef.contents import (
FragAt,
FragEmoji,
FragVoice,
FragVideo,
FragLink,
FragmentUnknown,
Expand All @@ -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
Expand Down Expand Up @@ -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]): 链接碎片列表
Expand All @@ -173,6 +176,7 @@ class Contents_p(Containers[TypeFragment]):
'_texts',
'_emojis',
'_imgs',
'_voices',
'_videos',
'_ats',
'_links',
Expand Down Expand Up @@ -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)
Expand All @@ -229,6 +234,7 @@ def _init_by_type(proto):
self._links = []
self._imgs = []
self._emojis = []
self._voices = []
self._videos = []
self._ats = []
self._tiebapluses = []
Expand All @@ -245,6 +251,7 @@ def _init_null(self) -> "Contents_p":
self._texts = []
self._emojis = []
self._imgs = []
self._voices = []
self._videos = []
self._ats = []
self._links = []
Expand Down Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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碎片列表
Expand All @@ -361,6 +377,7 @@ class Contents_pc(Containers[TypeFragment]):
'_text',
'_texts',
'_emojis',
'_voices',
'_ats',
'_links',
'_tiebapluses',
Expand All @@ -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]:
Expand All @@ -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
Expand All @@ -419,6 +438,7 @@ def _init_null(self) -> "Contents_pc":
self._text = ""
self._texts = []
self._emojis = []
self._voices = []
self._ats = []
self._links = []
self._tiebapluses = []
Expand Down Expand Up @@ -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]:
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_get_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions tests/test_get_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e888bf5

Please sign in to comment.