From e754f6fe59b9c3ee3f63e99ebbf8b813b8bb13c5 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Mon, 26 May 2025 06:40:29 -0400 Subject: [PATCH] Add ability to uppload photos 1 at a time --- blastodon/client/client.py | 18 +++++++++++++++++- pyproject.toml | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/blastodon/client/client.py b/blastodon/client/client.py index b3fb7fb..e0a89ba 100644 --- a/blastodon/client/client.py +++ b/blastodon/client/client.py @@ -1,7 +1,9 @@ from typing import Self +from pathlib import Path from atproto import Client as BskyClient from mastodon import Mastodon, MastodonError +import magic from blastodon import auth from blastodon.client import RetreivedPost, CreatedPost @@ -36,4 +38,18 @@ class Client: return CreatedPost(bsky=bsky_post, mastodon=mastodon_post) - \ No newline at end of file + def send_image_post(self, post_text: str, image_path: Path | str, alt_text: str): + with open(image_path, mode='rb') as file: + image = file.read() + bsky_post = self.bsky.send_image(text=post_text, image=image, image_alt=alt_text) + try: + mime = magic.from_buffer(image, mime=True) + media_upload = self.mastodon.media_post(media_file=image, mime_type=mime, description=alt_text) + mastodon_post = self.mastodon.status_post(status=post_text, media_ids=[media_upload.id]) + 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 + + mastodon_post.url + return CreatedPost(bsky=bsky_post, mastodon=mastodon_post) diff --git a/pyproject.toml b/pyproject.toml index e50c41c..6601a2d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ license = "AGPL-3.0-only" dependencies = [ "atproto", "mastodon.py", + "python-magic", ] dynamic = ["version"]