Skip to content

Commit

Permalink
add FragVideo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangaiwen committed Aug 15, 2023
1 parent b6b6d5c commit 8cb84be
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 6 deletions.
142 changes: 142 additions & 0 deletions aiotieba/api/_classdef/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
链接碎片
Expand Down
45 changes: 43 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,
FragVideo,
FragLink,
FragmentUnknown,
FragText,
Expand All @@ -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
Expand Down Expand Up @@ -157,22 +159,26 @@ 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__ = [
'_text',
'_texts',
'_emojis',
'_imgs',
'_videos',
'_ats',
'_links',
'_tiebapluses',
'_has_voice',
'_has_video'
]

def _init(self, protos: Iterable[TypeMessage]) -> "Contents_p":
Expand Down Expand Up @@ -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)
Expand All @@ -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]

Expand All @@ -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:
Expand Down Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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]):
"""
Expand Down Expand Up @@ -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碎片列表
Expand All @@ -1479,6 +1508,7 @@ class Contents_pt(Containers[TypeFragment]):
'_texts',
'_emojis',
'_imgs',
'_videos',
'_ats',
'_links',
'_tiebapluses',
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand All @@ -1540,6 +1572,7 @@ def _init_null(self) -> "Contents_pt":
self._texts = []
self._emojis = []
self._imgs = []
self._videos = []
self._ats = []
self._links = []
self._tiebapluses = []
Expand Down Expand Up @@ -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]:
"""
Expand Down
Loading

0 comments on commit 8cb84be

Please sign in to comment.