pst_bsky new
This commit is contained in:
52
bsky_post.py
52
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
|
||||
# ============================================================
|
||||
|
||||
Reference in New Issue
Block a user