From 8285abb04e97ca6a2fa1b741c4a2cc38671f300c Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sat, 15 Apr 2017 12:00:05 +0200 Subject: [PATCH] Allow attaching media files to posts fixes #1 --- README.rst | 4 ++++ toot/__init__.py | 5 +++-- toot/console.py | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/README.rst b/README.rst index 743bc2e..fdb943a 100644 --- a/README.rst +++ b/README.rst @@ -69,3 +69,7 @@ To post a new status to your timeline: .. code-block:: toot post "Hello world!" + +Optionally attach an image or video to the status: + + toot post "Hello world!" --media=path/to/world.jpg diff --git a/toot/__init__.py b/toot/__init__.py index 06320da..34bbaaf 100644 --- a/toot/__init__.py +++ b/toot/__init__.py @@ -94,9 +94,10 @@ def login(app, username, password): return User(username, access_token) -def post_status(app, user, status): +def post_status(app, user, status, media_ids=None): return _post(app, user, '/api/v1/statuses', { - 'status': status + 'status': status, + 'media_ids[]': media_ids, }) diff --git a/toot/console.py b/toot/console.py index 35cb912..547bd80 100644 --- a/toot/console.py +++ b/toot/console.py @@ -7,10 +7,11 @@ import logging from bs4 import BeautifulSoup from builtins import input from datetime import datetime +from future.moves.itertools import zip_longest from getpass import getpass from itertools import chain +from optparse import OptionParser from textwrap import TextWrapper -from future.moves.itertools import zip_longest from .config import save_user, load_user, load_app, save_app, CONFIG_APP_FILE, CONFIG_USER_FILE from . import create_app, login, post_status, timeline_home, upload_media, DEFAULT_INSTANCE @@ -137,11 +138,24 @@ def cmd_timeline(app, user): def cmd_post_status(app, user): - if len(sys.argv) < 3: - print(red("No status text given")) - return + parser = OptionParser(usage="toot post [options] TEXT") - response = post_status(app, user, sys.argv[2]) + parser.add_option("-m", "--media", dest="media", type="string", + help="path to the media file to attach") + + (options, args) = parser.parse_args() + + if len(args) < 2: + parser.print_help() + raise ConsoleError("No text given") + + if options.media: + media = do_upload(app, user, options.media) + media_ids = [media['id']] + else: + media_ids = None + + response = post_status(app, user, args[1], media_ids=media_ids) print("Toot posted: " + green(response.get('url'))) @@ -169,13 +183,7 @@ def cmd_upload(app, user): path = sys.argv[2] - if not os.path.exists(path): - print_error("File does not exist: " + path) - return - - with open(path, 'rb') as f: - print("Uploading {} ...".format(green(f.name))) - response = upload_media(app, user, f) + response = do_upload(path) print("\nSuccessfully uploaded media ID {}, type '{}'".format( yellow(response['id']), yellow(response['type']))) @@ -184,6 +192,15 @@ def cmd_upload(app, user): print("Text URL: " + green(response['text_url'])) +def do_upload(app, user, path): + if not os.path.exists(path): + raise ConsoleError("File does not exist: " + path) + + with open(path, 'rb') as f: + print("Uploading media: {}".format(green(f.name))) + return upload_media(app, user, f) + + def run_command(command): app = load_app() user = load_user()