Created
November 18, 2024 18:42
-
-
Save dutc/50f54d50afa16c16ddc78c463d1fdac5 to your computer and use it in GitHub Desktop.
Posting to Bluesky via API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from asyncio import run | |
from os import environ | |
from atproto import AsyncClient, client_utils, models | |
def chunked(text, *, size): | |
buf = [] | |
for ln in text.splitlines(): | |
if sum(map(len, buf)) + len(ln) >= size: | |
yield '\n'.join(buf) | |
buf = [] | |
buf.append(ln) | |
yield '\n'.join(buf) | |
async def main(handle, password, code): | |
parts = [*chunked(code, size=290)] | |
messages = [ | |
f'{txt}\n### {idx:02d}/{len(parts):02d}' | |
for idx, txt in enumerate(parts, start=1) | |
] | |
client = AsyncClient() | |
profile = await client.login(handle, password) | |
root_ref = last_ref = None | |
for msg in messages: | |
post = await client.send_post( | |
client_utils.TextBuilder().text(msg), | |
reply_to=models.AppBskyFeedPost.ReplyRef(parent=last_ref, root=root_ref) if root_ref else None, | |
) | |
last_ref = models.create_strong_ref(post) | |
root_ref = root_ref or last_ref | |
if __name__ == '__main__': | |
with open(__file__) as f: | |
code = f.read().replace(' ' * 4, '\t') | |
run(main(handle=environ['HANDLE'], password=environ['PASSWORD'], code=code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment