Add ability to uppload photos 1 at a time

This commit is contained in:
D. Scott Boggs 2025-05-26 06:40:29 -04:00
parent ff83ad6647
commit e754f6fe59
2 changed files with 18 additions and 1 deletions

View file

@ -1,7 +1,9 @@
from typing import Self from typing import Self
from pathlib import Path
from atproto import Client as BskyClient from atproto import Client as BskyClient
from mastodon import Mastodon, MastodonError from mastodon import Mastodon, MastodonError
import magic
from blastodon import auth from blastodon import auth
from blastodon.client import RetreivedPost, CreatedPost from blastodon.client import RetreivedPost, CreatedPost
@ -36,4 +38,18 @@ class Client:
return CreatedPost(bsky=bsky_post, mastodon=mastodon_post) return CreatedPost(bsky=bsky_post, mastodon=mastodon_post)
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)

View file

@ -14,6 +14,7 @@ license = "AGPL-3.0-only"
dependencies = [ dependencies = [
"atproto", "atproto",
"mastodon.py", "mastodon.py",
"python-magic",
] ]
dynamic = ["version"] dynamic = ["version"]