๐Ÿ Tiny CLI to post simultaneously to Mastodon and Bluesky
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Adds `--alt-texts`

Closes #1

+24 -6
+24 -6
not_my_ex/cli/post.py
··· 1 1 from asyncio import gather, get_event_loop 2 2 from collections import namedtuple 3 + from itertools import zip_longest 3 4 from os import getenv 4 5 from pathlib import Path 5 6 from subprocess import call ··· 41 42 return Sent(url, client.name, client.emoji) 42 43 43 44 44 - async def media_from(auth: Auth, path: str, ask_for_alt_text: bool) -> Media: 45 - media = await Media.from_img(path, None, auth.image_size_limit) 45 + async def media_from( 46 + auth: Auth, path: str, alt_text: str | None, ask_for_alt_text: bool 47 + ) -> Media: 48 + media = await Media.from_img(path, alt_text, auth.image_size_limit) 46 49 if ask_for_alt_text: 47 50 media.check_alt_text() 48 51 return media 49 52 50 53 51 54 async def main( 52 - auth: Auth, text: str, images: list[str], lang: str | None, yes_to_all: bool 55 + auth: Auth, 56 + text: str, 57 + images: list[str], 58 + alt_texts: list[str], 59 + lang: str | None, 60 + yes_to_all: bool, 53 61 ) -> None: 54 62 if len(images) > 4: 55 - raise ValueError("You can only post up to 4 images") 63 + error("You can only post up to 4 images") 64 + 65 + if len(alt_texts) > len(images): 66 + error(f"Got {len(alt_texts)} alt texts, but only {len(images)}") 56 67 57 68 if await os.path.exists(text): 58 69 async with open(text) as handler: 59 70 text = await handler.read() 60 71 61 - load = tuple(media_from(auth, path, not yes_to_all) for path in images) 72 + load = tuple( 73 + media_from(auth, path, alt_text, not yes_to_all) 74 + for path, alt_text in zip_longest(images, alt_texts) 75 + ) 62 76 try: 63 77 imgs = await gather(*load) 64 78 except ImageTooBigError as err: ··· 99 113 images: Annotated[ 100 114 list[str], Option("--images", "-i", help="Path to the images to post (max. 4)") 101 115 ] = [], 116 + alt_texts: Annotated[ 117 + list[str], 118 + Option("--alt-texts", "-a", help="Alt text for --images (same order)"), 119 + ] = [], 102 120 lang: Annotated[ 103 121 str | None, 104 122 Option( ··· 141 159 text = text or editor() 142 160 loop = get_event_loop() 143 161 try: 144 - loop.run_until_complete(main(auth, text, images, lang, yes_to_all)) 162 + loop.run_until_complete(main(auth, text, images, alt_texts, lang, yes_to_all)) 145 163 except PostTooLongError as err: 146 164 error(err)