From 9b48432d04d7ea7904ede5e18b8790ec9765eb56 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Wed, 19 Apr 2017 15:29:40 +0200 Subject: [PATCH] Add whois command --- toot/commands.py | 42 +++++++++++++++++++++++++++++------------- toot/console.py | 13 +++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/toot/commands.py b/toot/commands.py index cb15be9..243d101 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -11,7 +11,7 @@ from datetime import datetime from future.moves.itertools import zip_longest from getpass import getpass from itertools import chain -from textwrap import TextWrapper +from textwrap import TextWrapper, wrap from toot import api, config, DEFAULT_INSTANCE, User, App, ConsoleError from toot.output import green, yellow, print_error @@ -260,13 +260,34 @@ def _do_upload(app, user, file): def _find_account(app, user, account_name): - """For a given account name, returns the Account object or None if not found.""" + """For a given account name, returns the Account object or raises an exception if not found.""" response = api.search(app, user, account_name, False) for account in response['accounts']: if account['acct'] == account_name or "@" + account['acct'] == account_name: return account + raise ConsoleError("Account not found") + + +def _print_account(account): + print("{} {}".format(green("@" + account['acct']), account['display_name'])) + + if account['note']: + print("") + note = BeautifulSoup(account['note'], "html.parser") + print("\n".join(wrap(note.get_text()))) + + print("") + print("ID: " + green(account['id'])) + print("Since: " + green(account['created_at'][:19].replace('T', ' @ '))) + print("") + print("Followers: " + yellow(account['followers_count'])) + print("Following: " + yellow(account['following_count'])) + print("Statuses: " + yellow(account['statuses_count'])) + print("") + print(account['url']) + def follow(app, user, args): account = _find_account(app, user, args.account) @@ -293,15 +314,10 @@ def unfollow(app, user, args): def whoami(app, user, args): - response = api.verify_credentials(app, user) + account = api.verify_credentials(app, user) + _print_account(account) - print("{} {}".format(green("@" + response['acct']), response['display_name'])) - print(response['note']) - print(response['url']) - print("") - print("ID: " + green(response['id'])) - print("Since: " + green(response['created_at'][:19].replace('T', ' @ '))) - print("") - print("Followers: " + yellow(response['followers_count'])) - print("Following: " + yellow(response['following_count'])) - print("Statuses: " + yellow(response['statuses_count'])) + +def whois(app, user, args): + account = _find_account(app, user, args.account) + _print_account(account) diff --git a/toot/console.py b/toot/console.py index 2bcf97b..378eec5 100644 --- a/toot/console.py +++ b/toot/console.py @@ -57,6 +57,16 @@ COMMANDS = [ arguments=[], require_auth=True, ), + Command( + name="whois", + description="Display user details", + arguments=[ + (["account"], { + "help": "account name or numeric ID" + }), + ], + require_auth=True, + ), Command( name="post", description="Post a status text to your timeline", @@ -178,6 +188,9 @@ def run_command(app, user, name, args): fn = commands.__dict__.get(name) + if not fn: + raise NotImplementedError("Command '{}' does not have an implementation.".format(name)) + return fn(app, user, parsed_args)