From c2f6c1255c86f1cbff9d79c6aa240c83843eaa6b Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 22 Oct 2024 17:10:05 +0800 Subject: [PATCH] feat: bsky support video --- defs/bsky.py | 10 ++++++++++ models/models/bsky.py | 18 +++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/defs/bsky.py b/defs/bsky.py index 64bf8e9..70720a5 100644 --- a/defs/bsky.py +++ b/defs/bsky.py @@ -100,6 +100,16 @@ class Timeline: parse_mode=ParseMode.HTML, reply_markup=Timeline.get_button(post), ) + elif post.video: + return await bot.send_video( + reply.cid, + post.video, + caption=text, + thumb=post.video_thumbnail, + reply_to_message_id=reply.mid, + parse_mode=ParseMode.HTML, + reply_markup=Timeline.get_button(post), + ) elif not post.images: return await bot.send_message( reply.cid, diff --git a/models/models/bsky.py b/models/models/bsky.py index 0553f03..64b55fa 100644 --- a/models/models/bsky.py +++ b/models/models/bsky.py @@ -27,6 +27,7 @@ if TYPE_CHECKING: ) TZ = pytz.timezone("Asia/Shanghai") +XRPC_DOMAIN = "bsky.social" class HumanAuthor(BaseModel): @@ -99,6 +100,7 @@ class HumanPost(BaseModel, frozen=False): images: Optional[list[str]] = None gif: Optional[str] = None video: Optional[str] = None + video_thumbnail: Optional[str] = None external: Optional[str] = None created_at: datetime @@ -138,12 +140,18 @@ class HumanPost(BaseModel, frozen=False): @staticmethod def parse_view(post: Union["PostView", "BskyViewRecordRecord"]) -> "HumanPost": record = post.value if isinstance(post, BskyViewRecordRecord) else post.record + # author + author = HumanAuthor.parse(post.author) embed = ( (post.embeds[0] if post.embeds else None) if isinstance(post, BskyViewRecordRecord) else post.embed ) - content = bsky_html_parser.unparse(record.text, record.facets) if record.facets else record.text + content = ( + bsky_html_parser.unparse(record.text, record.facets) + if record.facets + else record.text + ) created_at = record.created_at # images images = [] @@ -151,9 +159,10 @@ class HumanPost(BaseModel, frozen=False): for image in embed.images: images.append(image.fullsize) # video - video = None + video, video_thumbnail = None, None if isinstance(embed, BskyViewVideo): - video = embed.playlist # m3u8 + video = f"https://{XRPC_DOMAIN}/xrpc/com.atproto.sync.getBlob?did={author.did}&cid={embed.cid}" + video_thumbnail = embed.thumbnail # gif gif, extra = None, None if isinstance(embed, BskyViewExternal): @@ -162,14 +171,13 @@ class HumanPost(BaseModel, frozen=False): gif = uri else: extra = uri - # author - author = HumanAuthor.parse(post.author) return HumanPost( cid=post.cid, content=content, images=images, gif=gif, video=video, + video_thumbnail=video_thumbnail, external=extra, created_at=created_at, like_count=post.like_count,