From 22b32b0b0ef549fa30b75567522af564e14492c6 Mon Sep 17 00:00:00 2001 From: Guillem Hernandez Sola Date: Fri, 8 May 2026 14:24:17 +0200 Subject: [PATCH] pst_bsky new --- bsky_post.py | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/bsky_post.py b/bsky_post.py index 61d754c..0277712 100644 --- a/bsky_post.py +++ b/bsky_post.py @@ -551,12 +551,18 @@ def post_to_bsky( video_settle_delay: float = 30.0, allow_pds_video_fallback: bool = False, ) -> bool: - rich_text = make_rich(text) + post_text = text.strip() + + # Allow empty text only if media exists + if not post_text and not image_path and not video_path: + logging.error("โŒ Empty post text with no media is not allowed.") + return False try: + embed_obj = None + if video_path: logging.info(f"๐ŸŽฌ Preparing video upload: {video_path}") - video_embed = upload_video_smart( client=client, video_path=video_path, @@ -565,29 +571,48 @@ def post_to_bsky( settle_delay_seconds=video_settle_delay, allow_pds_video_fallback=allow_pds_video_fallback, ) - if not video_embed: logging.error("โŒ Aborting post: video upload/processing failed.") return False - - logging.info("๐Ÿš€ Sending video post...") - result = client.send_post(text=rich_text, embed=video_embed, langs=langs) + embed_obj = video_embed elif image_path: image = upload_image(client, image_path, alt_text=alt_text) if not image: logging.error("โŒ Aborting post: image upload failed.") return False + embed_obj = models.AppBskyEmbedImages.Main(images=[image]) - embed = models.AppBskyEmbedImages.Main(images=[image]) - logging.info("๐Ÿš€ Sending image post...") - result = client.send_post(text=rich_text, embed=embed, langs=langs) + # Build record explicitly (most reliable) + record = { + "$type": "app.bsky.feed.post", + "text": post_text, # <-- guaranteed string + "createdAt": time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime()), + } - else: - logging.info("๐Ÿš€ Sending text post...") - result = client.send_post(text=rich_text, langs=langs) + if langs: + record["langs"] = langs - uri = getattr(result, "uri", None) + if embed_obj is not None: + # atproto models -> plain dict + if hasattr(embed_obj, "model_dump"): + record["embed"] = embed_obj.model_dump(by_alias=True, exclude_none=True) + elif hasattr(embed_obj, "dict"): + record["embed"] = embed_obj.dict(by_alias=True, exclude_none=True) + else: + record["embed"] = embed_obj + + logging.info(f"๐Ÿงพ Final record text={record.get('text')!r}, has_embed={'embed' in record}") + + resp = client.com.atproto.repo.create_record( + models.ComAtprotoRepoCreateRecord.Data( + repo=client.me.did, + collection="app.bsky.feed.post", + record=record, + ) + ) + + uri = getattr(resp, "uri", None) or (resp.get("uri") if isinstance(resp, dict) else None) logging.info(f"โœ… Post published! URI: {uri}") return True @@ -595,7 +620,6 @@ def post_to_bsky( logging.error(f"โŒ Failed to send post: {repr(e)}") return False - # ============================================================ # CLI # ============================================================