blastodon/blastodon/client/client.py
2025-05-24 15:43:16 -04:00

39 lines
1.1 KiB
Python

from typing import Self
from atproto import Client as BskyClient
from mastodon import Mastodon, MastodonError
from blastodon import auth
from blastodon.client import RetreivedPost, CreatedPost
class Client:
"""A wrapper around both Mastodon and Bsky clients"""
mastodon: Mastodon
bsky: BskyClient
def __init__(self, mastodon: Mastodon, bsky: BskyClient):
self.mastodon = mastodon
self.bsky = bsky
@classmethod
def init_cli(cls) -> Self:
return cls(
mastodon=auth.cli.auth_mastodon(),
bsky=auth.cli.auth_bsky()
)
def most_recent_posts(self) -> RetreivedPost:
return RetreivedPost.most_recent(client=self)
def send_text_post(self, text: str):
bsky_post = self.bsky.send_post(text=text)
try:
mastodon_post = self.mastodon.status_post(status=text)
except MastodonError as err:
print('error posting to mastodon after posting to bsky. Deleting bsky post.')
self.bsky.delete_post(bsky_post.uri)
raise err
return CreatedPost(bsky=bsky_post, mastodon=mastodon_post)