fix: bsky parse empty author

This commit is contained in:
xtaodada 2024-10-21 14:33:55 +08:00
parent 68c546914c
commit 16e2a9bce5
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 24 additions and 8 deletions

View File

@ -9,7 +9,6 @@ from pyrogram.types import (
InlineKeyboardMarkup, InlineKeyboardMarkup,
InlineKeyboardButton, InlineKeyboardButton,
InputMediaPhoto, InputMediaPhoto,
Message,
) )
from init import bot, logs from init import bot, logs
@ -78,12 +77,16 @@ class Timeline:
def get_post_text(post: HumanPost) -> str: def get_post_text(post: HumanPost) -> str:
text = "<b>Bsky Post Info</b>\n\n<code>" text = "<b>Bsky Post Info</b>\n\n<code>"
text += post.content text += post.content
text += "</code>\n\n"
key = "发表" key = "发表"
if post.is_reply: if post.is_reply:
key = "回复" key = "回复"
elif post.is_quote: elif post.is_quote:
key = "引用" key = "引用"
text += f"</code>\n\n{post.author.format} {key}{post.time_str}" elif post.is_repost:
text += f"{post.repost_info.by.format} 转发于 {post.repost_info.time_str}\n"
text += f"{post.author.format} {key}{post.time_str}\n"
text += f"点赞: {post.like_count} | 引用: {post.quote_count} | 回复: {post.reply_count} | 转发: {post.repost_count}"
return text return text
@staticmethod @staticmethod

View File

@ -31,7 +31,7 @@ class HumanAuthor(BaseModel):
display_name: str display_name: str
handle: str handle: str
did: str did: str
avatar_img: str avatar_img: Optional[str] = None
created_at: datetime created_at: datetime
description: Optional[str] = None description: Optional[str] = None
@ -59,7 +59,7 @@ class HumanAuthor(BaseModel):
@staticmethod @staticmethod
def parse(author: "ProfileViewBasic") -> "HumanAuthor": def parse(author: "ProfileViewBasic") -> "HumanAuthor":
return HumanAuthor( return HumanAuthor(
display_name=author.display_name, display_name=author.display_name or author.handle,
handle=author.handle, handle=author.handle,
did=author.did, did=author.did,
avatar_img=author.avatar, avatar_img=author.avatar,
@ -69,7 +69,7 @@ class HumanAuthor(BaseModel):
@staticmethod @staticmethod
def parse_detail(author: "ProfileViewDetailed") -> "HumanAuthor": def parse_detail(author: "ProfileViewDetailed") -> "HumanAuthor":
return HumanAuthor( return HumanAuthor(
display_name=author.display_name, display_name=author.display_name or author.handle,
handle=author.handle, handle=author.handle,
did=author.did, did=author.did,
avatar_img=author.avatar, avatar_img=author.avatar,
@ -81,6 +81,16 @@ class HumanAuthor(BaseModel):
) )
class HumanRepostInfo(BaseModel):
by: HumanAuthor
at: datetime
@property
def time_str(self) -> str:
# utc+8
return self.at.astimezone(TZ).strftime("%Y-%m-%d %H:%M:%S")
class HumanPost(BaseModel, frozen=False): class HumanPost(BaseModel, frozen=False):
cid: str cid: str
content: str content: str
@ -103,7 +113,8 @@ class HumanPost(BaseModel, frozen=False):
is_reply: bool = False is_reply: bool = False
is_repost: bool = False is_repost: bool = False
parent_post: "HumanPost" = None repost_info: Optional[HumanRepostInfo] = None
parent_post: Optional["HumanPost"] = None
@property @property
def url(self) -> str: def url(self) -> str:
@ -163,13 +174,14 @@ class HumanPost(BaseModel, frozen=False):
def parse(data: "FeedViewPost") -> "HumanPost": def parse(data: "FeedViewPost") -> "HumanPost":
base = HumanPost.parse_view(data.post) base = HumanPost.parse_view(data.post)
is_quote, is_reply, is_repost = False, False, False is_quote, is_reply, is_repost = False, False, False
parent_post = None parent_post, repost_info = None, None
if data.reply: if data.reply:
is_reply = True is_reply = True
if hasattr(data.reply.parent, "post"): if hasattr(data.reply.parent, "post"):
parent_post = HumanPost.parse_view(data.reply.parent) parent_post = HumanPost.parse_view(data.reply.parent)
elif data.reason: elif data.reason:
is_repost = True is_repost = True
repost_info = HumanRepostInfo(by=HumanAuthor.parse(data.reason.by), at=data.reason.at)
elif data.post.embed and isinstance(data.post.embed, BskyViewRecord): elif data.post.embed and isinstance(data.post.embed, BskyViewRecord):
is_quote = True is_quote = True
if isinstance(data.post.embed.record, BskyViewRecordRecord): if isinstance(data.post.embed.record, BskyViewRecordRecord):
@ -178,6 +190,7 @@ class HumanPost(BaseModel, frozen=False):
base.is_reply = is_reply base.is_reply = is_reply
base.is_repost = is_repost base.is_repost = is_repost
base.parent_post = parent_post base.parent_post = parent_post
base.repost_info = repost_info
return base return base
@staticmethod @staticmethod
@ -188,7 +201,7 @@ class HumanPost(BaseModel, frozen=False):
if data.parent: if data.parent:
is_reply = True is_reply = True
if hasattr(data.parent, "post"): if hasattr(data.parent, "post"):
parent_post = HumanPost.parse_view(data.parent.post) parent_post = HumanPost.parse_thread(data.parent)
elif data.post.embed and isinstance(data.post.embed, BskyViewRecord): elif data.post.embed and isinstance(data.post.embed, BskyViewRecord):
is_quote = True is_quote = True
if isinstance(data.post.embed.record, BskyViewRecordRecord): if isinstance(data.post.embed.record, BskyViewRecordRecord):