diff --git a/changelog.yaml b/changelog.yaml
index 6892e52..3d1c874 100644
--- a/changelog.yaml
+++ b/changelog.yaml
@@ -2,6 +2,7 @@
date: TBA
changes:
- "Add `--scheduled-at` option to `toot post`, allows scheduling toots"
+ - "Add `--description` option to `toot post`, for adding descriptions to media attachments (thanks @ansuz)"
0.27.0:
date: 2020-06-15
diff --git a/toot/api.py b/toot/api.py
index 27d8c9d..da5f6f0 100644
--- a/toot/api.py
+++ b/toot/api.py
@@ -217,10 +217,11 @@ def anon_tag_timeline_generator(instance, hashtag, local=False, limit=20):
return _anon_timeline_generator(instance, path, params)
-def upload_media(app, user, file):
- return http.post(app, user, '/api/v1/media', files={
- 'file': file
- }).json()
+def upload_media(app, user, file, description=None):
+ return http.post(app, user, '/api/v1/media',
+ data={'description': description},
+ files={'file': file}
+ ).json()
def search(app, user, query, resolve):
diff --git a/toot/commands.py b/toot/commands.py
index c041ff9..d19cdae 100644
--- a/toot/commands.py
+++ b/toot/commands.py
@@ -87,15 +87,20 @@ def post(app, user, args):
if not args.text and not sys.stdin.isatty():
args.text = sys.stdin.read().rstrip()
- if args.media:
- media = [_do_upload(app, user, file) for file in args.media]
- media_ids = [m["id"] for m in media]
- else:
- media = None
- media_ids = None
+ # Match media to corresponding description and upload
+ media = args.media or []
+ descriptions = args.description or []
+ uploaded_media = []
- if media and not args.text:
- args.text = "\n".join(m['text_url'] for m in media)
+ for idx, file in enumerate(media):
+ description = descriptions[idx].strip() if idx < len(descriptions) else None
+ result = _do_upload(app, user, file, description)
+ uploaded_media.append(result)
+
+ media_ids = [m["id"] for m in uploaded_media]
+
+ if uploaded_media and not args.text:
+ args.text = "\n".join(m['text_url'] for m in uploaded_media)
if args.editor:
args.text = editor_input(args.editor, args.text)
@@ -210,7 +215,7 @@ def activate(app, user, args):
def upload(app, user, args):
- response = _do_upload(app, user, args.file)
+ response = _do_upload(app, user, args.file, args.description)
msg = "Successfully uploaded media ID {}, type '{}'"
@@ -226,9 +231,9 @@ def search(app, user, args):
print_search_results(response)
-def _do_upload(app, user, file):
+def _do_upload(app, user, file, description):
print_out("Uploading media: {}".format(file.name))
- return api.upload_media(app, user, file)
+ return api.upload_media(app, user, file, description=description)
def _find_account(app, user, account_name):
diff --git a/toot/console.py b/toot/console.py
index 5e88a52..6a004ca 100644
--- a/toot/console.py
+++ b/toot/console.py
@@ -297,6 +297,12 @@ POST_COMMANDS = [
"help": "path to the media file to attach (specify multiple "
"times to attach up to 4 files)"
}),
+ (["-d", "--description"], {
+ "action": "append",
+ "type": str,
+ "help": "plain-text description of the media for accessibility "
+ "purposes, one per attached media"
+ }),
(["-v", "--visibility"], {
"type": visibility,
"default": "public",
@@ -341,7 +347,11 @@ POST_COMMANDS = [
(["file"], {
"help": "Path to the file to upload",
"type": FileType('rb')
- })
+ }),
+ (["-d", "--description"], {
+ "type": str,
+ "help": "plain-text description of the media for accessibility purposes"
+ }),
],
require_auth=True,
),