Add 'upload' command for uploading media

pull/2/head
Ivan Habunek 2017-04-14 16:41:09 +02:00
rodzic 5f85d0847e
commit fd08b3884a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: CDBD63C43A30BB95
2 zmienionych plików z 50 dodań i 9 usunięć

Wyświetl plik

@ -19,11 +19,11 @@ def _get(app, user, url, params=None):
return response.json() return response.json()
def _post(app, user, url, data=None): def _post(app, user, url, data=None, files=None):
url = app.base_url + url url = app.base_url + url
headers = {"Authorization": "Bearer " + user.access_token} 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() response.raise_for_status()
return response.json() return response.json()
@ -76,3 +76,9 @@ def post_status(app, user, status):
def timeline_home(app, user): def timeline_home(app, user):
return _get(app, user, '/api/v1/timelines/home') return _get(app, user, '/api/v1/timelines/home')
def upload_media(app, user, file):
return _post(app, user, '/api/v1/media', files={
'file': file
})

Wyświetl plik

@ -1,3 +1,5 @@
from __future__ import print_function
import os import os
import sys import sys
import logging import logging
@ -11,19 +13,27 @@ from textwrap import TextWrapper
from future.moves.itertools import zip_longest 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, DEFAULT_INSTANCE from . import create_app, login, post_status, timeline_home, upload_media, DEFAULT_INSTANCE
class ConsoleError(Exception): class ConsoleError(Exception):
pass pass
def green(text):
return "\033[92m{}\033[0m".format(text)
def red(text): def red(text):
return "\033[91m{}\033[0m".format(text) return "\033[31m{}\033[0m".format(text)
def green(text):
return "\033[32m{}\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(): def create_app_interactive():
@ -152,6 +162,28 @@ def cmd_logout(app, user):
print("You are now logged out") 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): def run_command(command):
app = load_app() app = load_app()
user = load_user() user = load_user()
@ -178,6 +210,9 @@ def run_command(command):
if command == 'timeline': if command == 'timeline':
return cmd_timeline(app, user) return cmd_timeline(app, user)
if command == 'upload':
return cmd_upload(app, user)
print(red("Unknown command '{}'\n".format(command))) print(red("Unknown command '{}'\n".format(command)))
print_usage() print_usage()
@ -194,4 +229,4 @@ def main():
try: try:
run_command(command) run_command(command)
except ConsoleError as e: except ConsoleError as e:
print(red(str(e))) print_error(str(e))