Allow attaching media files to posts

fixes #1
This commit is contained in:
Ivan Habunek 2017-04-15 12:00:05 +02:00
parent c2625d536f
commit 8285abb04e
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 36 additions and 14 deletions

View File

@ -69,3 +69,7 @@ To post a new status to your timeline:
.. code-block:: .. code-block::
toot post "Hello world!" toot post "Hello world!"
Optionally attach an image or video to the status:
toot post "Hello world!" --media=path/to/world.jpg

View File

@ -94,9 +94,10 @@ def login(app, username, password):
return User(username, access_token) 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', { return _post(app, user, '/api/v1/statuses', {
'status': status 'status': status,
'media_ids[]': media_ids,
}) })

View File

@ -7,10 +7,11 @@ import logging
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from builtins import input from builtins import input
from datetime import datetime from datetime import datetime
from future.moves.itertools import zip_longest
from getpass import getpass from getpass import getpass
from itertools import chain from itertools import chain
from optparse import OptionParser
from textwrap import TextWrapper 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 .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 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): def cmd_post_status(app, user):
if len(sys.argv) < 3: parser = OptionParser(usage="toot post [options] TEXT")
print(red("No status text given"))
return
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'))) print("Toot posted: " + green(response.get('url')))
@ -169,13 +183,7 @@ def cmd_upload(app, user):
path = sys.argv[2] path = sys.argv[2]
if not os.path.exists(path): response = do_upload(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)
print("\nSuccessfully uploaded media ID {}, type '{}'".format( print("\nSuccessfully uploaded media ID {}, type '{}'".format(
yellow(response['id']), yellow(response['type']))) yellow(response['id']), yellow(response['type'])))
@ -184,6 +192,15 @@ def cmd_upload(app, user):
print("Text URL: " + green(response['text_url'])) 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): def run_command(command):
app = load_app() app = load_app()
user = load_user() user = load_user()