pst_bsky new
This commit is contained in:
50
bsky_post.py
50
bsky_post.py
@@ -551,12 +551,18 @@ def post_to_bsky(
|
|||||||
video_settle_delay: float = 30.0,
|
video_settle_delay: float = 30.0,
|
||||||
allow_pds_video_fallback: bool = False,
|
allow_pds_video_fallback: bool = False,
|
||||||
) -> bool:
|
) -> 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:
|
try:
|
||||||
|
embed_obj = None
|
||||||
|
|
||||||
if video_path:
|
if video_path:
|
||||||
logging.info(f"🎬 Preparing video upload: {video_path}")
|
logging.info(f"🎬 Preparing video upload: {video_path}")
|
||||||
|
|
||||||
video_embed = upload_video_smart(
|
video_embed = upload_video_smart(
|
||||||
client=client,
|
client=client,
|
||||||
video_path=video_path,
|
video_path=video_path,
|
||||||
@@ -565,29 +571,48 @@ def post_to_bsky(
|
|||||||
settle_delay_seconds=video_settle_delay,
|
settle_delay_seconds=video_settle_delay,
|
||||||
allow_pds_video_fallback=allow_pds_video_fallback,
|
allow_pds_video_fallback=allow_pds_video_fallback,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not video_embed:
|
if not video_embed:
|
||||||
logging.error("❌ Aborting post: video upload/processing failed.")
|
logging.error("❌ Aborting post: video upload/processing failed.")
|
||||||
return False
|
return False
|
||||||
|
embed_obj = video_embed
|
||||||
logging.info("🚀 Sending video post...")
|
|
||||||
result = client.send_post(text=rich_text, embed=video_embed, langs=langs)
|
|
||||||
|
|
||||||
elif image_path:
|
elif image_path:
|
||||||
image = upload_image(client, image_path, alt_text=alt_text)
|
image = upload_image(client, image_path, alt_text=alt_text)
|
||||||
if not image:
|
if not image:
|
||||||
logging.error("❌ Aborting post: image upload failed.")
|
logging.error("❌ Aborting post: image upload failed.")
|
||||||
return False
|
return False
|
||||||
|
embed_obj = models.AppBskyEmbedImages.Main(images=[image])
|
||||||
|
|
||||||
embed = models.AppBskyEmbedImages.Main(images=[image])
|
# Build record explicitly (most reliable)
|
||||||
logging.info("🚀 Sending image post...")
|
record = {
|
||||||
result = client.send_post(text=rich_text, embed=embed, langs=langs)
|
"$type": "app.bsky.feed.post",
|
||||||
|
"text": post_text, # <-- guaranteed string
|
||||||
|
"createdAt": time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime()),
|
||||||
|
}
|
||||||
|
|
||||||
|
if langs:
|
||||||
|
record["langs"] = langs
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
logging.info("🚀 Sending text post...")
|
record["embed"] = embed_obj
|
||||||
result = client.send_post(text=rich_text, langs=langs)
|
|
||||||
|
|
||||||
uri = getattr(result, "uri", None)
|
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}")
|
logging.info(f"✅ Post published! URI: {uri}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -595,7 +620,6 @@ def post_to_bsky(
|
|||||||
logging.error(f"❌ Failed to send post: {repr(e)}")
|
logging.error(f"❌ Failed to send post: {repr(e)}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# CLI
|
# CLI
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user