Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor bluesky plugin #42

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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