From 82ed630864592ba19d84a809ef27664205d2ead4 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Wed, 2 Jan 2019 12:24:38 +0100 Subject: [PATCH] Add status related commands (un)favourite, (un)reblog & (un)pin. fixes #75 --- CHANGELOG.md | 2 ++ docs/index.rst | 2 +- docs/usage.rst | 48 ++++++++++++++++++++++++------------------ toot/api.py | 30 ++++++++++++++++++++++++++ toot/commands.py | 31 ++++++++++++++++++++++++++- toot/console.py | 55 +++++++++++++++++++++++++++++++++++++++++------- 6 files changed, 138 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06eaef6..f9adef7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ Changelog * Enable interaction with instances using http instead of https (#56) * Enable proxy usage via environment variables (#47) * Make `toot post` prompt for input if no text is given (#82) +* Add post-related commands: `favourite`, `unfavourite`, `reblog`, `unreblog`, + `pin` & `unpin` (#75) **0.19.0 (2018-06-27)** diff --git a/docs/index.rst b/docs/index.rst index b157bb6..10748e9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -17,7 +17,7 @@ toot is a commandline tool for interacting with Mastodon social networks. Features -------- -* Posting, replying, deleting statuses +* Posting, replying, deleting, favouriting, reblogging & pinning statuses * Support for media uploads, spoiler text, sensitive content * Search by account or hash tag * Following, muting and blocking accounts diff --git a/docs/usage.rst b/docs/usage.rst index c345edc..91a9d6d 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -13,32 +13,40 @@ Running ``toot -h`` shows the documentation for the given command. toot - a Mastodon CLI client Authentication: - toot login Log into a mastodon instance using your browser (recommended) - toot login_cli Log in from the console, does NOT support two factor authentication - toot activate Switch between logged in accounts. - toot logout Log out, delete stored access keys - toot auth Show logged in accounts and instances + toot login Log into a mastodon instance using your browser (recommended) + toot login_cli Log in from the console, does NOT support two factor authentication + toot activate Switch between logged in accounts. + toot logout Log out, delete stored access keys + toot auth Show logged in accounts and instances Read: - toot whoami Display logged in user details - toot whois Display account details - toot instance Display instance details - toot search Search for users or hashtags - toot timeline Show recent items in a timeline (home by default) - toot curses An experimental timeline app (doesn't work on Windows) + toot whoami Display logged in user details + toot whois Display account details + toot instance Display instance details + toot search Search for users or hashtags + toot timeline Show recent items in a timeline (home by default) + toot curses An experimental timeline app (doesn't work on Windows) Post: - toot post Post a status text to your timeline - toot upload Upload an image or video file - toot delete Delete an existing status + toot post Post a status text to your timeline + toot upload Upload an image or video file + + Status: + toot delete Delete a status + toot favourite Favourite a status + toot unfavourite Unfavourite a status + toot reblog Reblog a status + toot unreblog Unreblog a status + toot pin Pin a status + toot unpin Unpin a status Accounts: - toot follow Follow an account - toot unfollow Unfollow an account - toot mute Mute an account - toot unmute Unmute an account - toot block Block an account - toot unblock Unblock an account + toot follow Follow an account + toot unfollow Unfollow an account + toot mute Mute an account + toot unmute Unmute an account + toot block Block an account + toot unblock Unblock an account To get help for each command run: toot --help diff --git a/toot/api.py b/toot/api.py index ec57f0c..e0cca77 100644 --- a/toot/api.py +++ b/toot/api.py @@ -17,6 +17,12 @@ def _account_action(app, user, account, action): return http.post(app, user, url).json() +def _status_action(app, user, status_id, action): + url = '/api/v1/statuses/{}/{}'.format(status_id, action) + + return http.post(app, user, url).json() + + def create_app(domain, scheme='https'): url = '{}://{}/api/v1/apps'.format(scheme, domain) @@ -114,6 +120,30 @@ def delete_status(app, user, status_id): return http.delete(app, user, '/api/v1/statuses/{}'.format(status_id)) +def favourite(app, user, status_id): + return _status_action(app, user, status_id, 'favourite') + + +def unfavourite(app, user, status_id): + return _status_action(app, user, status_id, 'unfavourite') + + +def reblog(app, user, status_id): + return _status_action(app, user, status_id, 'reblog') + + +def unreblog(app, user, status_id): + return _status_action(app, user, status_id, 'unreblog') + + +def pin(app, user, status_id): + return _status_action(app, user, status_id, 'pin') + + +def unpin(app, user, status_id): + return _status_action(app, user, status_id, 'unpin') + + def timeline_home(app, user): return http.get(app, user, '/api/v1/timelines/home').json() diff --git a/toot/commands.py b/toot/commands.py index 55df571..4d9342b 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -77,10 +77,39 @@ def post(app, user, args): def delete(app, user, args): api.delete_status(app, user, args.status_id) - print_out("✓ Status deleted") +def favourite(app, user, args): + api.favourite(app, user, args.status_id) + print_out("✓ Status favourited") + + +def unfavourite(app, user, args): + api.unfavourite(app, user, args.status_id) + print_out("✓ Status unfavourited") + + +def reblog(app, user, args): + api.reblog(app, user, args.status_id) + print_out("✓ Status reblogged") + + +def unreblog(app, user, args): + api.unreblog(app, user, args.status_id) + print_out("✓ Status unreblogged") + + +def pin(app, user, args): + api.pin(app, user, args.status_id) + print_out("✓ Status pinned") + + +def unpin(app, user, args): + api.unpin(app, user, args.status_id) + print_out("✓ Status unpinned") + + def auth(app, user, args): config_data = config.load_config() diff --git a/toot/console.py b/toot/console.py index 4b0883f..c6e5363 100644 --- a/toot/console.py +++ b/toot/console.py @@ -72,6 +72,10 @@ scheme_arg = (["--disable-https"], { "const": "http", }) +status_id_arg = (["status_id"], { + "help": "ID of the status", + "type": int, +}) AUTH_COMMANDS = [ Command( @@ -243,15 +247,49 @@ POST_COMMANDS = [ ], require_auth=True, ), +] + +STATUS_COMMANDS = [ Command( name="delete", - description="Delete an existing status", - arguments=[ - (["status_id"], { - "help": "ID of the status to delete", - "type": int, - }) - ], + description="Delete a status", + arguments=[status_id_arg], + require_auth=True, + ), + Command( + name="favourite", + description="Favourite a status", + arguments=[status_id_arg], + require_auth=True, + ), + Command( + name="unfavourite", + description="Unfavourite a status", + arguments=[status_id_arg], + require_auth=True, + ), + Command( + name="reblog", + description="Reblog a status", + arguments=[status_id_arg], + require_auth=True, + ), + Command( + name="unreblog", + description="Unreblog a status", + arguments=[status_id_arg], + require_auth=True, + ), + Command( + name="pin", + description="Pin a status", + arguments=[status_id_arg], + require_auth=True, + ), + Command( + name="unpin", + description="Unpin a status", + arguments=[status_id_arg], require_auth=True, ), ] @@ -307,7 +345,7 @@ ACCOUNTS_COMMANDS = [ ), ] -COMMANDS = AUTH_COMMANDS + READ_COMMANDS + POST_COMMANDS + ACCOUNTS_COMMANDS +COMMANDS = AUTH_COMMANDS + READ_COMMANDS + POST_COMMANDS + STATUS_COMMANDS + ACCOUNTS_COMMANDS def print_usage(): @@ -317,6 +355,7 @@ def print_usage(): ("Authentication", AUTH_COMMANDS), ("Read", READ_COMMANDS), ("Post", POST_COMMANDS), + ("Status", STATUS_COMMANDS), ("Accounts", ACCOUNTS_COMMANDS), ]