Skip to content

Commit

Permalink
Refactor bluesky_client
Browse files Browse the repository at this point in the history
  • Loading branch information
arash77 committed Jun 12, 2024
1 parent afe36ef commit b074fb6
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions lib/plugins/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,34 @@ def handle_url_card(
description_tag = soup.find("meta", attrs={"property": "og:description"})
description_tag_alt = soup.find("meta", attrs={"name": "description"})
image_tag = soup.find("meta", attrs={"property": "og:image"})
title = title_tag["content"] if title_tag else title_tag_alt
title = (
title_tag.attrs.get("content")
if title_tag and hasattr(title_tag, "attrs")
else title_tag_alt
)
description = (
description_tag["content"]
if description_tag
else description_tag_alt["content"] if description_tag_alt else ""
description_tag.attrs.get("content")
if description_tag and hasattr(description_tag, "attrs")
else (
description_tag_alt.attrs.get("content")
if description_tag_alt and hasattr(description_tag_alt, "attrs")
else ""
)
)
uri = url
thumb = (
self.blueskysocial.upload_blob(
requests.get(image_tag["content"]).content
).blob
if image_tag
image_url = (
image_tag.attrs.get("content")
if image_tag and hasattr(image_tag, "attrs")
else None
)
if isinstance(image_url, str):
thumb = self.blueskysocial.upload_blob(
requests.get(image_url).content
).blob
embed_external = atproto.models.AppBskyEmbedExternal.Main(
external=atproto.models.AppBskyEmbedExternal.External(
title=title,
description=description,
uri=uri,
uri=url,
thumb=thumb,
)
)
Expand Down Expand Up @@ -214,27 +223,24 @@ def create_post(self, content, **kwargs) -> Tuple[bool, Optional[str]]:
)

reply_to = None

link = None
for text in content["chunks"]:
facets, last_url = self.parse_facets(text)
if not content["images"] or reply_to:
embed = self.handle_url_card(cast(str, last_url))

post = self.blueskysocial.send_post(
text, facets=facets, embed=embed, reply_to=reply_to
)
try:
post = self.blueskysocial.send_post(
text, facets=facets, embed=embed, reply_to=reply_to
)

for _ in range(5):
data = self.blueskysocial.get_posts([post.uri]).posts
if data:
break
else:
if reply_to is None:
link = f"https://bsky.app/profile/{self.blueskysocial.me.handle}/post/{post.uri.split('/')[-1]}"
root = atproto.models.create_strong_ref(post)
parent = atproto.models.create_strong_ref(post)
reply_to = atproto.models.AppBskyFeedPost.ReplyRef(
parent=parent, root=root
)
except:
return False, None

if reply_to is None:
link = f"https://bsky.app/profile/{self.blueskysocial.me.handle}/post/{post.uri.split('/')[-1]}"
root = atproto.models.create_strong_ref(post)
parent = atproto.models.create_strong_ref(post)
reply_to = atproto.models.AppBskyFeedPost.ReplyRef(parent=parent, root=root)

return True, link

0 comments on commit b074fb6

Please sign in to comment.