Add 'upload' command for uploading media - toot - Unnamed repository; edit this file 'description' to name the repository.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit fd08b3884a1d50957af9f087545ad60518acec2d
(DIR) parent 5f85d0847e1cb1f6ab3cb4710ce4e166395fa7ab
(HTM) Author: Ivan Habunek <ivan@habunek.com>
Date: Fri, 14 Apr 2017 16:41:09 +0200
Add 'upload' command for uploading media
Diffstat:
toot/__init__.py | 10 ++++++++--
toot/console.py | 45 +++++++++++++++++++++++++++----
2 files changed, 48 insertions(+), 7 deletions(-)
---
(DIR) diff --git a/toot/__init__.py b/toot/__init__.py
@@ -19,11 +19,11 @@ def _get(app, user, url, params=None):
return response.json()
-def _post(app, user, url, data=None):
+def _post(app, user, url, data=None, files=None):
url = app.base_url + url
headers = {"Authorization": "Bearer " + user.access_token}
- response = requests.post(url, data, headers=headers)
+ response = requests.post(url, data, files=files, headers=headers)
response.raise_for_status()
return response.json()
@@ -76,3 +76,9 @@ def post_status(app, user, status):
def timeline_home(app, user):
return _get(app, user, '/api/v1/timelines/home')
+
+
+def upload_media(app, user, file):
+ return _post(app, user, '/api/v1/media', files={
+ 'file': file
+ })
(DIR) diff --git a/toot/console.py b/toot/console.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
import os
import sys
import logging
@@ -11,19 +13,27 @@ 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, DEFAULT_INSTANCE
+from . import create_app, login, post_status, timeline_home, upload_media, DEFAULT_INSTANCE
class ConsoleError(Exception):
pass
+def red(text):
+ return "\033[31m{}\033[0m".format(text)
+
+
def green(text):
- return "\033[92m{}\033[0m".format(text)
+ return "\033[32m{}\033[0m".format(text)
-def red(text):
- return "\033[91m{}\033[0m".format(text)
+def yellow(text):
+ return "\033[33m{}\033[0m".format(text)
+
+
+def print_error(text):
+ print(red(text), file=sys.stderr)
def create_app_interactive():
@@ -152,6 +162,28 @@ def cmd_logout(app, user):
print("You are now logged out")
+def cmd_upload(app, user):
+ if len(sys.argv) < 3:
+ print_error("No status text given")
+ return
+
+ 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)
+
+ print("\nSuccessfully uploaded media ID {}, type '{}'".format(
+ yellow(response['id']), yellow(response['type'])))
+ print("Original URL: " + green(response['url']))
+ print("Preview URL: " + green(response['preview_url']))
+ print("Text URL: " + green(response['text_url']))
+
+
def run_command(command):
app = load_app()
user = load_user()
@@ -178,6 +210,9 @@ def run_command(command):
if command == 'timeline':
return cmd_timeline(app, user)
+ if command == 'upload':
+ return cmd_upload(app, user)
+
print(red("Unknown command '{}'\n".format(command)))
print_usage()
@@ -194,4 +229,4 @@ def main():
try:
run_command(command)
except ConsoleError as e:
- print(red(str(e)))
+ print_error(str(e))