From e171578878306a6246e19556d38c9266865970d1 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Thu, 17 Nov 2022 22:45:51 +0100 Subject: [PATCH] Implement following/followers list retrieval --- toot/api.py | 16 ++++++++++++++++ toot/commands.py | 12 +++++++++++- toot/console.py | 16 ++++++++++++++++ toot/output.py | 12 +++++++----- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/toot/api.py b/toot/api.py index 1b4e1a2..3b2ffcf 100644 --- a/toot/api.py +++ b/toot/api.py @@ -260,6 +260,22 @@ def follow(app, user, account): def unfollow(app, user, account): return _account_action(app, user, account, 'unfollow') +def _get_account_list(app, user, path): + accounts = [] + while path: + response = http.get(app, user, path) + accounts += response.json() + path = _get_next_path(response.headers) + return accounts + +def following(app, user, account): + path = '/api/v1/accounts/{}/{}'.format(account, 'following') + return _get_account_list(app, user, path) + +def followers(app, user, account): + path = '/api/v1/accounts/{}/{}'.format(account, 'followers') + return _get_account_list(app, user, path) + def mute(app, user, account): return _account_action(app, user, account, 'mute') diff --git a/toot/commands.py b/toot/commands.py index 845a55f..caa7bb1 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -5,7 +5,7 @@ import sys from toot import api, config from toot.auth import login_interactive, login_browser_interactive, create_app_interactive from toot.exceptions import ConsoleError, NotFoundError -from toot.output import (print_out, print_instance, print_account, +from toot.output import (print_out, print_instance, print_account, print_acct_list, print_search_results, print_timeline, print_notifications) from toot.utils import assert_domain_exists, editor_input, multiline_input, EOF_KEY @@ -279,6 +279,16 @@ def unfollow(app, user, args): api.unfollow(app, user, account['id']) print_out("✓ You are no longer following {}".format(args.account)) +def following(app, user, args): + account = _find_account(app, user, args.account) + response = api.following(app, user, account['id']) + print_acct_list(response) + +def followers(app, user, args): + account = _find_account(app, user, args.account) + response = api.followers(app, user, account['id']) + print_acct_list(response) + def mute(app, user, args): account = _find_account(app, user, args.account) diff --git a/toot/console.py b/toot/console.py index a0fc0d2..4a30a83 100644 --- a/toot/console.py +++ b/toot/console.py @@ -441,6 +441,22 @@ ACCOUNTS_COMMANDS = [ ], require_auth=True, ), + Command( + name="following", + description="List accounts followed by the given account", + arguments=[ + account_arg, + ], + require_auth=True, + ), + Command( + name="followers", + description="List accounts following the given account", + arguments=[ + account_arg, + ], + require_auth=True, + ), Command( name="mute", description="Mute an account", diff --git a/toot/output.py b/toot/output.py index 4f64ed4..2b464a9 100644 --- a/toot/output.py +++ b/toot/output.py @@ -126,6 +126,12 @@ HASHTAG_PATTERN = re.compile(r'(?\\1', line) +def print_acct_list(accounts): + for account in accounts: + print_out("* @{} {}".format( + account['acct'], + account['display_name'] + )) def print_search_results(results): accounts = results['accounts'] @@ -133,11 +139,7 @@ def print_search_results(results): if accounts: print_out("\nAccounts:") - for account in accounts: - print_out("* @{} {}".format( - account['acct'], - account['display_name'] - )) + print_acct_list(accounts) if hashtags: print_out("\nHashtags:")