Command line support for following hashtags (Mastodon 4+)

This commit is contained in:
Daniel Schwarz 2022-12-20 12:55:32 -05:00 committed by Ivan Habunek
parent a3fa7e1e3a
commit 67b52757a4
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
4 changed files with 76 additions and 8 deletions

View File

@ -24,6 +24,12 @@ def _status_action(app, user, status_id, action, data=None):
return http.post(app, user, url, data=data).json()
def _tag_action(app, user, tag_name, action):
url = '/api/v1/tags/{}/{}'.format(tag_name, action)
return http.post(app, user, url).json()
def create_app(domain, scheme='https'):
url = '{}://{}/api/v1/apps'.format(scheme, domain)
@ -318,23 +324,36 @@ def unfollow(app, user, account):
return _account_action(app, user, account, 'unfollow')
def _get_account_list(app, user, path):
accounts = []
def follow_tag(app, user, tag_name):
return _tag_action(app, user, tag_name, 'follow')
def unfollow_tag(app, user, tag_name):
return _tag_action(app, user, tag_name, 'unfollow')
def _get_response_list(app, user, path):
items = []
while path:
response = http.get(app, user, path)
accounts += response.json()
items += response.json()
path = _get_next_path(response.headers)
return accounts
return items
def following(app, user, account):
path = '/api/v1/accounts/{}/{}'.format(account, 'following')
return _get_account_list(app, user, path)
return _get_response_list(app, user, path)
def followers(app, user, account):
path = '/api/v1/accounts/{}/{}'.format(account, 'followers')
return _get_account_list(app, user, path)
return _get_response_list(app, user, path)
def followed_tags(app, user):
path = '/api/v1/followed_tags'
return _get_response_list(app, user, path)
def mute(app, user, account):

View File

@ -8,7 +8,8 @@ from toot import api, config, __version__
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
from toot.exceptions import ApiError, ConsoleError
from toot.output import (print_out, print_instance, print_account, print_acct_list,
print_search_results, print_timeline, print_notifications)
print_search_results, print_timeline, print_notifications,
print_tag_list)
from toot.tui.utils import parse_datetime
from toot.utils import editor_input, multiline_input, EOF_KEY
@ -323,6 +324,23 @@ def followers(app, user, args):
print_acct_list(response)
def follow_tag(app, user, args):
tn = args.tag_name if not args.tag_name.startswith("#") else args.tag_name[1:]
api.follow_tag(app, user, tn)
print_out("<green>✓ You are now following #{}</green>".format(tn))
def unfollow_tag(app, user, args):
tn = args.tag_name if not args.tag_name.startswith("#") else args.tag_name[1:]
api.unfollow_tag(app, user, tn)
print_out("<green>✓ You are no longer following #{}</green>".format(tn))
def followed_tags(app, user, args):
response = api.followed_tags(app, user)
print_tag_list(response)
def mute(app, user, args):
account = _find_account(app, user, args.account)
api.mute(app, user, account['id'])

View File

@ -161,6 +161,10 @@ visibility_arg = (["-v", "--visibility"], {
"the TOOT_POST_VISIBILITY environment variable",
})
tag_arg = (["tag_name"], {
"type": str,
"help": "tag name, e.g. Caturday, or \"#Caturday\"",
})
# Arguments for selecting a timeline (see `toot.commands.get_timeline_generator`)
common_timeline_args = [
@ -552,7 +556,28 @@ ACCOUNTS_COMMANDS = [
),
]
COMMANDS = AUTH_COMMANDS + READ_COMMANDS + TUI_COMMANDS + POST_COMMANDS + STATUS_COMMANDS + ACCOUNTS_COMMANDS
TAG_COMMANDS = [
Command(
name="follow_tag",
description="Follow a hashtag",
arguments=[tag_arg],
require_auth=True,
),
Command(
name="unfollow_tag",
description="Unfollow a hashtag",
arguments=[tag_arg],
require_auth=True,
),
Command(
name="followed_tags",
description="List hashtags you follow",
arguments=[],
require_auth=True,
),
]
COMMANDS = AUTH_COMMANDS + READ_COMMANDS + TUI_COMMANDS + POST_COMMANDS + STATUS_COMMANDS + ACCOUNTS_COMMANDS + TAG_COMMANDS
def print_usage():
@ -565,6 +590,7 @@ def print_usage():
("Post", POST_COMMANDS),
("Status", STATUS_COMMANDS),
("Accounts", ACCOUNTS_COMMANDS),
("Hashtags", TAG_COMMANDS),
]
print_out("<green>{}</green>".format(CLIENT_NAME))

View File

@ -198,6 +198,11 @@ def print_acct_list(accounts):
print_out(f"* <green>@{account['acct']}</green> {account['display_name']}")
def print_tag_list(tags):
for tag in tags:
print_out(f"* <green>#{tag['name']}\t</green> {tag['url']}")
def print_search_results(results):
accounts = results['accounts']
hashtags = results['hashtags']